All checks were successful
Build And Publish Package / publish (push) Successful in 54s
122 lines
2.4 KiB
Markdown
122 lines
2.4 KiB
Markdown
# AioHTTPX
|
|
|
|
**AioHTTPX** is an internal async HTTP client built on top of **httpx.AsyncClient**, using **aiohttp** as the transport layer for improved async performance.
|
|
It also adds **Redis-backed rate limiting and response caching** at the transport level.
|
|
|
|
---
|
|
|
|
## What it provides
|
|
|
|
* `AioHTTPXClient` — a drop-in replacement for `httpx.AsyncClient`
|
|
* `aiohttp`-based async transport
|
|
* Optional **rate limiting** (Redis)
|
|
* Optional **response caching** (Redis, TTL via headers)
|
|
|
|
---
|
|
|
|
## Installation (internal)
|
|
|
|
Configure pip with your private index:
|
|
|
|
```ini
|
|
[global]
|
|
index-url = https://git.miwory.dev/api/packages/Miwory/pypi/simple
|
|
username = <USERNAME>
|
|
password = <PASSWORD>
|
|
```
|
|
|
|
Then install:
|
|
|
|
```bash
|
|
pip install aiohttpx
|
|
```
|
|
|
|
---
|
|
|
|
## Usage
|
|
|
|
### Basic client (no Redis)
|
|
|
|
```python
|
|
from aiohttpx import AioHTTPXClient
|
|
|
|
async with AioHTTPXClient() as client:
|
|
r = await client.get("https://example.com")
|
|
print(r.text)
|
|
```
|
|
|
|
This uses:
|
|
|
|
* `aiohttp.ClientSession` under the hood
|
|
* httpx-compatible request / response API
|
|
|
|
---
|
|
|
|
## Rate limiting
|
|
|
|
Rate limiting is **optional** and requires Redis.
|
|
|
|
```python
|
|
client = AioHTTPXClient(
|
|
redis_url="redis://localhost:6379",
|
|
key="my-rate-limit-key",
|
|
limit=60, # requests per minute
|
|
)
|
|
```
|
|
|
|
How it works:
|
|
|
|
* Token-based limiting using Redis time
|
|
* Requests exceeding the limit are delayed and retried
|
|
* Implemented in `AsyncRateLimit`
|
|
|
|
---
|
|
|
|
## Response caching
|
|
|
|
Caching is also **optional** and Redis-backed.
|
|
|
|
```python
|
|
client = AioHTTPXClient(
|
|
redis_url="redis://localhost:6379",
|
|
)
|
|
```
|
|
|
|
To enable caching for a request, set a TTL header:
|
|
|
|
```python
|
|
await client.get(
|
|
"https://example.com/data",
|
|
headers={"X-Cache-TTL": "30"},
|
|
)
|
|
```
|
|
|
|
Notes:
|
|
|
|
* Cache key = method + URL
|
|
* TTL is read from `X-Cache-TTL` header
|
|
* Cached responses are fully reconstructed `Response` objects
|
|
|
|
---
|
|
|
|
## Configuration parameters
|
|
|
|
`AioHTTPXClient(...)` supports all standard `httpx.AsyncClient` arguments **plus**:
|
|
|
|
| Parameter | Type | Description |
|
|
| ----------- | ------------- | -------------------- |
|
|
| `redis_url` | `str \| None` | Redis connection URL |
|
|
| `key` | `str \| None` | Rate limit Redis key |
|
|
| `limit` | `int \| None` | Requests per minute |
|
|
| `logger` | `str \| None` | Logger name |
|
|
|
|
---
|
|
|
|
## Notes & limitations
|
|
|
|
* ❌ Sync client is not supported
|
|
* ❌ HTTP/2 disabled (`http1=True`, `http2=False`)
|
|
* ✅ Automatic retry on network/connect errors
|
|
|
|
---
|