Fetch JSON over HTTP with retries
Python
import time
import requests
HEADERS = {"User-Agent": "bits-demo/1.0", "Accept": "application/json"}
RETRY_STATUS = {429, 500, 502, 503, 504}
def get_json(url: str, attempts: int = 3, timeout: float = 5.0) -> dict:
"""GET a JSON document, retrying transient failures with backoff."""
last_error: Exception | None = None
for attempt in range(1, attempts + 1):
try:
response = requests.get(url, headers=HEADERS, timeout=timeout)
if response.status_code in RETRY_STATUS:
raise requests.HTTPError(f"retryable status {response.status_code}")
response.raise_for_status()
return response.json()
except (requests.RequestException, ValueError) as exc:
last_error = exc
if attempt < attempts:
time.sleep(0.5 * 2 ** (attempt - 1)) # 0.5s, 1s, 2s
raise RuntimeError(f"GET {url} failed after {attempts} attempts: {last_error}")
if __name__ == "__main__":
try:
print(sorted(get_json("https://httpbin.org/json")))
except RuntimeError as exc:
print(exc)