-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
88 lines (73 loc) · 3.09 KB
/
api.py
File metadata and controls
88 lines (73 loc) · 3.09 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
GitHub API interaction module.
This module provides functions for making requests to the GitHub API
and handling responses.
"""
import json
import requests
from printcolors import printcolors
def fetch_repo_endpoint(term, repo_url, headers):
"""
Fetch a GitHub repository endpoint, print it, and return the data.
Parameters
----------
term : str
The endpoint to fetch (e.g., "contributors", "languages").
repo_url : str
The repository in "owner/repo" format.
headers : dict
HTTP headers including authentication for GitHub API.
Returns
-------
success : bool
True if the request succeeded (status code 200), False otherwise.
data : dict | None
Parsed JSON response if successful, None otherwise.
"""
term = term.strip("/")
print(printcolors.BOLD + f"Fetching repository {term}" + printcolors.ENDC)
r = requests.get(f'https://api.github.com/repos/{repo_url}/{term}', headers=headers)
if r.status_code == 200:
parsed = r.json()
print(json.dumps(parsed, indent=4))
return True, parsed
else:
print(printcolors.FAIL + f"ERR: Request failed with status code {r.status_code}" + printcolors.ENDC)
return False, None
def fetch_profile_endpoint(term, accountid, headers):
print(printcolors.BOLD + f"Fetching user {accountid}" + printcolors.ENDC)
r = requests.get(f'https://api.github.com/users/{accountid}/{term}', headers=headers)
if r.status_code == 200:
parsed = r.json()
print(json.dumps(parsed, indent=4))
return True, parsed
else:
print(printcolors.FAIL + f"ERR: Request failed with status code {r.status_code}" + printcolors.ENDC)
return False, None
def fetch_user_profile(user_id, term, headers):
"""
Fetches a specific public attribute of a GitHub user and prints it.
Args:
user_id (str): The GitHub username of the target user.
term (str): The specific user attribute to retrieve (e.g., 'location', 'bio').
headers (dict): HTTP headers to include in the GET request (e.g., authentication token).
Returns:
tuple:
- bool: False if the request failed, True otherwise.
- str or None: The value of the requested attribute if found, None if not found or request failed.
Behavior:
- Sends a GET request to the GitHub Users API for the given `user_id`.
- Prints the value of `term` if it exists in the user's public data.
- Prints a warning if the attribute is not available.
- Prints an error if the request fails.
"""
r = requests.get(f"https://api.github.com/users/{user_id}", headers=headers)
if r.status_code != 200:
print(printcolors.FAIL + f"ERR: Request failed with status code {r.status_code}" + printcolors.ENDC)
return False, None
term_value = r.json().get(term)
if term_value:
print(printcolors.BOLD + f"{term}: " + printcolors.ENDC + term_value)
else:
print(printcolors.WARNING + f"No public {term} found for this user." + printcolors.ENDC)
return True, term_value