2020-06-11 14:38:43 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Created by sarathkaul on 14/11/19
|
2020-11-25 05:13:14 +00:00
|
|
|
Updated by lawric1 on 24/11/20
|
2020-06-11 14:38:43 +00:00
|
|
|
|
2020-11-25 05:13:14 +00:00
|
|
|
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.
|
2020-06-11 14:38:43 +00:00
|
|
|
|
2020-11-25 05:13:14 +00:00
|
|
|
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=""
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
from typing import Any, Dict
|
2019-11-14 10:22:08 +00:00
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2020-11-25 05:13:14 +00:00
|
|
|
BASE_URL = "https://api.github.com"
|
2019-11-14 10:22:08 +00:00
|
|
|
|
2020-11-25 05:13:14 +00:00
|
|
|
# https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user
|
|
|
|
AUTHENTICATED_USER_ENDPOINT = BASE_URL + "/user"
|
2019-11-14 10:22:08 +00:00
|
|
|
|
2020-11-25 05:13:14 +00:00
|
|
|
# https://github.com/settings/tokens
|
|
|
|
USER_TOKEN = os.environ.get("USER_TOKEN", "")
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_github_info(auth_token: str) -> Dict[Any, Any]:
|
2020-06-11 14:38:43 +00:00
|
|
|
"""
|
|
|
|
Fetch GitHub info of a user using the requests module
|
|
|
|
"""
|
2020-11-25 05:13:14 +00:00
|
|
|
headers = {
|
|
|
|
"Authorization": f"token {auth_token}",
|
|
|
|
"Accept": "application/vnd.github.v3+json",
|
|
|
|
}
|
|
|
|
return requests.get(AUTHENTICATED_USER_ENDPOINT, headers=headers).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.")
|