Python v4 SDKs now retry failed requests, and retrying is on by default. The client accepts a retry_options keyword that controls how many retries are made, which status codes and methods qualify, and how the backoff grows between attempts.
Details
By default the SDK retries up to 3 times, after a connection that failed or timed out and after a 408, 429, 500, 502, 503 or 504. The wait starts at 1 second and doubles with each retry, with up to half a second of jitter and a ceiling of 60 seconds. When the server sends a Retry-After header, in seconds or as an HTTP date, the SDK waits that long instead, capped at the same ceiling.
Only GET, HEAD, PUT and OPTIONS are retried by default, so a POST, PATCH or DELETE is never repeated unless you add it to http_methods_to_retry. Every retry of a call carries the same Idempotency-Key, which makes adding POST safe for APIs that deduplicate on it. request_options overrides max_retries and status_codes_to_retry for one call.
When the last attempt fails, you get what a single attempt would give you: the ApiError for an error status, or the httpx2 exception for a connection that never answered. The policy is the same on the synchronous and asynchronous clients.
Usage Example
Keep the default policy, tune it on the client, or change it for one call:
from payments import PaymentsClient
from payments.core import RetryOptions
# The default policy: up to 3 retries of GET, HEAD, PUT and OPTIONS.
client = PaymentsClient(bearer_auth="YOUR_ACCESS_TOKEN")
# More retries, a shorter first wait, and POST retried because the API deduplicates on Idempotency-Key.
client = PaymentsClient(
bearer_auth="YOUR_ACCESS_TOKEN",
retry_options=RetryOptions(
max_retries=5,
initial_delay=0.5,
http_methods_to_retry=frozenset({"GET", "HEAD", "PUT", "OPTIONS", "POST"}),
),
)
# No retries for this one call.
payment = client.payments.get_payment("pay_123", request_options={"max_retries": 0})
Pass retry_options=0 to turn retrying off for the whole client, for example in a test that stubs an error response.
This feature is available in the Python v4 SDK. Generate it with version 2.x of the APIMatic CLI:
apimatic sdk generate --language=python
For the full set of flags and options, see the APIMatic CLI documentation.