Skip to content
Advertisement

Call a function in all API endpoints with decorator?

I was wondering if there was a way to ensure some function was called in all Flask API endpoints without manually calling the function? This is to prevent repetition in making sure the functions get called, and to avoid mistakes of accidentally forgetting to call it. Maybe with a library or decorator?

JavaScript

So i want

JavaScript

Thanks in advance!

Advertisement

Answer

You can use app.before_request

From the doc

before_request(f)

Register a function to run before each request. For example, this can be used to open a database connection, or to load the logged in user from the session.

example:

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