Skip to content

Commit

Permalink
FEAT: Add prototype of dynamic routes in hydrus
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sameshl committed Mar 9, 2020
1 parent 969ac69 commit 9eca60d
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions hydrus/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Main route for the application"""

import logging
import sys
from os.path import dirname, abspath
# insert the ./app.py file path in the PYTHONPATH variable for imports to work
sys.path.insert(0, dirname(dirname(abspath(__file__))))

from sqlalchemy import create_engine

from flask import request
from collections import defaultdict
from gevent.pywsgi import WSGIServer
from sqlalchemy.orm import sessionmaker

Expand Down Expand Up @@ -42,8 +47,8 @@
doc_parse.insert_classes(classes, session)
doc_parse.insert_properties(properties, session)

AUTH = True
TOKEN = True
AUTH = False
TOKEN = False

if AUTH:
try:
Expand All @@ -54,6 +59,41 @@
# Create a Hydrus app
app = app_factory(API_NAME)

# global dict to store mapping of each function to be run before
# specified route.
# stores in the form {'path1': {'method1': function_before_path1_for_method1}}
before_request_funcs = defaultdict(dict)


@app.before_request
def before_request_callback():
path = request.path
method = request.method
global before_request_funcs
func = before_request_funcs.get(path, {}).get(method, None)
if func:
func()


# decorator to define logic for custom before request methods
def custom_before_request(path, method):
def wrapper(f):
global before_request_funcs
before_request_funcs[path][method] = f
return f
return wrapper


@custom_before_request('/api/MessageCollection', 'PUT')
def do_this_before_put_on_drone_collections():
print("Do something before PUT request on MessageCollection")


@custom_before_request('/api/MessageCollection', 'GET')
def do_this_before_get_on_drone_collections():
print("Do something before GET request on MessageCollection")


with set_authentication(app, AUTH):
# Use authentication for all requests
with set_token(app, TOKEN):
Expand All @@ -76,3 +116,4 @@
http_server.serve_forever()
except KeyboardInterrupt:
pass

0 comments on commit 9eca60d

Please sign in to comment.