| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import requests |
| 4 |
|
| 5 |
from .retry import ( |
| 6 |
RetryCheck, |
| 7 |
RetryCheckFlags, |
| 8 |
RetryCheckResult, |
| 9 |
RetryContext, |
| 10 |
set_retry_check_flags, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
def check_request_exception(ctx: RetryContext) -> RetryCheckResult: |
| 15 |
""" |
| 16 |
Check if a network error was encountered |
| 17 |
|
| 18 |
:param ctx: The context object which describes the state of the request and the |
| 19 |
retries which may already have been attempted. |
| 20 |
""" |
| 21 |
if ctx.exception and isinstance(ctx.exception, requests.RequestException): |
| 22 |
return RetryCheckResult.do_retry |
| 23 |
return RetryCheckResult.no_decision |
| 24 |
|
| 25 |
|
| 26 |
def check_retry_after_header(ctx: RetryContext) -> RetryCheckResult: |
| 27 |
""" |
| 28 |
Check for a retry-after header if the response had a matching status |
| 29 |
|
| 30 |
:param ctx: The context object which describes the state of the request and the |
| 31 |
retries which may already have been attempted. |
| 32 |
""" |
| 33 |
retry_config = ctx.caller_info.retry_config |
| 34 |
if ctx.response is None or ( |
| 35 |
ctx.response.status_code not in retry_config.retry_after_status_codes |
| 36 |
): |
| 37 |
return RetryCheckResult.no_decision |
| 38 |
retry_after = _parse_retry_after(ctx.response) |
| 39 |
if retry_after: |
| 40 |
ctx.backoff = float(retry_after) |
| 41 |
return RetryCheckResult.do_retry |
| 42 |
|
| 43 |
|
| 44 |
def check_transient_error(ctx: RetryContext) -> RetryCheckResult: |
| 45 |
""" |
| 46 |
Check for transient error status codes which could be resolved by retrying |
| 47 |
the request |
| 48 |
|
| 49 |
:param ctx: The context object which describes the state of the request and the |
| 50 |
retries which may already have been attempted. |
| 51 |
""" |
| 52 |
retry_config = ctx.caller_info.retry_config |
| 53 |
if ctx.response is not None and ( |
| 54 |
ctx.response.status_code in retry_config.transient_error_status_codes |
| 55 |
): |
| 56 |
return RetryCheckResult.do_retry |
| 57 |
return RetryCheckResult.no_decision |
| 58 |
|
| 59 |
|
| 60 |
@set_retry_check_flags(RetryCheckFlags.RUN_ONCE) |
| 61 |
def check_expired_authorization(ctx: RetryContext) -> RetryCheckResult: |
| 62 |
""" |
| 63 |
This check evaluates whether or not there is invalid or expired authorization |
| 64 |
information which could be updated with some action -- most typically a token |
| 65 |
refresh for an expired access token. |
| 66 |
|
| 67 |
The check is flagged to only run once per request. |
| 68 |
|
| 69 |
:param ctx: The context object which describes the state of the request and the |
| 70 |
retries which may already have been attempted. |
| 71 |
""" |
| 72 |
retry_config = ctx.caller_info.retry_config |
| 73 |
if ( # is the current check applicable? |
| 74 |
ctx.response is None |
| 75 |
or ctx.caller_info is None |
| 76 |
or ctx.caller_info.authorizer is None |
| 77 |
or ( |
| 78 |
ctx.response.status_code |
| 79 |
not in retry_config.expired_authorization_status_codes |
| 80 |
) |
| 81 |
): |
| 82 |
return RetryCheckResult.no_decision |
| 83 |
|
| 84 |
# run the authorizer's handler, and 'do_retry' if the handler indicated |
| 85 |
# that it was able to make a change which should make the request retryable |
| 86 |
if ctx.caller_info.authorizer.handle_missing_authorization(): |
| 87 |
return RetryCheckResult.do_retry |
| 88 |
return RetryCheckResult.no_decision |
| 89 |
|
| 90 |
|
| 91 |
def _parse_retry_after(response: requests.Response) -> int | None: |
| 92 |
""" |
| 93 |
Get the 'Retry-After' header as an int. |
| 94 |
|
| 95 |
:param response: The response to parse. |
| 96 |
""" |
| 97 |
val = response.headers.get("Retry-After") |
| 98 |
if not val: |
| 99 |
return None |
| 100 |
try: |
| 101 |
return int(val) |
| 102 |
except ValueError: |
| 103 |
return None |
| 104 |
|
| 105 |
|
| 106 |
DEFAULT_RETRY_CHECKS: tuple[RetryCheck, ...] = ( |
| 107 |
check_expired_authorization, |
| 108 |
check_request_exception, |
| 109 |
check_retry_after_header, |
| 110 |
check_transient_error, |
| 111 |
) |