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")

For async usage:

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, so you don't have to explicitly define policies for simple use cases.

HTTPX adds support for asynchronous requests, so the package exposes a new retry utility. To make it easy to migrate, the API surface is almost identical, 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)

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.2.1.tar.gz (11.0 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.2.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for httpx_retries-0.2.1.tar.gz
Algorithm Hash digest
SHA256 cba1606b2f170a30a5d3eb00134895f31757d90becc8c8bd58bf16e7bae63aca
MD5 dd4ae081ee9eee36a146b5ed228aa746
BLAKE2b-256 064e604de40b83038152402a2c99f9b83e8d1ccfa15619558ee2d89dc145b3c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for httpx_retries-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4024383139216cf2bebb25d5b21d6bf45646de23ddd2ef36255006bf775e6ab8
MD5 e8e05b814ed37168d53eb8a3dafdd6a2
BLAKE2b-256 294d3de9bcc4dfc019ea1fde4242d191b4f43a5e4458bacbf3b30a4941dd351c

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