import httpx import logging from typing import Any logger: logging.Logger = logging.getLogger("gitea.client") from .config import GITEA_URL, GITEA_TOKEN, GITEA_ORG_FILTER from .files_client import FilesClient from .issues_client import IssuesClient from .notifications_client import NotificationsClient from .prs_client import PullRequestsClient from .repos_client import ReposClient class GiteaClient: """HTTP client for Gitea API v1. This is a facade class that provides access to focused sub-clients for different API domains: - repos: Repository operations (ReposClient) - issues: Issue operations (IssuesClient) - prs: Pull request operations (PullRequestsClient) - files: File and git ref operations (FilesClient) - notifications: Notification operations (NotificationsClient) """ def __init__(self) -> None: self.base_url: str = GITEA_URL.rstrip("/") self.headers: dict[str, str] = { "Authorization": f"token {GITEA_TOKEN}", "Accept": "application/json", } self.client: httpx.Client = httpx.Client(headers=self.headers) self.repos: ReposClient = ReposClient( self.base_url, self.client, GITEA_ORG_FILTER ) self.issues: IssuesClient = IssuesClient( self.base_url, self.client, get_user=self.repos.get_authenticated_user, get_repos=self.repos.list_all_user_repos, ) self.prs: PullRequestsClient = PullRequestsClient( self.base_url, self.client, get_user=self.repos.get_authenticated_user, get_repos=self.repos.list_all_user_repos, ) self.files: FilesClient = FilesClient(self.base_url, self.client) self.notifications: NotificationsClient = NotificationsClient( self.base_url, self.client, GITEA_ORG_FILTER ) def close(self) -> None: """Close the underlying HTTP client.""" self.client.close() def __enter__(self) -> "GiteaClient": return self def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: self.close() def __del__(self) -> None: try: self.client.close() except Exception: pass