In a project I am doing, I have to perform a lot of repetitive checks at the beginning of each API end point. As the amount of duplicate code started to grow, I thought of using a decorator to wrap all of my api endpoint view functions, and to perform the checks there. This approach actually turned out to be useful for a while. but then again as the time passed, the number of checks growed, and the end points diverged in the tests that they needed. Now, I have ended up with a decorator that is quite long, and which takes a variety of parameters, and handles different responsibilities. To me, it smells of bad design, but I can't understand how I can fix it. I thought about adding other decorators but that doesn't seem to be applicable here, as I want my tests to run prior to the execution of the view it self, (the code after the view execution is fixed). But as far as I know, multiple decorators each wrap the other one. Would you mind providing me with any tips on the matter please?
Composition is a powerful tool.
Here is one way to define a flask endpoint.
def report( ... ):
You can freely mix and match such decorators. They offer great economy of expression.
If you find your business use case for say, credentials, or for logging, becomes quite complex, then step back and re-evaluate. Perhaps you need more than one credentials decorator.
There are several tools in your toolbox. Find the right one for the task at hand.