### Install Dependencies Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/contributing.md Install project dependencies using the 'uv sync' command. ```shell uv sync ``` -------------------------------- ### Requests Library Equivalent with urllib3 Retry Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/index.md This example shows the equivalent setup using the requests library and urllib3's Retry utility. It demonstrates how httpx-retries aims for a similar developer experience. ```python 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") ``` -------------------------------- ### Install httpx-retries Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/index.md Install the httpx-retries package using pip. This is the first step to enable retry functionality. ```bash pip install httpx-retries ``` -------------------------------- ### Basic Asynchronous Client Setup Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/index.md Use RetryTransport with an asynchronous httpx.AsyncClient for retrying requests in async applications. The setup is similar to the synchronous version. ```python import httpx from httpx_retries import RetryTransport async with httpx.AsyncClient(transport=RetryTransport()) as client: response = await client.get("https://example.com") ``` -------------------------------- ### Tenacity Retry Example for Connection Errors Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/differences_with_other_retry_libraries.md This example demonstrates how to configure Tenacity to retry a function up to three times specifically for HTTPX connection errors on a particular host, logging each retry attempt. ```python import logging import httpx from tenacity import before_sleep_log, retry, retry_if_exception, stop_after_attempt logger = logging.getLogger(__name__) SOME_HOST: str = "..." # GET requests are known to be flaky on this host. def is_some_host_predicate(exc: BaseException) -> bool: return isinstance(exc, httpx.ConnectError) and exc.request.url.host == SOME_HOST @retry( retry=retry_if_exception(is_some_host_predicate), before_sleep=before_sleep_log(logger, logging.WARNING), stop=stop_after_attempt(3), ) async def get_from_some_host(client: httpx.AsyncClient, ...): ... ``` -------------------------------- ### Basic Synchronous Client Setup Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/index.md Add the RetryTransport to a synchronous httpx.Client to enable automatic retries. Defaults are used for retry configuration. ```python import httpx from httpx_retries import RetryTransport with httpx.Client(transport=RetryTransport()) as client: response = client.get("https://example.com") ``` -------------------------------- ### Custom Retry Strategy with Specific Backoff Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/behaviour.md Implement a custom retry strategy by subclassing Retry. This example waits 1 second for every third attempt, otherwise using the default strategy. Ensure the Retry class is imported. ```python from httpx_retries import Retry class CustomRetry(Retry): def backoff_strategy(self) -> float: if self.attempts_made % 3: return 1.0 return super().backoff_strategy() ``` -------------------------------- ### Manually Retrying Body-Phase Errors with httpx Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/faq.md Provides an example of how to manually implement retries for exceptions that occur during response body reading, such as ReadTimeout or RemoteProtocolError. ```python import httpx retryable = (httpx.ReadTimeout, httpx.RemoteProtocolError) for attempt in range(5): try: response = client.get("https://example.com") break except retryable: if attempt == 4: raise ``` -------------------------------- ### Preview Documentation Locally Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/contributing.md Serve the documentation locally using MkDocs to preview changes. Access it via http://127.0.0.1:8000 in your browser. ```shell mkdocs serve ``` -------------------------------- ### Run Test Suite Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/contributing.md Execute the project's test suite with coverage reporting using the provided test script. ```shell ./scripts/test ``` -------------------------------- ### Configure Logging and RetryTransport Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/logging.md This snippet demonstrates how to set up basic logging with a custom format and level, and initialize RetryTransport with retry parameters. It's used to enable detailed logging for HTTPX requests that involve retries. ```python import logging import httpx from httpx_retries import Retry, RetryTransport logging.basicConfig( format="%(levelname)s [%(asctime)s] %(name)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.DEBUG ) transport = RetryTransport(retry=Retry(total=3, backoff_factor=0.5)) with httpx.Client(transport=transport) as client: response = client.get("https://httpco.de/429") ``` -------------------------------- ### Run All Code Quality Checks Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/contributing.md Run all code quality checks, including formatting, linting with ruff, and type checking with mypy. ```shell ./scripts/check ``` -------------------------------- ### Clone the Repository Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/contributing.md Clone your forked repository to your local machine. Replace YOUR-USERNAME with your actual GitHub username. ```shell git clone https://github.com/YOUR-USERNAME/httpx-retries.git cd httpx-retries ``` -------------------------------- ### Configuring Retry with Max Backoff Wait and Backoff Factor Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/faq.md Shows how to configure Retry with specific values for max_backoff_wait and backoff_factor to control retry behavior and estimate total timeout. ```python Retry(total=3, max_backoff_wait=5.0, backoff_factor=1, jitter=0) ``` -------------------------------- ### Manually Publish to PyPI Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/contributing.md Manually publish a new release to PyPI if the automated GitHub release process fails. This script is intended for maintainers. ```shell ./scripts/publish ``` -------------------------------- ### Chaining RetryTransport with AsyncHTTPTransport and RateLimitTransport Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/faq.md Demonstrates how to chain RetryTransport with other custom transports like RateLimitTransport and AsyncHTTPTransport for advanced request handling. ```python with AsyncClient( transport=RetryTransport( RateLimitTransport( AsyncHTTPTransport(), interval=timedelta(seconds=1), count=3, ), retry=Retry(total=5, backoff_factor=0.5), ), timeout=90.0, ) as client: ... ``` -------------------------------- ### Applying Client Parameters with Custom Transports Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/faq.md When using a custom transport like `RetryTransport`, parameters like `http2`, `limits`, and `verify` must be passed to the underlying transport (e.g., `httpx.HTTPTransport`) instead of directly to `httpx.Client` to be applied. ```python from httpx import Client, HTTPTransport from httpx_retries import Retry, RetryTransport with Client( transport=RetryTransport( HTTPTransport( # Pass a transport with these parameters to wrap. http2=True, limits=httpx.Limits(max_connections=100, max_keepalive_connections=100, keepalive_expiry=60), verify=False ), retry=Retry(total=5, backoff_factor=0.5), ) # These will do nothing! http2=True, limits=httpx.Limits(max_connections=100, max_keepalive_connections=100, keepalive_expiry=60), verify=False ) as client: ... ``` -------------------------------- ### Add Upstream Remote Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/contributing.md Add the original repository as a remote named 'upstream' to easily sync the latest changes. ```shell git remote add upstream https://github.com/will-ockmore/httpx-retries.git ``` -------------------------------- ### Custom Retry Strategy with Specific Configuration Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/index.md Configure a custom retry strategy by providing a Retry object with specific parameters like total retries and backoff factor. This allows fine-tuning retry behavior. ```python from httpx_retries import Retry from httpx_retries import RetryTransport 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") ``` -------------------------------- ### Exponential Backoff Formula Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/behaviour.md This formula illustrates the calculation for exponential backoff, where the wait time increases exponentially with each attempt. On the first attempt, the backoff time equals the backoff factor. ```python backoff_time = backoff_factor * (2 ** attempts_made) ``` -------------------------------- ### Custom Retry Strategy with RetryTransport Source: https://github.com/will-ockmore/httpx-retries/blob/main/README.md Configure a specific retry strategy using Retry(total=5, backoff_factor=0.5) and pass it to RetryTransport. This allows fine-tuning retry attempts and backoff delays. ```python 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") ``` -------------------------------- ### Asynchronous HTTPX Client with RetryTransport Source: https://github.com/will-ockmore/httpx-retries/blob/main/README.md Add the RetryTransport to an asynchronous httpx.AsyncClient for automatic retries. This is suitable for async applications. ```python async with httpx.AsyncClient(transport=RetryTransport()) as client: response = await client.get("https://example.com") ``` -------------------------------- ### Configure Retry with Total Timeout Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/behaviour.md Set the `total_timeout` parameter to limit the cumulative sleep time across all retry attempts for a request. This ensures that a request does not exceed a specified total duration due to retries. ```python retry = Retry(total=10, total_timeout=30) ``` -------------------------------- ### Retry on Custom Response Content Source: https://github.com/will-ockmore/httpx-retries/blob/main/docs/faq.md Use `validate_response` to inspect response content or headers and raise a custom exception to trigger a retry. Avoid calling `.read()` or `.aread()` within `validate_response` when using streaming to prevent buffering. ```python import httpx from httpx_retries import Retry, RetryTransport class ContentBlocked(ValueError): pass def validate_response(response: httpx.Response) -> None: # safely inspect status and headers if needed response.raise_for_status() # NOTE: Do not call `.read()` here with `Client.stream`, # it will buffer the entire body, which defeats the purpose of streaming. response.read() if "content blocked" in response.text: raise ContentBlocked(response.text) retry = Retry(validate_response=validate_response, retry_on_exceptions=[httpx.HTTPStatusError, ContentBlocked]) with httpx.Client(transport=RetryTransport(retry=retry)) as client: response = client.get("https://example.com") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.