pythonpython-requests

Using Python decorators to retry request


I have multiple functions in my script which does a REST API api requests.As i need to handle the error scenarios i have put a retry mechanism as below.

no_of_retries = 3
def check_status():
    for i in range(0,no_of_retries):
        url = "http://something/something"
        try:
            result = requests.get(url, auth=HTTPBasicAuth(COMMON_USERNAME, COMMON_PASSWORD)).json()
            if 'error' not in result:
                return result
            else:
                continue
        except Exception as e:
            continue
    return None

I have several different methods which does similar operation. How can we do it better way to avoid duplication may be using decorators.


Solution

  • Instead of using decorators, the probably better solution is to move the request to its own function, arriving at a structure similar to this:

    no_of_retries = 3
    
    def make_request(url):
        for i in range(0,no_of_retries):
            try:
                result = requests.get(url, auth=HTTPBasicAuth(COMMON_USERNAME, COMMON_PASSWORD)).json()
                if 'error' not in result:
                    return result
                else:
                    continue
            except Exception as e:
                continue
        return result
    
    def check_status():
        result = make_request("http://something/status")
    
    def load_file():
        result = make_request("http://something/file")
    

    This way, you avoid duplicate code while encapsulating the request. If you were to use a decorator, you would need to wrap the whole load_file() method which would prevent you from further processing the request's result within this function.