Skip to main content
Version: 1.2.0 (latest)

sources.helpers.requests.retry

retry_if_status Objects

class retry_if_status(retry_base)

[view_source]

Retry for given response status codes

Client Objects

class Client()

[view_source]

Wrapper for requests to create a Session with configurable retry functionality.

Notes:

Create a requests.Session which automatically retries requests in case of error. By default retries are triggered for 5xx and 429 status codes and when the server is unreachable or drops connection.

Custom retry condition You can provide one or more custom predicates for specific retry condition. The predicate is called after every request with the resulting response and/or exception. For example, this will trigger a retry when the response text is error:


from typing import Optional
from requests import Response

def should_retry(response: Optional[Response], exception: Optional[BaseException]) -> bool:
if response is None:
return False
return response.text == 'error'

The retry is triggered when either any of the predicates or the default conditions based on status code/exception are True.

Arguments:

  • request_timeout - Timeout for requests in seconds. May be passed as timedelta or float/int number of seconds.
  • max_connections - Max connections per host in the HTTPAdapter pool
  • raise_for_status - Whether to raise exception on error status codes (using response.raise_for_status())
  • session - Optional requests.Session instance to add the retry handler to. A new session is created by default.
  • status_codes - Retry when response has any of these status codes. Default 429 and all 5xx codes. Pass an empty list to disable retry based on status.
  • exceptions - Retry on exception of given type(s). Default (requests.Timeout, requests.ConnectionError). Pass an empty list to disable retry on exceptions.
  • request_max_attempts - Max number of retry attempts before giving up
  • retry_condition - A predicate or a list of predicates to decide whether to retry. If any predicate returns True the request is retried
  • request_backoff_factor - Multiplier used for exponential delay between retries
  • request_max_retry_delay - Maximum delay when using exponential backoff
  • respect_retry_after_header - Whether to use the Retry-After response header (when available) to determine the retry delay
  • session_attrs - Extra attributes that will be set on the session instance, e.g. {headers: {'Authorization': 'api-key'}} (see requests.sessions.Session for possible attributes)

update_from_config

def update_from_config(config: RunConfiguration) -> None

[view_source]

Update session/retry settings from RunConfiguration

This demo works on codespaces. Codespaces is a development environment available for free to anyone with a Github account. You'll be asked to fork the demo repository and from there the README guides you with further steps.
The demo uses the Continue VSCode extension.

Off to codespaces!

DHelp

Ask a question

Welcome to "Codex Central", your next-gen help center, driven by OpenAI's GPT-4 model. It's more than just a forum or a FAQ hub – it's a dynamic knowledge base where coders can find AI-assisted solutions to their pressing problems. With GPT-4's powerful comprehension and predictive abilities, Codex Central provides instantaneous issue resolution, insightful debugging, and personalized guidance. Get your code running smoothly with the unparalleled support at Codex Central - coding help reimagined with AI prowess.