sources.helpers.requests.retry
retry_if_status Objects
class retry_if_status(retry_base)
Retry for given response status codes
Client Objects
class Client()
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 astimedelta
orfloat/int
number of seconds.max_connections
- Max connections per host in the HTTPAdapter poolraise_for_status
- Whether to raise exception on error status codes (usingresponse.raise_for_status()
)session
- Optionalrequests.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. Default429
and all5xx
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 upretry_condition
- A predicate or a list of predicates to decide whether to retry. If any predicate returnsTrue
the request is retriedrequest_backoff_factor
- Multiplier used for exponential delay between retriesrequest_max_retry_delay
- Maximum delay when using exponential backoffrespect_retry_after_header
- Whether to use theRetry-After
response header (when available) to determine the retry delaysession_attrs
- Extra attributes that will be set on the session instance, e.g.{headers: {'Authorization': 'api-key'}}
(seerequests.sessions.Session
for possible attributes)
update_from_config
def update_from_config(config: RunConfiguration) -> None
Update session/retry settings from RunConfiguration