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=""
|
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-11-25 05:13:14 +00:00
|
|
|
import os
|
2021-09-07 11:37:03 +00:00
|
|
|
from typing import Any
|
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", "")
|
|
|
|
|
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
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",
|
|
|
|
}
|
2024-04-21 17:34:18 +00:00
|
|
|
return requests.get(AUTHENTICATED_USER_ENDPOINT, headers=headers, timeout=10).json()
|
2020-11-25 05:13:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
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.")
|