pythonapiexceptionretry-logicconnectionexception

Implement Retry Functionality for request api in Python without any library


I am trying to build a vaccine availability project. I make a request every 4 seconds to check doses for a vaccine,

response = requests.get(https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=770&date=)

At times I face exceptions of Connection Error. I want to add retry functionality to my code, I know there are many answers out there, but they are using the library. I want to build my own retry functionality to understand core mechanics and for fun.

Can anyone help with how it should be implemented?


Solution

  • import requests
    import time
    def foo():
        try:
            response = response.get("some URL")
        # you can modify exceptions as per your choice
        except Exception as e:
            time.sleep(30)
            foo()