-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcalls.py
More file actions
46 lines (39 loc) · 1.14 KB
/
calls.py
File metadata and controls
46 lines (39 loc) · 1.14 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
46
"""
Processing requests to GitHub API.
"""
import logging
from typing import Any, Dict, List, Union
import requests
from ..settings import GITHUB_API_URL, GITHUB_API_REQUEST_TIMEOUT
logger = logging.getLogger(__name__)
class GitHubApiCallFailed(Exception):
"""
Raised on actions that involve calls to GitHub API which are failed.
"""
def fetch_repository_forks(
owner: str,
repo: str,
sort: str = "newest",
per_page: int = 100,
page: int = 1,
) -> List[Dict[str, Any]]:
"""
Fetch forks for provided repository from GitHub.
"""
url = f"{GITHUB_API_URL}/repos/{owner}/{repo}/forks"
headers = {"Accept": "application/vnd.github.v3+json"}
params: Dict[str, Union[str, int]] = {
"sort": sort,
"per_page": per_page,
"page": page,
}
try:
r = requests.get(
url, headers=headers, params=params, timeout=GITHUB_API_REQUEST_TIMEOUT
)
r.raise_for_status()
response = r.json()
except Exception as e:
logger.error(repr(e))
raise GitHubApiCallFailed("An error occurred due fetching forks via GitHub API")
return response