CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-hishel

Persistent cache implementation for httpx and httpcore following RFC 9111 specification

74

1.48x
Overview
Eval results
Files

task.mdevals/scenario-1/

HTTP Cache Validator Implementation

Build a simple HTTP caching client that properly handles conditional requests using cache validators (ETags and Last-Modified timestamps).

Requirements

Your task is to implement a caching HTTP client that:

  1. Makes an initial request to an API endpoint and caches the response along with its validator headers (ETag and/or Last-Modified)
  2. On subsequent requests to the same endpoint, sends conditional request headers based on the cached validators
  3. Handles 304 Not Modified responses by serving the cached content
  4. Updates the cache when receiving a 200 OK response with new content

Implementation Details

Create a module cache_client.py with a CachedClient class that:

  • Stores responses in memory with their ETag and Last-Modified values
  • Automatically includes If-None-Match header when an ETag is cached
  • Automatically includes If-Modified-Since header when a Last-Modified value is cached
  • Returns cached content when the server responds with 304 Not Modified
  • Updates the cache when receiving fresh content (200 OK)

Your implementation should handle:

  • Endpoints that provide ETag headers
  • Endpoints that provide Last-Modified headers
  • Endpoints that provide both validator types
  • Proper conditional request formation

Test Cases

Test Case 1: ETag Validation { @test }

# 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.content

Test Case 2: Last-Modified Validation { @test }

def 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.content

Test Case 3: Cache Update on Fresh Content { @test }

def 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_content

Dependencies { .dependencies }

hishel { .dependency }

An 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-hishel

tile.json