A zero-dependency HTTP client for Python with retry, timeout, and rate-limiting
Project description
petchr
A zero-dependency HTTP client for Python with retry, timeout, and rate-limiting built in.
pip install petchr
Requires Python 3.9+
Why petchr?
requests is great but has no built-in retry or rate-limiting. httpx is modern but requires extra packages for resilience. petchr wraps Python's built-in urllib with everything you need — zero dependencies.
| Feature | urllib | requests | petchr |
|---|---|---|---|
| Zero dependencies | ✅ | ❌ | ✅ |
| Auto-retry w/ backoff | ❌ | ❌ | ✅ |
| Timeout | ✅ | ✅ | ✅ |
| Rate limiting | ❌ | ❌ | ✅ |
| JSON body shorthand | ❌ | ✅ | ✅ |
| Query params object | ❌ | ✅ | ✅ |
| Shared instance config | ❌ | ✅ | ✅ |
Quick Start
from petchr import petch
resp = petch("https://api.example.com/users/1")
print(resp.data) # parsed JSON
Instance API
from petchr import Petchr
api = Petchr(
base_url="https://api.example.com",
headers={"Authorization": f"Bearer {token}"},
timeout=10.0,
retry=3,
)
user = api.get("/users/1")
post = api.post("/posts", json={"title": "Hello"})
api.delete("/posts/123")
Retry
resp = petch("https://api.example.com/data",
retry=5,
retry_delay=1.0, # initial delay in seconds
retry_backoff=2.0, # exponential multiplier
retry_max_delay=30.0, # cap
retry_on={429, 503}, # status codes to retry
on_retry=lambda attempt, err, resp: print(f"Retry {attempt}"),
)
Timeout
from petchr import PetchrTimeoutError
try:
resp = petch("https://slow-api.example.com", timeout=5.0)
except PetchrTimeoutError as e:
print(f"Timed out after {e.timeout}s")
Rate Limiting
from petchr import Petchr, RateLimiter
api = Petchr(
base_url="https://api.example.com",
rate_limiter=RateLimiter(max_requests=10, window_seconds=1.0),
)
Error Handling
from petchr import petch, PetchrError, PetchrTimeoutError, PetchrRateLimitError
try:
resp = petch("https://api.example.com/users/1")
except PetchrTimeoutError as e:
print(f"Timed out after {e.timeout}s")
except PetchrRateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after}s")
except PetchrError as e:
print(f"HTTP {e.status_code}: {e}")
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 petchr-1.0.0.tar.gz.
File metadata
- Download URL: petchr-1.0.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf0050d93d88e4d5b14e472a133c0503b0e75b5b7ef7ae344d90b6cdb084433f
|
|
| MD5 |
282ad0fc685d6274f34d4367a262d39f
|
|
| BLAKE2b-256 |
831292cc655363d611a380fae94ff0ec1c63097390eb17b1f31a5a94f076b50c
|
File details
Details for the file petchr-1.0.0-py3-none-any.whl.
File metadata
- Download URL: petchr-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
379430a70ef0e35e023ab6fd320cc841c57e66aa51d80ba134acd1e50f92fa59
|
|
| MD5 |
556afe1eba48de6b71d90f4654b9764e
|
|
| BLAKE2b-256 |
27309d84ead67190a101097cc3b52f51c84fda99bcb49b246fb2e53a517f8ff0
|