HTTPX Throttle Cache Client

0.3.5 · active · verified Thu Apr 16

httpxthrottlecache is a Python library that provides an HTTPX client with integrated rate limiting and caching capabilities. It abstracts away complex configurations for managing network requests, improving performance and adhering to API limits. The library is actively maintained, currently at version 0.3.5, and releases frequently to add features and address issues.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the HttpxThrottleCache client with file-based caching and rate limiting. It demonstrates a synchronous GET request to an example URL, printing the status code and headers. Users can configure cache mode, directory, rate limits, user agent, and specific caching rules using regular expressions. The client can be used as a context manager to ensure proper resource handling.

import os
from httpxthrottlecache import HttpxThrottleCache

# Example usage with a dummy URL and basic configuration
# For real usage, replace 'https://api.example.com' and 'your_user_agent'
url = "https://httpbingo.org/get"

with HttpxThrottleCache(
    cache_mode="FileCache", # Use 'FileCache' or 'Hishel-File' (if hishel is installed)
    cache_dir="_my_cache",
    rate_limiter_enabled=True,
    request_per_sec_limit=5,
    user_agent=os.environ.get('MY_APP_USER_AGENT', 'httpxthrottlecache-example/1.0'),
    # Example cache_rules to cache all paths for an hour
    cache_rules={
        '.*': {
            '.*': 3600 # Cache all responses for 3600 seconds (1 hour)
        }
    }
) as manager:
    with manager.http_client() as client:
        response = client.get(url)
        print(f"Status Code: {response.status_code}")
        print(f"Response headers: {response.headers}")
        # Uncomment to see response body:
        # print(response.json())

view raw JSON →