mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 21:41:08 +00:00
bc8df6de31
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2) - [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Created by sarathkaul on 14/11/19
|
|
Updated by lawric1 on 24/11/20
|
|
|
|
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.
|
|
|
|
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=""
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Any
|
|
|
|
import requests
|
|
|
|
BASE_URL = "https://api.github.com"
|
|
|
|
# https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user
|
|
AUTHENTICATED_USER_ENDPOINT = BASE_URL + "/user"
|
|
|
|
# https://github.com/settings/tokens
|
|
USER_TOKEN = os.environ.get("USER_TOKEN", "")
|
|
|
|
|
|
def fetch_github_info(auth_token: str) -> dict[Any, Any]:
|
|
"""
|
|
Fetch GitHub info of a user using the requests module
|
|
"""
|
|
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.")
|