Persistent cache implementation for httpx and httpcore following RFC 9111 specification
74
Build a simple HTTP caching client that properly handles conditional requests using cache validators (ETags and Last-Modified timestamps).
Your task is to implement a caching HTTP client that:
Create a module cache_client.py with a CachedClient class that:
Your implementation should handle:
# test_cache_client.py
def test_etag_conditional_request():
"""Test that cached responses with ETags trigger conditional requests."""
client = CachedClient()
# First request - cache the response with ETag
response1 = client.get("https://api.example.com/data")
assert response1.status_code == 200
assert "ETag" in response1.headers
# Second request - should send If-None-Match
response2 = client.get("https://api.example.com/data")
# If server returns 304, client should serve cached content
assert response2.content == response1.contentdef test_last_modified_conditional_request():
"""Test that cached responses with Last-Modified trigger conditional requests."""
client = CachedClient()
# First request - cache the response with Last-Modified
response1 = client.get("https://api.example.com/resource")
assert response1.status_code == 200
assert "Last-Modified" in response1.headers
# Second request - should send If-Modified-Since
response2 = client.get("https://api.example.com/resource")
# Client should handle 304 by serving cached content
assert response2.content == response1.contentdef test_cache_update_on_new_content():
"""Test that cache is updated when server returns new content."""
client = CachedClient()
# First request
response1 = client.get("https://api.example.com/mutable")
original_content = response1.content
# Simulate time passing and content changing
# Second request gets new content (200 OK instead of 304)
response2 = client.get("https://api.example.com/mutable")
# If content changed, new content should be cached and returned
if response2.status_code == 200:
assert response2.content != original_contentAn elegant HTTP caching library for Python that implements RFC 9111 specifications. Provides built-in support for cache validators including ETag and Last-Modified headers with automatic conditional request generation.
Install with Tessl CLI
npx tessl i tessl/pypi-hisheldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10