RESTful Mixin¶
restful ¶
Classes¶
BearerAuth ¶
Bases: Auth
HTTP Bearer token authentication handler for httpx.
Implements the httpx.Auth interface to automatically add Bearer token authentication to requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token | str | The bearer token to use for authentication. | required |
Functions¶
auth_flow ¶
Add Bearer token to request Authorization header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request | Request | The HTTP request to authenticate. | required |
Yields:
| Type | Description |
|---|---|
Request | The authenticated request with Authorization header. |
Source code in audex/lib/restful.py
RESTfulMixin ¶
RESTfulMixin(base_url: str, *, auth: Auth | None = None, proxy: str | URL | None = None, timeout: float = 10.0, http_client: AsyncClient | None = None, default_headers: dict[str, str] | None = None, default_params: dict[str, Any] | None = None)
Bases: AsyncContextMixin
Abstract base class for RESTful API clients with async support.
Provides a high-level interface for making HTTP requests to RESTful APIs with automatic retry logic, response validation using Pydantic models, and built-in error handling.
Features: - Automatic retry with configurable attempts and wait time - Response validation and parsing using Pydantic models - Support for Bearer token and custom authentication - Proxy support - Configurable timeouts and default headers/params - Async context manager support
Attributes:
| Name | Type | Description |
|---|---|---|
url | The base URL for the API. | |
http_client | The underlying httpx AsyncClient instance. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url | str | Base URL for all API requests. | required |
auth | Auth | None | Optional httpx authentication handler. | None |
proxy | str | URL | None | Optional proxy URL. | None |
timeout | float | Request timeout in seconds. Defaults to 10.0. | 10.0 |
http_client | AsyncClient | None | Optional pre-configured httpx.AsyncClient. If not provided, a new client will be created. | None |
default_headers | dict[str, str] | None | Optional default headers for all requests. | None |
default_params | dict[str, Any] | None | Optional default query parameters for all requests. | None |
Example
class MyAPIResponse(pyd.BaseModel):
id: str
name: str
class MyAPI(RESTful):
async def get_item(self, item_id: str) -> MyAPIResponse:
return await self.request(
f"/items/{item_id}",
method="GET",
cast_to=MyAPIResponse,
)
# Usage
async with MyAPI(
"https://api.example.com", auth=BearerAuth("token")
) as api:
item = await api.get_item("123")
print(item.name)
Source code in audex/lib/restful.py
Functions¶
request async ¶
request(endpoint: str, *, method: Literal['GET', 'POST', 'PUT', 'DELETE'] = 'GET', headers: Mapping[str, str] | None = None, params: Mapping[str, Any] | None = None, json: Mapping[str, Any] | None = None, cast_to: type[RespT], validate: bool = True, strict: bool = True, raise_for_status: bool = True, max_retries: int = 3, retry_wait: float = 2.0) -> RespT
Make an HTTP request with automatic retry and response validation.
Sends an HTTP request to the specified endpoint and parses the response into the specified Pydantic model. Automatically retries on failure and validates the response data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint | str | API endpoint path (relative to base_url). | required |
method | Literal['GET', 'POST', 'PUT', 'DELETE'] | HTTP method to use. Defaults to "GET". | 'GET' |
headers | Mapping[str, str] | None | Optional request headers to merge with default headers. | None |
params | Mapping[str, Any] | None | Optional query parameters to merge with default params. | None |
json | Mapping[str, Any] | None | Optional JSON body for POST/PUT requests. | None |
cast_to | type[RespT] | Pydantic model class to parse the response into. | required |
validate | bool | Whether to validate response data. Defaults to True. | True |
strict | bool | Whether to use strict validation (reject extra fields). Defaults to True. | True |
raise_for_status | bool | Whether to raise an exception for HTTP error status codes. Defaults to True. | True |
max_retries | int | Maximum number of retry attempts. Defaults to 3. | 3 |
retry_wait | float | Wait time in seconds between retries. Defaults to 2.0. | 2.0 |
Returns:
| Type | Description |
|---|---|
RespT | Parsed response as an instance of cast_to model. |
Raises:
| Type | Description |
|---|---|
HTTPStatusError | If the response status indicates an error after all retries. |
ValidationError | If response validation fails. |
RetryError | If all retry attempts are exhausted. |
Example
class UserResponse(pyd.BaseModel):
user_id: str
username: str
email: str
# GET request
user = await api.request(
"/users/123", method="GET", cast_to=UserResponse
)
# POST request with retry
new_user = await api.request(
"/users",
method="POST",
json={"username": "john", "email": "john@example.com"},
cast_to=UserResponse,
max_retries=5,
retry_wait=3.0,
)
Source code in audex/lib/restful.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
close async ¶
Close the HTTP client and cleanup resources.
Should be called when the API client is no longer needed to properly close all connections. Automatically called when using as a context manager.
Example
Source code in audex/lib/restful.py
BaseModel ¶
Bases: BaseModel
Base Pydantic model with common configuration.
Sets common configurations for all derived models, such as allowing arbitrary types and enabling ORM mode.
options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true