hydrus
hydrus copied to clipboard
FEAT: Add prototype of dynamic routes in hydrus
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 code
2.method
- the HTTP verb at which the user to execute the custom code at the specified url path
After this, when the decorator is applied on a user defined function, I save these three parameters(path
, method
, and the function
on which the decorator is applied) in a global double/nested dict called before_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's before_request
decorator) which checks the global before_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:
- I have not included code for error handling, etc as I thought this was just a POC.
- I have tested it and it works locally.
- 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 #404
To test the feature locally:
- activate your virtualenv
-
python3 app.py
- Now, send a GET request to
http://localhost:8080/api/MessageCollection
endpoint and observe the console output in your terminal. - You could also try sending a PUT request to
http://localhost:8080/api/MessageCollection
endpoint with appropriate body such as
{
"MessageString": "Test string",
"@type": "Message"
}
and observe the console output.
Checklist
- [x] My branch is up-to-date with upstream/develop branch.
- [x] Everything works and tested for Python 3.5.2 and above.
Description
Change logs
There are PEP-8 warnings. Please check here
There are PEP-8 warnings. Please check here
@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?
How it handles GET request with query parameters?
I have not implemented that yet. I was just waiting for general approval on my approach. I will implement GET request with query parameters by tonight if the approach seems fine.
@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 flask.request.args
and the user can take appropriate action with the query_parameters