Category: requests

Problem

Retry failed API calls with exponential backoff.

Solution

  for i in range(3):
    try:
        return requests.get(url, timeout=10)
    except requests.RequestException:
        time.sleep(2 ** i)
  

Notes

  • Adapt variable names and paths to your project
  • Add error handling for production use
  • See related chapters in the Learning Path