A retry layer for HTTPX.
Maintained. Last release 5 months ago, commits in 2 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install httpx-retriesA 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")
Errors while reading the response body (such as
ReadTimeoutpart-way through a download) happen after the transport has returned, soRetryTransportcan't retry them. This is a niche case, but if you read very large or slow bodies and need those retried, see theretry_request/aretry_requesthelpers in Why wasn't myReadTimeoutretried?.
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")