e91780169e
- Create gitea/issues_client.py with IssuesClient class (9 methods) - Create gitea/prs_client.py with PullRequestsClient class (17 methods) - Create gitea/files_client.py with FilesClient class (4 methods) - Create gitea/notifications_client.py with NotificationsClient class (2 methods) - Create gitea/repos_client.py with ReposClient class (2 methods) - Create gitea/__init__.py to export all client classes - Remove delegation methods from GiteaClient (now ~70 lines) - Update all callers to use sub-clients (client.issues, client.prs, etc.) - Update test files to mock sub-client attributes GiteaClient is now a facade that provides access to focused sub-clients: - repos: Repository operations (ReposClient) - issues: Issue operations (IssuesClient) - prs: Pull request operations (PullRequestsClient) - files: File and git ref operations (FilesClient) - notifications: Notification operations (NotificationsClient) Refs: #godclass-refactor
69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
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
|