I'm working on a project with Flask and Flask-Restful, trying to get dynamic urls according to input from the user in my Restful API. I created a class with a get method that will be responding to requests, and when the user calls upon another resource, it activates the new one and a url should be created.
The problem comes when I try to add a new resource to my api, which calls the function setupmethod in Flask that checks if the app is in debug and if the first request was already made.
if self.debug and self._got_first_request:
raise AssertionError('A setup function was called after the '
'first request was handled. This usually indicates a bug '
'in the application where a module was not imported '
'and decorators or other functionality was called too late.\n'
'To fix this make sure to import all your view modules, '
'database models and everything related at a central place '
'before the application starts serving requests.')
One simple way to overcome this would be to modify the add_view function in Flask to avoid this behaviour. I first want to understand why would you avoid a setup method like creating a view or in my case a resource after the first request is called?
It is to avoid designs such as the one you just described. Define all your routes before hand. Then use the session or database to store whether a user has gone to one enpoint and is allowed to go to the next.
If you need routes with different parameters, then add a route with a path
argument that can dispatch based on value, rather than dynamically adding new routes during runtime.
Another point, you should never modify the code of libraries you installed. If you ever upgrade the library, your changes will get overwritten. In Flask's case, you can subclass the Flask
object and override this method instead.