poet-api
Requests with Limiter.
Uses pyrate_limiter.SQLiteBucket with use_file_lock=True.
It has a default RATE_LIMIT_SITES setting and waits for settings.DJANGO_RATE_LIMIT_SITES:
Configuration:
DJANGO_RATE_LIMIT_SITES = {
"payment_gateway": {
"domain_keyword": "adomain.com",
"rates": [Rate(25, Duration.MINUTE)],
"max_wait_seconds": 5, # Custom timeout in seconds
},
"analytics_service": {
"domain_keyword": "anotherdomain.com",
"rates": [Rate(50, Duration.MINUTE)],
# No timeout here: default = -1
},
# The following line, already exists as default setting:
# "default": {"rates": [Rate(59, Duration.MINUTE)], "max_wait_seconds": -1},
}
DJANGO_RATE_LIMIT_SITES.keys()[0]: str-> Unique identity keywordDJANGO_RATE_LIMIT_SITES.values()[0]: dict->domain_keyword: str->urlparse(url).netlock or "default". A new autonomous limiter is based on this keyword.rates: list-> List ofRateobjects for thedomain_keywordmax_wait_seconds: int-> Max seconds the limiter can wait before failing.- -1: Wait until the bucket allows a request, the request never fails.
- positive int: If the bucket needs more than
positive intseconds to allow a request, the request fails.- A mocked
HTTP 429 ("Too Many Requests")response object is returned without making an actual request. requests().raise_for_status()raisesrequests.exceptions.HTTPError.
- A mocked
Example
Bare metal use
import requests
from api.rate_limiter import send_request
try:
# 1. Send request
response = send_request("https://example.com", method="POST", json={"amount": 10})
# 2. Raise for error
# If:
# - max_wait_seconds != -1 (-1 is the default)
# - the bucket cannot afford it
# -> a mocked HTTP 429 ("Too Many Requests") response is returned
response.raise_for_status()
# 3. Continue if everything is OK
data = response.json()
print("Success:", data)
except requests.exceptions.HTTPError as e:
print(f"HTTP Error occurred: {e}")
if e.response and e.response.status_code == 429:
print("Reason: The application queue was too full and exceeded max_wait_seconds!")
except Exception as e:
print(f"An unexpected error occurred: {e}")
```
### `api.api_call.call_them`
This method provides a mechanism to retry up to 10 times with backoff strategy.
```python
from api import api_call
from api.protocols import SyncHttpResponse
response: SyncHttpResponse = api_call.call_them(
url="https://www.example.com",
action="GET",
**kwargs,
)
print(f"HTTP Error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
Release files for poet-api 4.3.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 | |
|---|---|---|---|
| poet_api-4.3.0.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| poet_api-4.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.2 kB
Release files / poet_api-4.3.0.tar.gz
| Download URL | poet_api-4.3.0.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6ddf5f1db87c15c183289fb10a77a90a12e7f388504d43de0dbc8ab420d3e422
|
|
BLAKE2b-256 checksum How to use checksums |
dcadec7a3d4452325db471954387ba7a37757997acaaa64056fbf87b0edf0aec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.13.13 Linux/6.12.107+deb13-amd64
|
Release files / poet_api-4.3.0-py3-none-any.whl
| Download URL | poet_api-4.3.0-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d74e0547f4c27cb8ded018acbae9ed436433a7961180b1724d5d83b4c15cae82
|
|
BLAKE2b-256 checksum How to use checksums |
b80b82e3b42d59c92148ed325e5cf4a9f9e28902ba531e3d738171350cefacdf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.13.13 Linux/6.12.107+deb13-amd64
|