Debian Codegraph @

src/globus_sdk/experimental/transfer_v2/transport.py

raw
1 """
2 Custom retry check collection for the TransferClientV2 that overrides
3 the default check_transient_error
4 """
5
6 from __future__ import annotations
7
8 from globus_sdk.transport import RetryCheck, RetryCheckResult, RetryContext
9 from globus_sdk.transport.default_retry_checks import (
10 DEFAULT_RETRY_CHECKS,
11 check_transient_error,
12 )
13
14
15 def check_transfer_v2_transient_error(ctx: RetryContext) -> RetryCheckResult:
16 """
17 check for transient error status codes which could be resolved by
18 retrying the request. Does not retry ExternalErrors or EndpointErrors
19 as those are unlikely to actually be transient.
20
21 :param ctx: The context object which describes the state of the request and the
22 retries which may already have been attempted
23 """
24 # JSON:API uses "the most generally applicable HTTP error code" for the
25 # status of the response, so if its a transient status code then all of the
26 # error objects should also have transient status codes.
27 retry_config = ctx.caller_info.retry_config
28 if ctx.response is not None and (
29 ctx.response.status_code in retry_config.transient_error_status_codes
30 ):
31 try:
32 # if any of the error objects have a `code` of ExternalError or
33 # EndpointError then the error likely isn't transient
34 errors = ctx.response.json()["errors"]
35 for error in errors:
36 if error["code"] in ("ExternalError", "EndpointError"):
37 return RetryCheckResult.no_decision
38
39 except (ValueError, KeyError):
40 pass
41
42 return RetryCheckResult.do_retry
43
44 return RetryCheckResult.no_decision
45
46
47 # Transfer retry checks are the defaults with the transient error one replaced
48 TRANSFER_V2_DEFAULT_RETRY_CHECKS: tuple[RetryCheck, ...] = tuple(
49 check_transfer_v2_transient_error if check is check_transient_error else check
50 for check in DEFAULT_RETRY_CHECKS
51 )