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
Metadata
Release files for async-rest-adapter 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| async_rest_adapter-0.1.0.tar.gz | 7.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| async_rest_adapter-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.8 kB
Release files / async_rest_adapter-0.1.0.tar.gz
| Download URL | async_rest_adapter-0.1.0.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
25e1bc86d8fae68d4b4335348be1c46ef802e5b9a3267177f697478cb6b63c20
|
|
BLAKE2b-256 checksum How to use checksums |
2b2a8c122011df09c7f0ef78f38f0df1016e88a6d03336ed1f108c5c75d44996
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Release files / async_rest_adapter-0.1.0-py3-none-any.whl
| Download URL | async_rest_adapter-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a71d2630d1ad70209a5d1bbe22a0b3785abe8d4a5994ce5d48da691063228c09
|
|
BLAKE2b-256 checksum How to use checksums |
2087162a7aea15ff108932a91dc0b0d98134da21e5e3633379b3a2df9ab75aac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Security for async-rest-adapter 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.