|
| 1 | +from importlib.metadata import version |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import httpx |
| 5 | + |
| 6 | +from s2_sdk._exceptions import AppendConditionFailed, S2ApiError |
| 7 | + |
| 8 | +_VERSION = version("s2-sdk") |
| 9 | +_USER_AGENT = f"s2-sdk-python/{_VERSION}" |
| 10 | + |
| 11 | + |
| 12 | +class HttpClient: |
| 13 | + __slots__ = ("_client",) |
| 14 | + |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + base_url: str, |
| 18 | + access_token: str, |
| 19 | + timeout: float, |
| 20 | + http2: bool = True, |
| 21 | + ) -> None: |
| 22 | + self._client = httpx.AsyncClient( |
| 23 | + base_url=base_url, |
| 24 | + headers={ |
| 25 | + "authorization": f"Bearer {access_token}", |
| 26 | + "user-agent": _USER_AGENT, |
| 27 | + }, |
| 28 | + timeout=timeout, |
| 29 | + http2=http2, |
| 30 | + ) |
| 31 | + |
| 32 | + async def request( |
| 33 | + self, |
| 34 | + method: str, |
| 35 | + path: str, |
| 36 | + *, |
| 37 | + json: Any = None, |
| 38 | + params: dict[str, Any] | None = None, |
| 39 | + headers: dict[str, str] | None = None, |
| 40 | + content: bytes | None = None, |
| 41 | + ) -> httpx.Response: |
| 42 | + response = await self._client.request( |
| 43 | + method, |
| 44 | + path, |
| 45 | + json=json, |
| 46 | + params=params, |
| 47 | + headers=headers, |
| 48 | + content=content, |
| 49 | + ) |
| 50 | + _raise_for_status(response) |
| 51 | + return response |
| 52 | + |
| 53 | + def stream( |
| 54 | + self, |
| 55 | + method: str, |
| 56 | + path: str, |
| 57 | + *, |
| 58 | + params: dict[str, Any] | None = None, |
| 59 | + headers: dict[str, str] | None = None, |
| 60 | + ): |
| 61 | + return self._client.stream( |
| 62 | + method, |
| 63 | + path, |
| 64 | + params=params, |
| 65 | + headers=headers, |
| 66 | + ) |
| 67 | + |
| 68 | + async def close(self) -> None: |
| 69 | + await self._client.aclose() |
| 70 | + |
| 71 | + |
| 72 | +def _raise_for_status(response: httpx.Response) -> None: |
| 73 | + status = response.status_code |
| 74 | + if 200 <= status < 300: |
| 75 | + return |
| 76 | + |
| 77 | + if status == 412: |
| 78 | + body = response.json() |
| 79 | + if "fencing_token_mismatch" in body: |
| 80 | + raise AppendConditionFailed( |
| 81 | + f"Fencing token mismatch: {body['fencing_token_mismatch']}", |
| 82 | + status_code=status, |
| 83 | + ) |
| 84 | + elif "seq_num_mismatch" in body: |
| 85 | + raise AppendConditionFailed( |
| 86 | + f"Sequence number mismatch: {body['seq_num_mismatch']}", |
| 87 | + status_code=status, |
| 88 | + ) |
| 89 | + raise AppendConditionFailed(str(body), status_code=status) |
| 90 | + |
| 91 | + if status == 416: |
| 92 | + # Tail response — not an error, handled by callers |
| 93 | + return |
| 94 | + |
| 95 | + try: |
| 96 | + body = response.json() |
| 97 | + message = body.get("message", response.text) |
| 98 | + code = body.get("code") |
| 99 | + except Exception: |
| 100 | + message = response.text |
| 101 | + code = None |
| 102 | + |
| 103 | + raise S2ApiError(message, status_code=status, code=code) |
0 commit comments