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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 56 additions & 14 deletions hydrus/app.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
"""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
# ignoring import error by flake 8 temporarily
# for setting PYTHONPATH before imports
from sqlalchemy import create_engine # noqa: E402
from flask import request # noqa: E402
from collections import defaultdict # noqa: E402
from gevent.pywsgi import WSGIServer # noqa: E402
from sqlalchemy.orm import sessionmaker # noqa: E402

from gevent.pywsgi import WSGIServer
from sqlalchemy.orm import sessionmaker

from hydrus.app_factory import app_factory
from hydrus.app_factory import app_factory # noqa: E402
from hydrus.conf import (
HYDRUS_SERVER_URL, API_NAME, DB_URL, APIDOC_OBJ, PORT, DEBUG)
from hydrus.data import doc_parse
from hydrus.data.db_models import Base
from hydrus.data.exceptions import UserExists
from hydrus.data.user import add_user
from hydra_python_core import doc_maker
HYDRUS_SERVER_URL, API_NAME, DB_URL, APIDOC_OBJ, PORT, DEBUG) # noqa: E402
from hydrus.data import doc_parse # noqa: E402
from hydrus.data.db_models import Base # noqa: E402
from hydrus.data.exceptions import UserExists # noqa: E402
from hydrus.data.user import add_user # noqa: E402
from hydra_python_core import doc_maker # noqa: E402
from hydrus.utils import (
set_session, set_doc, set_hydrus_server_url,
set_token, set_api_name, set_authentication)
set_token, set_api_name, set_authentication) # noqa: E402

logger = logging.getLogger(__file__)

Expand All @@ -42,8 +49,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 +61,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 Down