Skip to main content

A retry layer for HTTPX.

Project description

A retry layer for HTTPX.


HTTPX Retries implements request retry for HTTPX.

It's very common to deal with flaky and unreliable APIs. When requests fail, your program needs to be able to retry them.


Install HTTPX Retries using pip:

pip install httpx-retries

To get started, add the transport to your client:

import httpx
from httpx_retries import RetryTransport

with httpx.Client(transport=RetryTransport()) as client:
    response = client.get("https://example.com")

Async usage is just as straightforward.

async with httpx.AsyncClient(transport=RetryTransport()) as client:
    response = await client.get("https://example.com")

If you want to use a specific retry strategy, provide a Retry configuration:

from httpx_retries import Retry

retry = Retry(total=5, backoff_factor=0.5)
transport = RetryTransport(retry=retry)

with httpx.Client(transport=transport) as client:
    response = client.get("https://example.com")

Features

HTTPX Retries builds on the patterns users will expect from urllib and requests. The typical approach has been to use urllib3's Retry utility to configure a retry policy. The equivalent code to match the above example using requests is:

from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

retry = Retry(total=5, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)

with requests.Session() as session:
    session.mount("http://", adapter)
    session.mount("https://", adapter)
    response = session.get("https://example.com")

To reduce boilerplate, this package includes a transport that works with both sync and async HTTPX Clients, with sensible defaults for simple use cases.

HTTPX adds support for asynchronous requests, and this package includes a new retry utility that can handle this behaviour. To make it easy to migrate, the API surface is almost identical to Retry from urllib3, with a few main differences:

  • total is the only parameter used to configure the number of retries.
  • asleep is an async implementation of sleep.
  • backoff_strategy can be overridden to customize backoff behavior.
  • Some options that are not strictly retry-related are not included (raise_on_status, raise_on_redirect)

Differences with other retry libraries

HTTPX Retries tries to make configuring retries simple and centralised, for the case of request / response. Other libraries are general purpose, which is good if you need to have a fully custom retry strategy. Usually though, most programs need a set of sensible defaults, configured once.

Take a look at the comparison with other libraries in the documentation.

Contributing

If you want to contribute to the project, check out the Contributing Guide.

Acknowledgements

This package builds on the great work done on HTTPX, urllib3 and requests.

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_retries-0.4.6.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

httpx_retries-0.4.6-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file httpx_retries-0.4.6.tar.gz.

File metadata

  • Download URL: httpx_retries-0.4.6.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.24

File hashes

Hashes for httpx_retries-0.4.6.tar.gz
Algorithm Hash digest
SHA256 a076d8a5ede5d5794e9c241da17b15b393b482129ddd2fdf1fa56a3fa1f28a7f
MD5 1796f64e03371be300dc88c8cd04c07b
BLAKE2b-256 a4135eac2df576c02280f79e4639a6d4c93a25cfe94458275f5aa55f5e6c8ea0

See more details on using hashes here.

File details

Details for the file httpx_retries-0.4.6-py3-none-any.whl.

File metadata

File hashes

Hashes for httpx_retries-0.4.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d66d912173b844e065ffb109345a453b922f4c2cd9c9e11139304cb33e7a1ee1
MD5 0e767302d820a2a69fa4dbd0161071a2
BLAKE2b-256 f29763f56da4400034adde22adfe7524635dba068f17d6858f92ecd96f55b53e

See more details on using hashes here.

Supported by

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