Skip to main content

Drop-in requests wrapper with automatic retry, response caching, and rate limiting

This project has been quarantined.

PyPI Admins need to review this project before it can be restored. While in quarantine, the project is not installable by clients, and cannot be being modified by its maintainers.

Read more in the project in quarantine help article.

Project description

requests-enhancer

Python License: MIT PyPI version PyPI downloads Tests

A drop-in enhancement layer for the popular requests library. Adds automatic retry with exponential backoff, thread-safe LRU response caching, configurable rate limiting, and pagination helpers — with zero changes to your existing requests code.


Why requests-enhancer?

Writing robust HTTP client code means handling transient failures, avoiding hammering rate-limited APIs, caching expensive responses, and navigating paginated endpoints. requests-enhancer packages all of this into a single SmartSession class that works exactly like requests.Session.


Installation

pip install requests-enhancer

Quick Start

from requests_enhancer import SmartSession

# 3 retries, 60-second response cache, max 10 requests/second
session = SmartSession(retries=3, cache_ttl=60, rate_limit=10)
resp = session.get("https://api.example.com/users")
print(resp.json())

Features

Feature Description
Automatic retry Configurable attempts, exponential backoff, and status codes to retry
Response caching Thread-safe LRU cache with per-entry TTL and explicit bypass
Rate limiting Per-instance calls-per-second ceiling using a sliding-window limiter
Context manager with SmartSession() as s: works out of the box
Pagination helper paginate() generator for page- and cursor-based REST APIs
URL utilities build_url, strip_auth, safe_json, is_json

API Reference

SmartSession

SmartSession(
    retries=3,                                    # max retry attempts
    backoff=0.5,                                  # exponential backoff factor (seconds)
    status_forcelist=(429, 500, 502, 503, 504),   # HTTP status codes that trigger retry
    cache_ttl=None,                               # GET cache TTL in seconds (None = disabled)
    cache_maxsize=256,                            # max entries in the LRU cache
    rate_limit=None,                              # max requests per second (None = disabled)
    timeout=(5, 30),                              # (connect_timeout, read_timeout) in seconds
)

All standard requests.Session methods are available: .get(), .post(), .put(), .delete(), .request().

Additional keyword argument on .request():

  • use_cache=True — set to False to bypass the response cache for a single request.

Examples

from requests_enhancer import SmartSession

# Retry on server errors and rate-limit responses
session = SmartSession(retries=5, backoff=1.0, status_forcelist=(429, 500, 503))
resp = session.get("https://api.example.com/data")

# Cache responses for 5 minutes
session = SmartSession(cache_ttl=300, cache_maxsize=512)
resp1 = session.get("https://api.example.com/config")   # hits network
resp2 = session.get("https://api.example.com/config")   # served from cache

# Force a fresh request regardless of cache
fresh = session.get("https://api.example.com/config", use_cache=False)

# Rate-limited scraping — at most 2 requests/second
session = SmartSession(rate_limit=2.0, retries=3)
for url in urls:
    session.get(url)

# Context manager — session closed automatically
with SmartSession(retries=3, cache_ttl=60) as s:
    users = s.get("https://api.example.com/users").json()
    posts = s.get("https://api.example.com/posts").json()

paginate(session, url, ...)

Generator that yields requests.Response objects across paginated API endpoints.

paginate(
    session,                    # SmartSession or requests.Session instance
    url,                        # endpoint URL
    params=None,                # base query parameters
    page_param="page",          # name of the page number parameter
    per_page=100,               # items per page
    per_page_param="per_page",  # name of the page-size parameter
    max_pages=50,               # safety limit on total pages fetched
)
from requests_enhancer import SmartSession, paginate

session = SmartSession(retries=3)

all_items = []
for resp in paginate(session, "https://api.example.com/items", per_page=50):
    all_items.extend(resp.json())

print(f"Fetched {len(all_items)} items")

Pagination stops automatically when an empty page is returned, a non-OK status is received, or max_pages is reached.


URL and response utilities

from requests_enhancer import build_url, strip_auth, safe_json, is_json

# Build a URL with a path and query parameters
build_url("https://api.example.com", "/v2/users", {"page": 1, "limit": 50})
# → "https://api.example.com/v2/users?limit=50&page=1"

# Strip credentials before logging
strip_auth("https://alice:secret@api.example.com/data")
# → "https://api.example.com/data"

# Parse JSON safely — never raises
data = safe_json(response, default={})

# Check content type
if is_json(response):
    data = response.json()

Comparison with plain requests

Feature requests requests-enhancer
Automatic retry Manual HTTPAdapter setup ✅ One parameter
Response caching ✅ Thread-safe LRU with TTL
Rate limiting ✅ Per-instance sliding window
Pagination paginate() generator
Context manager ✅ Inherited
Drop-in compatible ✅ Same method signatures

Development

git clone https://github.com/Hexa-devy/requests-enhancer.git
cd requests-enhancer
pip install -e ".[dev]"
pytest

Contributing

See CONTRIBUTING.md.


Changelog

See CHANGELOG.md.


License

MIT © Hexa-devy

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

requests_enhancer-1.4.2.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

requests_enhancer-1.4.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file requests_enhancer-1.4.2.tar.gz.

File metadata

  • Download URL: requests_enhancer-1.4.2.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for requests_enhancer-1.4.2.tar.gz
Algorithm Hash digest
SHA256 1102476ec36f1e69f6b656d1bb8d23e617cc7d4a4cad4a3748e12f06dd407635
MD5 d4af8b5f972846d0dcbe046e06c1d267
BLAKE2b-256 53ea78c6840bf6b51be09fdd5362875143cf265f9f4217ede18164348e19f167

See more details on using hashes here.

File details

Details for the file requests_enhancer-1.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for requests_enhancer-1.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 995ee49151cc2c2ef32531983153c8d2e4c57dbfe4bab6944b34241cbe92b63b
MD5 10659dd20eaf203ccee3624e4e37dfdd
BLAKE2b-256 41e14d1267695ec2105fee0869e13ee1e537f3113fd3f834b9ed4639764cdf0e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page