| 1 |
|
| 2 |
|
| 3 |
from typing import Dict, Generic, TypeVar |
| 4 |
|
| 5 |
import httpx |
| 6 |
|
| 7 |
|
| 8 |
T = TypeVar("T") |
| 9 |
|
| 10 |
|
| 11 |
class BaseHttpResponse: |
| 12 |
"""Minimalist HTTP response wrapper that exposes response headers and status code.""" |
| 13 |
|
| 14 |
_response: httpx.Response |
| 15 |
|
| 16 |
def __init__(self, response: httpx.Response): |
| 17 |
self._response = response |
| 18 |
|
| 19 |
@property |
| 20 |
def headers(self) -> Dict[str, str]: |
| 21 |
return dict(self._response.headers) |
| 22 |
|
| 23 |
@property |
| 24 |
def status_code(self) -> int: |
| 25 |
return self._response.status_code |
| 26 |
|
| 27 |
|
| 28 |
class HttpResponse(Generic[T], BaseHttpResponse): |
| 29 |
"""HTTP response wrapper that exposes response headers and data.""" |
| 30 |
|
| 31 |
_data: T |
| 32 |
|
| 33 |
def __init__(self, response: httpx.Response, data: T): |
| 34 |
super().__init__(response) |
| 35 |
self._data = data |
| 36 |
|
| 37 |
@property |
| 38 |
def data(self) -> T: |
| 39 |
return self._data |
| 40 |
|
| 41 |
def close(self) -> None: |
| 42 |
self._response.close() |
| 43 |
|
| 44 |
|
| 45 |
class AsyncHttpResponse(Generic[T], BaseHttpResponse): |
| 46 |
"""HTTP response wrapper that exposes response headers and data.""" |
| 47 |
|
| 48 |
_data: T |
| 49 |
|
| 50 |
def __init__(self, response: httpx.Response, data: T): |
| 51 |
super().__init__(response) |
| 52 |
self._data = data |
| 53 |
|
| 54 |
@property |
| 55 |
def data(self) -> T: |
| 56 |
return self._data |
| 57 |
|
| 58 |
async def close(self) -> None: |
| 59 |
await self._response.aclose() |