FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Python-algorithm/web_programming/fetch_github_info.py at master · rasata/Python-algorithm · GitHub
rasata
/
Python-algorithm
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Python-algorithm
/
web_programming
/
fetch_github_info.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
60 lines (45 loc) · 1.54 KB
Breadcrumbs
Python-algorithm
/
web_programming
/
fetch_github_info.py
Copy path
File metadata and controls
60 lines (45 loc) · 1.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
"""
Created by sarathkaul on 14/11/19
Updated by lawric1 on 24/11/20
Authentication will be made via access token.
To generate your personal access token visit https://github.com/settings/tokens.
NOTE:
Never hardcode any credential information in the code. Always use an environment
file to store the private information and use the `os` module to get the information
during runtime.
Create a ".env" file in the root directory and write these two lines in that file
with your token::
#!/usr/bin/env bash
export USER_TOKEN=""
"""
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx",
# ]
# ///
from
__future__
import
annotations
import
os
from
typing
import
Any
import
httpx
BASE_URL
=
"https://api.github.com"
# https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user
AUTHENTICATED_USER_ENDPOINT
=
BASE_URL
+
"/user"
# https://github.com/settings/tokens
USER_TOKEN
=
os
.
environ
.
get
(
"USER_TOKEN"
,
""
)
def
fetch_github_info
(
auth_token
:
str
)
->
dict
[
Any
,
Any
]:
"""
Fetch GitHub info of a user using the httpx module
"""
headers
=
{
"Authorization"
:
f"token
{
auth_token
}
"
,
"Accept"
:
"application/vnd.github.v3+json"
,
}
return
httpx
.
get
(
AUTHENTICATED_USER_ENDPOINT
,
headers
=
headers
,
timeout
=
10
).
json
()
if
__name__
==
"__main__"
:
# pragma: no cover
if
USER_TOKEN
:
for
key
,
value
in
fetch_github_info
(
USER_TOKEN
).
items
():
print
(
f"
{
key
}
:
{
value
}
"
)
else
:
raise
ValueError
(
"'USER_TOKEN' field cannot be empty."
)
Back
|
FazBrowse Home
|
New Git URL