Spaces:
Running
Running
Download github_client.py from Sdm0/opencodereview: direct link, hf CLI and curl.
- Browser
- Download file 6.5 kB
-
https://huggingface.co/spaces/Sdm0/opencodereview/resolve/4d26ba1bb4e0fa987eecbcdc8d72cd8f299bac28/github_client.py
- Command line
-
hf download hf://spaces/Sdm0/opencodereview@4d26ba1bb4e0fa987eecbcdc8d72cd8f299bac28/github_client.py
-
curl -L -o github_client.py https://huggingface.co/spaces/Sdm0/opencodereview/resolve/4d26ba1bb4e0fa987eecbcdc8d72cd8f299bac28/github_client.py
6.5 kB
| #!/usr/bin/env python3 | |
| """Shared GitHub API client with automatic token resolution and 401 resilience. | |
| Caching | |
| ------- | |
| All HTTP requests made through ``requests`` within this process are cached | |
| to a local SQLite backend with a 15-minute TTL, so re-reviewing the same PR | |
| does not re-fetch data from GitHub. | |
| Usage | |
| ----- | |
| from github_client import GitHubSession, resolve_token, guard_401 | |
| # Preferred: use the session for all API calls | |
| session = GitHubSession() | |
| resp = session.get("https://api.github.com/repos/psf/requests") | |
| # Decorator for standalone functions | |
| @guard_401 | |
| def my_api_call(): | |
| ... | |
| """ | |
| from __future__ import annotations | |
| import functools | |
| import logging | |
| import os | |
| from typing import Optional | |
| import keyring | |
| import requests | |
| logger = logging.getLogger(__name__) | |
| SERVICE_NAME = "opencodereview" | |
| # βββ Global HTTP cache βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Cache all requests (GitHub API + raw content) to a local SQLite store | |
| # so re-reviewing the same PR avoids redundant network fetches. | |
| _CACHE_BACKEND = None | |
| def _init_cache() -> None: | |
| """Install ``requests-cache`` with a 15-minute TTL. | |
| Patches ``requests.Session`` globally, so :class:`GitHubSession` | |
| (which inherits from it) automatically gets caching. Safe to call | |
| multiple times β subsequent calls are no-ops. | |
| """ | |
| global _CACHE_BACKEND | |
| if _CACHE_BACKEND is not None: | |
| return | |
| try: | |
| import requests_cache | |
| cache_dir = os.environ.get( | |
| "OPENCODEREVIEW_CACHE_DIR", | |
| os.path.join(os.getcwd(), ".opencodereview"), | |
| ) | |
| os.makedirs(cache_dir, exist_ok=True) | |
| db_path = os.path.join(cache_dir, "http_cache.sqlite") | |
| _CACHE_BACKEND = requests_cache.install_cache( | |
| cache_name=db_path, | |
| backend="sqlite", | |
| expire_after=900, # 15 minutes | |
| allowable_codes=(200, 404), | |
| ) | |
| logger.debug("HTTP cache initialised at %s", db_path) | |
| except ImportError: | |
| logger.debug("requests-cache not installed β skipping HTTP cache") | |
| except Exception as exc: | |
| logger.warning("Failed to initialise HTTP cache: %s", exc) | |
| # βββ Exceptions ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class TokenRevokedError(Exception): | |
| """Raised when a GitHub API call returns 401 (token invalid or revoked).""" | |
| # βββ Token resolution βββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def resolve_token() -> Optional[str]: | |
| """Return a GitHub token from: | |
| 1. The system keyring (set via ``opencodereview auth login``) | |
| 2. The ``GITHUB_TOKEN`` environment variable | |
| Returns ``None`` if neither source has a token, so callers can fall back | |
| gracefully (e.g., dry-run mode in the executor). | |
| """ | |
| # Try keyring first (auth login stores here) | |
| try: | |
| token = keyring.get_password(SERVICE_NAME, "github_token") | |
| if token: | |
| return token | |
| except Exception: | |
| pass # keyring back-end unavailable (headless CI, etc.) | |
| # Fall back to env var | |
| token = os.environ.get("GITHUB_TOKEN") | |
| return token.strip() if token else None | |
| # βββ 401 decorator ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def guard_401(func): | |
| """Decorator: catch 401 responses from GitHub API calls and raise | |
| :class:`TokenRevokedError` with a helpful message. | |
| Use this on any standalone function that makes GitHub API calls and | |
| calls ``raise_for_status()``. | |
| For functions that use :class:`GitHubSession` the 401 handling happens | |
| automatically β no decorator needed. | |
| """ | |
| def wrapper(*args, **kwargs): | |
| try: | |
| return func(*args, **kwargs) | |
| except requests.HTTPError as exc: | |
| if exc.response is not None and exc.response.status_code == 401: | |
| raise TokenRevokedError( | |
| "Your GitHub token appears to be invalid or has been " | |
| "revoked.\n\n" | |
| " Re-authenticate:\n" | |
| " opencodereview auth login\n\n" | |
| " Or update the GITHUB_TOKEN environment variable.\n" | |
| ) from exc | |
| raise | |
| return wrapper | |
| # βββ Custom Session βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class GitHubSession(requests.Session): | |
| """A :class:`requests.Session` subclass that: | |
| * Auto-resolves the GitHub token from keyring then env var | |
| * Sends ``Authorization`` and standard headers on every request | |
| * Raises :class:`TokenRevokedError` on ``401`` responses from | |
| ``api.github.com`` | |
| Usage | |
| ----- | |
| session = GitHubSession() | |
| resp = session.get("https://api.github.com/repos/psf/requests") | |
| """ | |
| def __init__(self, token: Optional[str] = None) -> None: | |
| super().__init__() | |
| self.token = token or resolve_token() | |
| self.headers.update({ | |
| "Accept": "application/vnd.github.v3+json", | |
| "User-Agent": "OpenCodeReview/1.0", | |
| }) | |
| if self.token: | |
| self.headers["Authorization"] = f"Bearer {self.token}" | |
| def request(self, method, url, *args, **kwargs): | |
| """Override :meth:`requests.Session.request` to intercept 401 | |
| responses from ``api.github.com``.""" | |
| resp = super().request(method, url, *args, **kwargs) | |
| # Only intercept 401 from the GitHub API, not raw content or other hosts | |
| if resp.status_code == 401 and "api.github.com" in url: | |
| raise TokenRevokedError( | |
| "Your GitHub token appears to be invalid or has been " | |
| "revoked.\n\n" | |
| " Re-authenticate:\n" | |
| " opencodereview auth login\n\n" | |
| " Or update the GITHUB_TOKEN environment variable.\n" | |
| ) | |
| return resp | |