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
try:
response: ApiResponse = api_call.call_them(
url="https://www.example.com",
action="GET",
**kwargs,
)
return response
except api_call.RETRIABLE_ERRORS as e:
print(f"HTTP Error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
poet_api-4.1.0.tar.gz
(5.6 kB
view details)
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 poet_api-4.1.0.tar.gz.
File metadata
- Download URL: poet_api-4.1.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
poetry/2.4.1 CPython/3.13.13 Linux/6.12.96+deb13-amd64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2134447f72253e19e05b3f5eae2d01911d180c2262ac2677b7ec209d2c159f43
|
|
| MD5 |
0899e58e3c64a20c437decafc71e8849
|
|
| BLAKE2b-256 |
5efc09c3064b5e45785728b4bdb1a8edb5fc46bbe0543a408e8c21264192ec34
|
File details
Details for the file poet_api-4.1.0-py3-none-any.whl.
File metadata
- Download URL: poet_api-4.1.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
poetry/2.4.1 CPython/3.13.13 Linux/6.12.96+deb13-amd64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed9ea80562a5eed01558308b19f87429b93905e469d8812f99762dc8d682c324
|
|
| MD5 |
d1a52ddaa54cc1d706b2e34d890983a8
|
|
| BLAKE2b-256 |
eba6d91118a19356494bf1293dadcd7ac0271c6eac903b93d6376664811b7e8d
|