0

I have some code to check the API response status, when it is correct, it wil return the reponse.json() from that API. However I want to expand this with a retry command in terms if there is a disconnect between the client and server and eventually a break with the 404 code because now currently I am just redoing the same function.

def API_status_report(url: str):
    response = requests.get(url)
    if response.status_code == 200:
        return response.json()
    elif response.status_code == 204:
        return response.json()
    elif response.status_code == 404:
        print("Not Found: retrying")
        API_status_report(url)

I am currently looking in the retry module, and I still cannot get my heads arround it, because it is kind of confusing. Can anyone help me on this?

1 Answers1

0

We probably want to avoid the recursive API_status_report call here (Python supports recursion, but tail call optimization is not planned and it's possible to get into trouble with infinite recursion depth).

Here is one way to handle this with the retry module suggested in the question:

import requests
from retry import retry


class Exception404(Exception):
    """Custom error raised when a 404 response code is sent by the server."""
    def __init__(self, message):
        super().__init__(message)


@retry(Exception404, tries=3, delay=1)
def API_status_report(url: str):
    response = requests.get(url)
    if response.status_code == 200:
        return response.json()
    elif response.status_code == 204:
        return response.json()
    elif response.status_code == 404:
        print("Not Found: retrying")

        # Instead of recurring, we'll raise an Exception.
        raise Exception404("Could not complete request")


if __name__ == "__main__":
    # Good url: this should return successfully:
    print(API_status_report("http://date.jsontest.com/"))

    try:
        # This is a bad url, but we'll try this three times.
        print(API_status_report("http://jsontest.com/bad-data/"))
    except Exception404:
        # We can still handle the error to exit gracefully.
        print("Handle the 404 Error.")

Expected output:

{'date': '04-18-2021', 'milliseconds_since_epoch': 1618766324441, 'time': '05:18:44 PM'}
Not Found: retrying
Not Found: retrying
Not Found: retrying
Handle the 404 Error.
Alexander L. Hayes
  • 1,657
  • 2
  • 11
  • 23
  • Question considering, this python script is already gonna have a main function (because this one is being integrated in flask Application, having three inputs ) could I have something else without main? I am quit an amateur at python so I do not know why you if _name_ = "_main_" is used for?. – ThunderSpark Apr 19 '21 at 17:38
  • [See this thread for what `if __name__ == "__main__"` does](https://stackoverflow.com/questions/419163/what-does-if-name-main-do). And sure: this could just as easily be used in a flask application. – Alexander L. Hayes Apr 19 '21 at 17:53