Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Add prototype of dynamic routes in hydrus #456

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

sameshl
Copy link
Member

@sameshl sameshl commented Mar 9, 2020

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

  • My branch is up-to-date with upstream/develop branch.
  • Everything works and tested for Python 3.5.2 and above.

Description

Change logs

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
@Mec-iS
Copy link
Contributor

Mec-iS commented Mar 9, 2020

There are PEP-8 warnings. Please check here

@sameshl sameshl closed this Mar 9, 2020
@sameshl sameshl reopened this Mar 9, 2020
Ignored the import error E402 for flake8 to set the PYTHONPATH before
all imports are finished.
@sameshl
Copy link
Member Author

sameshl commented Mar 9, 2020

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.

@vddesai1871
Copy link
Contributor

How it handles GET request with query parameters?

@sameshl
Copy link
Member Author

sameshl commented Mar 16, 2020

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
Copy link
Member Author

sameshl commented Mar 17, 2020

@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

@Mec-iS Mec-iS closed this Feb 11, 2021
@Mec-iS Mec-iS deleted the branch HTTP-APIs:develop February 11, 2021 14:20
@Mec-iS Mec-iS reopened this Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants