PYTHON
Implementing Basic Retry Logic for Unreliable APIs in Python
Develop a simple retry mechanism in Python to handle transient API errors. This snippet retries failed requests a fixed number of times with a short, constant delay.
import requests
import time
def make_reliable_api_call(url, headers=None, max_retries=3, initial_delay_seconds=2):
retries = 0
while retries <= max_retries:
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
print(f"API call successful after {retries} retries.")
return response.json()
except requests.exceptions.HTTPError as e:
if 500 <= e.response.status_code < 600 and retries < max_retries:
# Server error (5xx) and we have retries left
print(f"Server error ({e.response.status_code}). Retrying in {initial_delay_seconds}s... (Attempt {retries + 1}/{max_retries})")
time.sleep(initial_delay_seconds)
retries += 1
else:
# Client error (4xx) or max retries reached, re-raise
print(f"HTTP error: {e.response.status_code} - {e.response.text}")
raise
except requests.exceptions.ConnectionError as e:
if retries < max_retries:
print(f"Connection error. Retrying in {initial_delay_seconds}s... (Attempt {retries + 1}/{max_retries})")
time.sleep(initial_delay_seconds)
retries += 1
else:
print(f"Connection error after {max_retries} retries: {e}")
raise
except requests.exceptions.Timeout as e:
if retries < max_retries:
print(f"Timeout error. Retrying in {initial_delay_seconds}s... (Attempt {retries + 1}/{max_retries})")
time.sleep(initial_delay_seconds)
retries += 1
else:
print(f"Timeout error after {max_retries} retries: {e}")
raise
except Exception as e:
print(f"An unexpected error occurred: {e}")
raise
# This part should ideally not be reached if exceptions are re-raised correctly
raise Exception("Max retries exceeded without successful response.")
# Example Usage:
# API_URL = 'https://api.example.com/sometimes-unreliable-endpoint'
# HEADERS = {'Authorization': 'Bearer YOUR_TOKEN'}
# try:
# data = make_reliable_api_call(API_URL, headers=HEADERS, max_retries=5, initial_delay_seconds=1)
# print('Received data:', data)
# except Exception as e:
# print('Failed to get data after retries:', e)
How it works: This Python snippet demonstrates a basic retry mechanism for handling transient failures when interacting with external APIs. The `make_reliable_api_call` function attempts to make an API request using `requests`. If it encounters a server-side error (HTTP 5xx status code), a connection error, or a timeout, it waits for a fixed `initial_delay_seconds` and retries the request up to `max_retries` times. For client-side errors (HTTP 4xx), it immediately re-raises the error. This simple strategy helps improve the robustness of integrations with APIs that might occasionally be unavailable or slow.