FEAT: Add prototype of dynamic routes in hydrus #456
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a POC of having dynamic routes in hydrus. For the prototype I am showing only how a custom before request function can be added for a specific route, this can easily be extended to for after request function also. I have used an approach with decorators.
I am exposing a decorator called
custom_before_request
which will take two arguments:1.
path
- url route at which the user wants to execute the custom before request code2.
method
- the HTTP verb at which the user to execute the custom code at the specified url pathAfter this, when the decorator is applied on a user defined function, I save these three parameters(
path
,method
, and thefunction
on which the decorator is applied) in a global double/nested dict calledbefore_request_funcs
in the form{'path1':{'method1': function_to_be_called_at_this_path_and_method}}
.In the hydrus backend I have declared a function called
before_request_callback
(which is decorated by flask'sbefore_request
decorator) which checks the globalbefore_request_funcs
dict for any function to be called by checking the path and method of the current request and then calls the appropriate function.Let me know what you think of this implementation and how it could be improved.
PS:
app.py
and not been imported from any other file is to avoid the circular dependency issues in flask at the prototype stage.I will fix this issue once the prototype is accepted
related to #404
To test the feature locally:
python3 app.py
http://localhost:8080/api/MessageCollection
endpoint and observe the console output in your terminal.http://localhost:8080/api/MessageCollection
endpoint with appropriate body such asand observe the console output.
Checklist
Description
Change logs