Skip to content

Commit

Permalink
Merge branch 'master' into feature/configurable-backend-#125
Browse files Browse the repository at this point in the history
  • Loading branch information
louise-davies committed Jun 4, 2020
2 parents 5ae2cc8 + 05ad009 commit 3862d11
Show file tree
Hide file tree
Showing 16 changed files with 12,398 additions and 8,159 deletions.
4 changes: 3 additions & 1 deletion common/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import uuid
import sys
from common.exceptions import AuthenticationError
import datetime


class Backend(ABC):
Expand Down Expand Up @@ -218,7 +219,8 @@ class DatabaseBackend(Backend):
def login(self, credentials):
if credentials["username"] == "user" and credentials["password"] == "password":
session_id = str(uuid.uuid1())
insert_row_into_table(SESSION, SESSION(ID=session_id))
insert_row_into_table(SESSION, SESSION(ID=session_id, USERNAME="simple/root",
EXPIREDATETIME=datetime.datetime.now() + datetime.timedelta(days=1)))
return session_id
else:
raise AuthenticationError("Username and password are incorrect")
Expand Down
1 change: 1 addition & 0 deletions common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def queries_records(method):
Decorator for endpoint resources that search for a record in a table
:param method: The method for the endpoint
:return: Will return a 404, "No such record" if a MissingRecordError is caught
:return: Will return a 400, "Error message" if other expected errors are caught
"""

@wraps(method)
Expand Down
3 changes: 1 addition & 2 deletions dev-requirements.in
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Faker == 2.0.2
pyyaml == 5.1.2
Faker == 2.0.2
3 changes: 1 addition & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile '.\dev-requirements.in'
# pip-compile dev-requirements.in
#
faker==2.0.2
python-dateutil==2.8.0 # via faker
pyyaml==5.1.2
six==1.12.0 # via faker, python-dateutil
text-unidecode==1.3 # via faker
3 changes: 3 additions & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ flask_restful == 0.3.7
sqlalchemy == 1.3.8
pymysql == 0.9.3
flask-cors == 3.0.8
apispec == 3.3.0
flask-swagger-ui == 3.25.0
pyyaml == 5.1.2
7 changes: 5 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile '.\requirements.in'
# pip-compile requirements.in
#
aniso8601==8.0.0 # via flask-restful
apispec==3.3.0
click==7.0 # via flask
flask-cors==3.0.8
flask==1.1.1 # via flask-cors, flask-restful
flask-swagger-ui==3.25.0
flask==1.1.1 # via flask-cors, flask-restful, flask-swagger-ui
flask_restful==0.3.7
itsdangerous==1.1.0 # via flask
jinja2==2.10.1 # via flask
markupsafe==1.1.1 # via jinja2
pymysql==0.9.3
pytz==2019.2 # via flask-restful
pyyaml==5.1.2
six==1.12.0 # via flask-cors, flask-restful
sqlalchemy==1.3.8
werkzeug==0.16.0 # via flask
71 changes: 61 additions & 10 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Flask
from flask_cors import CORS
from flask_restful import Api
from flask_swagger_ui import get_swaggerui_blueprint

from common.config import config
from common.logger_setup import setup_logger
Expand All @@ -11,10 +12,16 @@
from src.resources.table_endpoints.table_endpoints import UsersInvestigations, UsersInvestigationsCount, \
InstrumentsFacilityCycles, InstrumentsFacilityCyclesCount, InstrumentsFacilityCyclesInvestigations, \
InstrumentsFacilityCyclesInvestigationsCount
from src.swagger.swagger_generator import swagger_gen
from common.exceptions import ApiError
from apispec import APISpec
from pathlib import Path
import json
from src.swagger.apispec_flask_restful import RestfulPlugin
from src.swagger.initialise_spec import initialise_spec

swagger_gen.write_swagger_spec()

spec = APISpec(title="DataGateway API", version="1.0", openapi_version="3.0.3",
plugins=[RestfulPlugin()], security=[{"session_id": []}])

app = Flask(__name__)
cors = CORS(app)
Expand All @@ -27,33 +34,77 @@ def handle_error(e):
return str(e), e.status_code


swaggerui_blueprint = get_swaggerui_blueprint(
"",
"/openapi.json",
config={
'app_name': "DataGateway API OpenAPI Spec"
},
)

app.register_blueprint(swaggerui_blueprint, url_prefix="/")

setup_logger()

initialise_spec(spec)

for entity_name in endpoints:
api.add_resource(get_endpoint(
entity_name, endpoints[entity_name]), f"/{entity_name.lower()}")
api.add_resource(get_id_endpoint(
entity_name, endpoints[entity_name]), f"/{entity_name.lower()}/<int:id>")
api.add_resource(get_count_endpoint(
entity_name, endpoints[entity_name]), f"/{entity_name.lower()}/count")
api.add_resource(get_find_one_endpoint(
entity_name, endpoints[entity_name]), f"/{entity_name.lower()}/findone")
get_endpoint_resource = get_endpoint(entity_name, endpoints[entity_name])
api.add_resource(get_endpoint_resource, f"/{entity_name.lower()}")
spec.path(resource=get_endpoint_resource, api=api)

get_id_endpoint_resource = get_id_endpoint(
entity_name, endpoints[entity_name])
api.add_resource(get_id_endpoint_resource,
f"/{entity_name.lower()}/<int:id>")
spec.path(resource=get_id_endpoint_resource, api=api)

get_count_endpoint_resource = get_count_endpoint(
entity_name, endpoints[entity_name])
api.add_resource(get_count_endpoint_resource,
f"/{entity_name.lower()}/count")
spec.path(resource=get_count_endpoint_resource, api=api)

get_find_one_endpoint_resource = get_find_one_endpoint(
entity_name, endpoints[entity_name])
api.add_resource(get_find_one_endpoint_resource,
f"/{entity_name.lower()}/findone")
spec.path(resource=get_find_one_endpoint_resource, api=api)


# Session endpoint
api.add_resource(Sessions, "/sessions")
spec.path(resource=Sessions, api=api)

# Table specific endpoints
api.add_resource(UsersInvestigations, "/users/<int:id>/investigations")
spec.path(resource=UsersInvestigations, api=api)
api.add_resource(UsersInvestigationsCount,
"/users/<int:id>/investigations/count")
spec.path(resource=UsersInvestigationsCount, api=api)
api.add_resource(InstrumentsFacilityCycles,
"/instruments/<int:id>/facilitycycles")
spec.path(resource=InstrumentsFacilityCycles, api=api)
api.add_resource(InstrumentsFacilityCyclesCount,
"/instruments/<int:id>/facilitycycles/count")
spec.path(resource=InstrumentsFacilityCyclesCount, api=api)
api.add_resource(InstrumentsFacilityCyclesInvestigations,
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>/investigations")
spec.path(resource=InstrumentsFacilityCyclesInvestigations, api=api)
api.add_resource(InstrumentsFacilityCyclesInvestigationsCount,
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>/investigations/count")
spec.path(resource=InstrumentsFacilityCyclesInvestigationsCount, api=api)

openapi_spec_path = Path(__file__).parent / "swagger/openapi.yaml"
with open(openapi_spec_path, "w") as f:
f.write(spec.to_yaml())


@app.route("/openapi.json")
def specs():
resp = app.make_response(json.dumps(spec.to_dict(), indent=2))
resp.mimetype = "application/json"
return resp


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 3862d11

Please sign in to comment.