CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/pypi-requests-cache

tessl install tessl/pypi-requests-cache@1.2.0

A persistent cache for python requests

Agent Success

Agent success rate when using this tile

76%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.27x

Baseline

Agent success rate without this tile

60%

task.mdevals/scenario-2/

HTTP Cache with Custom Expiration

Build a simple HTTP caching utility that allows configuring cache expiration times both at the session level and on a per-request basis.

Requirements

Create a utility that:

  1. Initializes a caching session with a default expiration time
  2. Makes cached HTTP GET requests that use the session's default expiration
  3. Allows overriding the default expiration for individual requests
  4. Supports both integer seconds and timedelta objects for expiration times

Test Cases

  • A session with 60-second default expiration caches a GET request to "https://httpbin.org/get" and the cache expires after 60 seconds @test
  • A session with 3600-second default expiration makes a GET request to "https://httpbin.org/delay/1" with expiration override of 10 seconds, and the cache expires after 10 seconds @test
  • A session with default expiration makes a GET request to "https://httpbin.org/json" with timedelta(seconds=30) override, and the cache expires after 30 seconds @test

Implementation

@generates

API

"""HTTP caching utility with per-request expiration control."""

from datetime import timedelta
from typing import Union, Optional

def create_cache_session(default_expiration: Union[int, timedelta]) -> object:
    """
    Create a new HTTP caching session with a default expiration time.

    Args:
        default_expiration: Default time for cached responses to remain valid.
                          Can be an integer (seconds) or timedelta object.

    Returns:
        A session object that supports making cached HTTP requests.
    """
    pass

def make_request(session: object, url: str, expiration: Optional[Union[int, timedelta]] = None) -> object:
    """
    Make an HTTP GET request using the provided session.

    Args:
        session: The caching session to use for the request.
        url: The URL to request.
        expiration: Optional expiration override for this specific request.
                   If not provided, uses the session's default expiration.
                   Can be an integer (seconds), timedelta, or special value.

    Returns:
        The response object from the HTTP request.
    """
    pass

def is_cached(session: object, url: str) -> bool:
    """
    Check if a URL is currently cached in the session.

    Args:
        session: The caching session to check.
        url: The URL to check for in the cache.

    Returns:
        True if the URL is cached and not expired, False otherwise.
    """
    pass

Dependencies { .dependencies }

requests-cache { .dependency }

Provides persistent HTTP caching for Python requests.

@satisfied-by

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/requests-cache@1.2.x
tile.json