Review this generated request wrapper before it ships.
Retry TimeoutError and ConnectionError up to retries times, honor timeout, add retry metadata without changing caller-owned headers, keep credentials out of logs, and forward remaining client options.
Python
def request_with_retry(client, url, /, **options):
retries = options.pop("retries", 3)
timeout = options.get("timeout", 5)
headers = options.pop("headers", {})
if type(retries) is not int or retries < 1:
raise ValueError("retries must be a positive integer")
headers["X-Retry-Limit"] = str(retries)
print(f"request {url} headers={headers}")
for attempt in range(retries):
request_options = dict(options)
request_options["headers"] = headers
try:
return client.get(
url,
timeout=timeout,
**request_options,
)
except (TimeoutError, ConnectionError):
if attempt == retries - 1:
raise
generated code is illustrative, not from any one model