-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_client.py
More file actions
45 lines (39 loc) · 1.47 KB
/
github_client.py
File metadata and controls
45 lines (39 loc) · 1.47 KB
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
import httpx
import os
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", "") # optional, increases rate limit
BASE_URL = "https://api.github.com"
headers = {
"Accept": "application/vnd.github+json",
**({"Authorization": f"Bearer {GITHUB_TOKEN}"} if GITHUB_TOKEN else {})
}
async def get_user_profile(username: str) -> dict:
async with httpx.AsyncClient() as client:
r = await client.get(f"{BASE_URL}/users/{username}", headers=headers)
r.raise_for_status()
return r.json()
async def get_user_repos(username: str) -> list:
async with httpx.AsyncClient() as client:
r = await client.get(
f"{BASE_URL}/users/{username}/repos",
headers=headers,
params={"sort": "updated", "per_page": 30}
)
r.raise_for_status()
return r.json()
async def get_repo_languages(username: str, repo: str) -> dict:
async with httpx.AsyncClient() as client:
r = await client.get(
f"{BASE_URL}/repos/{username}/{repo}/languages",
headers=headers
)
r.raise_for_status()
return r.json()
async def get_repo_commits(username: str, repo: str) -> list:
async with httpx.AsyncClient() as client:
r = await client.get(
f"{BASE_URL}/repos/{username}/{repo}/commits",
headers=headers,
params={"per_page": 5, "author": username}
)
r.raise_for_status()
return r.json()