backoff icon indicating copy to clipboard operation
backoff copied to clipboard

Obtain value returned by backoff_handler function after maximum tries is reached

Open AanandhiVB opened this issue 1 year ago • 0 comments

Hi, first of all, a huge THANK YOU to all the contributors of this wonderful package. I have been using backoff for a while now and I am amazed at how efficiently it can be used for various usecases.

However, recently I am facing a issue that is similar to issue #79. Using backoff decorator, I am trying to retry failed API calls "max_tries" times. Once maximum attempts are reached, the backoff_hdlr function is called by the decorator and backoff_hdlr returns the status code of the failed API call. Is there a way for call_url function to return the status code returned by backoff_hdlr function after maximum tries is reached?

def backoff_hdlr(e):
    return e['exception'].status

# backoff decorator to retry failed API calls by "max_tries"
@backoff.on_exception(backoff.expo, aiohttp.ClientResponseError, max_tries=2, logger=logger, on_giveup=backoff_hdlr)
async def call_url(language: str, word:str, headers:dict) -> bytes:

      url = f"https://sample-url/{language}/{word.lower()}"
      print(f"Begin api call: {url}")
    
      # Create aiohttp session to trigger 'get' API call with app_id, app_key as headers
      async with aiohttp.ClientSession(headers=headers) as session:

          # raise_for_status is used to raise exception for status_codes other than 200      
          async with session.get(url, raise_for_status=True) as response:
                # Awaits response from API  
                content = await response.read()                              
                status = response.status
                print("Finished: ", status)
                return status

If this is not possible, then is there any other way to get status code of failed API request from call_url function after maximum tries is reached? I have checked everywhere but I am not able to get a solution for this.

AanandhiVB avatar Nov 19 '22 21:11 AanandhiVB