fork of hey-api/openapi-ts because I need some additional things
at feat/use-mutation-hooks 62 lines 1.8 kB view raw
1from typing import Optional 2import httpx 3 4 5class BaseClient: 6 """Base HTTP client using httpx that SDK classes extend.""" 7 8 def __init__(self, client: Optional[httpx.Client] = None, base_url: Optional[str] = None, **kwargs): 9 if client is not None: 10 self._client = client 11 else: 12 self._client = httpx.Client(base_url=base_url or "", **kwargs) 13 14 @property 15 def client(self) -> httpx.Client: 16 """Get the httpx client instance.""" 17 return self._client 18 19 def request(self, method: str, url: str, **kwargs) -> httpx.Response: 20 """Make an HTTP request.""" 21 return self._client.request(method, url, **kwargs) 22 23 def get(self, url: str, **kwargs) -> httpx.Response: 24 """Make a GET request.""" 25 return self._client.get(url, **kwargs) 26 27 def post(self, url: str, **kwargs) -> httpx.Response: 28 """Make a POST request.""" 29 return self._client.post(url, **kwargs) 30 31 def put(self, url: str, **kwargs) -> httpx.Response: 32 """Make a PUT request.""" 33 return self._client.put(url, **kwargs) 34 35 def patch(self, url: str, **kwargs) -> httpx.Response: 36 """Make a PATCH request.""" 37 return self._client.patch(url, **kwargs) 38 39 def delete(self, url: str, **kwargs) -> httpx.Response: 40 """Make a DELETE request.""" 41 return self._client.delete(url, **kwargs) 42 43 def close(self): 44 """Close the client.""" 45 self._client.close() 46 47 def __enter__(self): 48 return self 49 50 def __exit__(self, *args): 51 self.close() 52 53 54class Client(BaseClient): 55 """HTTP client using httpx (alias for BaseClient).""" 56 57 pass 58 59 60def create_client(base_url: Optional[str] = None, **kwargs) -> Client: 61 """Create a new HTTP client instance.""" 62 return Client(base_url=base_url, **kwargs)