Skip to content
Advertisement

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.

JavaScript

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

Advertisement

Answer

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

JavaScript

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement