1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
from __future__ import annotations
import logging
from typing import Any
import httpx
logger = logging.getLogger(__name__)
class SourceHutClientError(RuntimeError):
"""Raised when a SourceHut GraphQL request fails."""
class SourceHutGraphQLClient:
def __init__(
self,
endpoint: str,
token: str,
*,
timeout: float = 15.0,
max_retries: int = 2,
transport: httpx.BaseTransport | None = None,
) -> None:
self.endpoint = endpoint
self.timeout = timeout
self.max_retries = max_retries
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
self._client = httpx.Client(headers=headers, timeout=timeout, transport=transport)
def execute(self, query: str, variables: dict[str, Any] | None = None) -> dict[str, Any]:
payload = {"query": query, "variables": variables or {}}
attempts = self.max_retries + 1
for attempt in range(1, attempts + 1):
try:
response = self._client.post(self.endpoint, json=payload)
response.raise_for_status()
body = response.json()
except httpx.HTTPStatusError as exc:
response_text = exc.response.text[:500]
logger.warning(
"SourceHut HTTP failure from %s on attempt %s/%s: %s %s",
self.endpoint,
attempt,
attempts,
exc.response.status_code,
response_text,
)
if exc.response.status_code >= 500 and attempt < attempts:
continue
raise SourceHutClientError(
f"HTTP error from SourceHut: {exc.response.status_code} {response_text}".strip()
) from exc
except httpx.HTTPError as exc:
logger.warning(
"SourceHut network failure from %s on attempt %s/%s",
self.endpoint,
attempt,
attempts,
)
if attempt < attempts:
continue
raise SourceHutClientError("Network error while contacting SourceHut") from exc
if "errors" in body:
raise SourceHutClientError(f"GraphQL errors returned by SourceHut: {body['errors']}")
return body.get("data", {})
raise SourceHutClientError("SourceHut request exhausted retries")
def close(self) -> None:
self._client.close()
|