Async REST API adapter base class with retry, rate-limit handling, and attribution injection
Project description
async-rest-adapter
A lightweight async REST API adapter base class built on httpx.
Provides a reusable BaseAdapter ABC that handles:
- Async HTTP client lifecycle (httpx.AsyncClient via context manager)
- Exponential-backoff retry on
httpx.RequestError - Rate-limit handling (HTTP 429 → sleep → retry)
- Typed error taxonomy (12 status-code →
APIErrormappings) - Attribution injection into every successful response
- Structured
APIResponse/APIErrormodels (Pydantic v2)
Installation
pip install async-rest-adapter
Quick start
from async_rest_adapter import BaseAdapter, APIResponse, Attribution
class MyAdapter(BaseAdapter):
ATTRIBUTION = Attribution(
license="CC-BY-4.0",
text="My Data Source",
url="https://example.com",
)
async def health_check(self) -> APIResponse:
try:
data = await self._make_request("GET", "/health")
return self._wrap_response(True, data)
except Exception as e:
return self._wrap_response(False, error=str(e), error_type="health_check_failed")
async with MyAdapter("my-api", "https://api.example.com") as adapter:
result = await adapter.health_check()
print(result.success, result.data)
API
BaseAdapter(provider_name, base_url, timeout=30.0, max_retries=3)
Subclass and implement health_check() -> APIResponse.
Key methods:
_make_request(method, endpoint, **kwargs)— HTTP request with retry, returns parsed body_wrap_response(success, data, error, error_type, metadata)— buildsAPIResponse, auto-injectsATTRIBUTION_handle_rate_limit(response)— sleep on 429, return True to retry_handle_errors(response)— map 4xx/5xx toAPIError
Test injection:
adapter.session = mock_client # inject mock httpx client in tests
APIResponse
class APIResponse(BaseModel):
success: bool
data: Any | None
error: str | None
error_type: str | None
metadata: dict # includes "attribution" on every successful response
APIError(Exception)
raise APIError("Not found", status_code=404, error_type="not_found_error")
Attribution
Attribution(license="CC-BY-4.0", text="Source Name", url="https://example.com")
Requirements
- Python 3.11+
- httpx ≥ 0.27
- pydantic ≥ 2.0
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file async_rest_adapter-0.1.0.tar.gz.
File metadata
- Download URL: async_rest_adapter-0.1.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25e1bc86d8fae68d4b4335348be1c46ef802e5b9a3267177f697478cb6b63c20
|
|
| MD5 |
a2034de23430fc4022705c76932bddad
|
|
| BLAKE2b-256 |
2b2a8c122011df09c7f0ef78f38f0df1016e88a6d03336ed1f108c5c75d44996
|
File details
Details for the file async_rest_adapter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: async_rest_adapter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a71d2630d1ad70209a5d1bbe22a0b3785abe8d4a5994ce5d48da691063228c09
|
|
| MD5 |
b28c607110d0f528e896de6b32e5b0cb
|
|
| BLAKE2b-256 |
2087162a7aea15ff108932a91dc0b0d98134da21e5e3633379b3a2df9ab75aac
|