|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, Callable, Dict, TypeVar, Union, overload |
| 4 | + |
| 5 | +from httpx import Response |
| 6 | +from pydantic import BaseModel |
| 7 | +from typing_extensions import Literal, Self |
| 8 | + |
| 9 | +from ..helpers import handle_exception |
| 10 | +from ..http_clients import AsyncClient |
| 11 | + |
| 12 | +T = TypeVar("T") |
| 13 | + |
| 14 | + |
| 15 | +class AsyncGoTrueBaseAPI: |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + *, |
| 19 | + url: str, |
| 20 | + headers: Dict[str, str], |
| 21 | + http_client: Union[AsyncClient, None], |
| 22 | + ): |
| 23 | + self._url = url |
| 24 | + self._headers = headers |
| 25 | + self._http_client = http_client or AsyncClient() |
| 26 | + |
| 27 | + async def __aenter__(self) -> Self: |
| 28 | + return self |
| 29 | + |
| 30 | + async def __aexit__(self, exc_t, exc_v, exc_tb) -> None: |
| 31 | + await self.close() |
| 32 | + |
| 33 | + async def close(self) -> None: |
| 34 | + await self._http_client.aclose() |
| 35 | + |
| 36 | + @overload |
| 37 | + async def _request( |
| 38 | + self, |
| 39 | + method: Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"], |
| 40 | + path: str, |
| 41 | + *, |
| 42 | + jwt: Union[str, None] = None, |
| 43 | + redirect_to: Union[str, None] = None, |
| 44 | + headers: Union[Dict[str, str], None] = None, |
| 45 | + query: Union[Dict[str, str], None] = None, |
| 46 | + body: Union[Any, None] = None, |
| 47 | + no_resolve_json: Literal[False] = False, |
| 48 | + xform: Callable[[Any], T], |
| 49 | + ) -> T: |
| 50 | + ... # pragma: no cover |
| 51 | + |
| 52 | + @overload |
| 53 | + async def _request( |
| 54 | + self, |
| 55 | + method: Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"], |
| 56 | + path: str, |
| 57 | + *, |
| 58 | + jwt: Union[str, None] = None, |
| 59 | + redirect_to: Union[str, None] = None, |
| 60 | + headers: Union[Dict[str, str], None] = None, |
| 61 | + query: Union[Dict[str, str], None] = None, |
| 62 | + body: Union[Any, None] = None, |
| 63 | + no_resolve_json: Literal[True], |
| 64 | + xform: Callable[[Response], T], |
| 65 | + ) -> T: |
| 66 | + ... # pragma: no cover |
| 67 | + |
| 68 | + @overload |
| 69 | + async def _request( |
| 70 | + self, |
| 71 | + method: Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"], |
| 72 | + path: str, |
| 73 | + *, |
| 74 | + jwt: Union[str, None] = None, |
| 75 | + redirect_to: Union[str, None] = None, |
| 76 | + headers: Union[Dict[str, str], None] = None, |
| 77 | + query: Union[Dict[str, str], None] = None, |
| 78 | + body: Union[Any, None] = None, |
| 79 | + no_resolve_json: bool = False, |
| 80 | + ) -> None: |
| 81 | + ... # pragma: no cover |
| 82 | + |
| 83 | + async def _request( |
| 84 | + self, |
| 85 | + method: Literal["GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE"], |
| 86 | + path: str, |
| 87 | + *, |
| 88 | + jwt: Union[str, None] = None, |
| 89 | + redirect_to: Union[str, None] = None, |
| 90 | + headers: Union[Dict[str, str], None] = None, |
| 91 | + query: Union[Dict[str, str], None] = None, |
| 92 | + body: Union[Any, None] = None, |
| 93 | + no_resolve_json: bool = False, |
| 94 | + xform: Union[Callable[[Any], T], None] = None, |
| 95 | + ) -> Union[T, None]: |
| 96 | + url = f"{self._url}/{path}" |
| 97 | + headers = {**self._headers, **(headers or {})} |
| 98 | + if "Content-Type" not in headers: |
| 99 | + headers["Content-Type"] = "application/json;charset=UTF-8" |
| 100 | + if jwt: |
| 101 | + headers["Authorization"] = f"Bearer {jwt}" |
| 102 | + query = query or {} |
| 103 | + if redirect_to: |
| 104 | + query["redirect_to"] = redirect_to |
| 105 | + try: |
| 106 | + response = await self._http_client.request( |
| 107 | + method, |
| 108 | + url, |
| 109 | + headers=headers, |
| 110 | + params=query, |
| 111 | + json=body.dict() if isinstance(body, BaseModel) else body, |
| 112 | + ) |
| 113 | + response.raise_for_status() |
| 114 | + result = response if no_resolve_json else response.json() |
| 115 | + if xform: |
| 116 | + return xform(result) |
| 117 | + except Exception as e: |
| 118 | + raise handle_exception(e) |
0 commit comments