FEAT: Add prototype of dynamic routes in hydrus#456
FEAT: Add prototype of dynamic routes in hydrus#456sameshl wants to merge 2 commits intoHTTP-APIs:developfrom
Conversation
the functions have been defined in 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 HTTP-APIs#404
|
There are PEP-8 warnings. Please check here |
Ignored the import error E402 for flake8 to set the PYTHONPATH before all imports are finished.
@Mec-iS Fixed the PEP8 issues. For now, I have ignored the import error E402 by flake8 as I wanted to append to the PYTHONPATH variable for fixing imports. |
|
How it handles GET request with query parameters? |
I have not implemented that yet. I was just waiting for general approval on my approach. |
|
@vddesai1871 I just realized that the above implementation could also handle GET request with query parameters. The query parameters can be accessed by the user with |
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_requestwhich 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 thefunctionon which the decorator is applied) in a global double/nested dict calledbefore_request_funcsin 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_requestdecorator) which checks the globalbefore_request_funcsdict 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.pyand 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.pyhttp://localhost:8080/api/MessageCollectionendpoint and observe the console output in your terminal.http://localhost:8080/api/MessageCollectionendpoint with appropriate body such asand observe the console output.
Checklist
Description
Change logs