hydrus icon indicating copy to clipboard operation
hydrus copied to clipboard

FEAT: Add prototype of dynamic routes in hydrus

Open sameshl opened this issue 4 years ago • 5 comments

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

sameshl avatar Mar 09 '20 14:03 sameshl

There are PEP-8 warnings. Please check here

Mec-iS avatar Mar 09 '20 14:03 Mec-iS

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.

sameshl avatar Mar 09 '20 15:03 sameshl

How it handles GET request with query parameters?

vddesai1871 avatar Mar 16 '20 10:03 vddesai1871

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.

sameshl avatar Mar 16 '20 10:03 sameshl

@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

sameshl avatar Mar 17 '20 04:03 sameshl