Skip to main content

Simple caching transport for httpx.

Project description

HTTPX-CACHE

codecov

Note: Early development / alpha, use at your own risk.

httpx-cache is yet another implementation/port is a port of the caching algorithms in httplib2 for use with httpx Transport object.

It is is heavily insipired by:

This project supports the latest version of httpx (at of the time of writing): httpx@0.21.1, when httpx releases a v1 version, the update should be straithforward for this project.

Installation

Using pip:

pip install httpx-cache

Using poetry:

poetry add httpx-cache

Features

  • In memory dict cache store
  • Support for cache-controle headers
  • Sync cache transport for httpx
  • Async cache transport for httpx

Quickstart

The lib provides an httpx compliant transport that you can use instead of the the defult one when creating your httpx client:

import httpx

from httpx_cache import CacheControlTransport

with httpx.Client(transport=CacheControlTransport()) as client:
  response = client.get("https://httpbin.org/get")

  # the response is effectively cached, calling teh same request with return a response from the cache

  response2 = client.get("https://httpbin.org/get")

You can also wrap an existing transport with CacheControlTransport. The CacheControlTransport will use the existing transport for making the request call and then cach the result if it satisfies the cache-control headers.

import httpx

from httpx_cache import CacheControlTransport

my_transport = httpx.HTTPTransport(http2=True, verify=False)

with httpx.Client(transport=CacheControlTransport(transport=my_transport)) as client:
  response = client.get("https://httpbin.org/get")

  # the response is effectively cached, calling teh same request with return a response from the cache

  response2 = client.get("https://httpbin.org/get")

Examples

more examples in ./examples.

TODO

  • support etags
  • support cache expiry dates
  • support file cach
  • support redis (sync/async) cache

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

httpx-cache-0.2.0.tar.gz (9.5 kB view hashes)

Uploaded Source

Built Distribution

httpx_cache-0.2.0-py3-none-any.whl (13.2 kB view hashes)

Uploaded Python 3

Supported by

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