Skip to content

Commit

Permalink
#150: Checkpoint in making backend configurable for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 2, 2020
1 parent 64f9981 commit 9532d91
Show file tree
Hide file tree
Showing 5 changed files with 437 additions and 366 deletions.
67 changes: 46 additions & 21 deletions datagateway_api/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
get_id_endpoint,
)
from datagateway_api.src.resources.entities.entity_map import endpoints
from datagateway_api.src.resources.non_entities.sessions_endpoints import Sessions
from datagateway_api.src.resources.non_entities.sessions_endpoints import (
session_endpoints,
)
from datagateway_api.src.resources.table_endpoints.table_endpoints import (
InstrumentsFacilityCycles,
InstrumentsFacilityCyclesCount,
InstrumentsFacilityCyclesInvestigations,
InstrumentsFacilityCyclesInvestigationsCount,
count_instrument_facility_cycles_endpoint,
instrument_facility_cycles_endpoint,
instrument_investigation_endpoint,
count_instrument_investigation_endpoint,
)
from datagateway_api.src.swagger.apispec_flask_restful import RestfulPlugin
from datagateway_api.src.swagger.initialise_spec import initialise_spec
Expand Down Expand Up @@ -68,56 +70,79 @@ def handle_error(e):
def create_api_endpoints(app, api, spec):
try:
backend_type = app.config["TEST_BACKEND"]
print(f"test backend: {backend_type}")
except KeyError:
backend_type = config.get_backend_type()
print(f"config backend: {backend_type}")

# TODO - Add :param backend: to the endpoint functions
backend = create_backend(backend_type)
print(f"Backend: {backend}, Type: {type(backend)}")

for entity_name in endpoints:
get_endpoint_resource = get_endpoint(entity_name, endpoints[entity_name])
get_endpoint_resource = get_endpoint(
entity_name, endpoints[entity_name], backend
)
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])
get_id_endpoint_resource = get_id_endpoint(
entity_name, endpoints[entity_name], backend
)
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],
entity_name, endpoints[entity_name], backend,
)
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],
entity_name, endpoints[entity_name], backend,
)
api.add_resource(
get_find_one_endpoint_resource, f"/{entity_name.lower()}/findone"
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)
session_endpoint_resource = session_endpoints(backend)
api.add_resource(session_endpoint_resource, "/sessions")
# spec.path(resource=session_endpoint_resource, api=api)

# Table specific endpoints
api.add_resource(InstrumentsFacilityCycles, "/instruments/<int:id_>/facilitycycles")
spec.path(resource=InstrumentsFacilityCycles, api=api)
instrument_facility_cycle_resource = instrument_facility_cycles_endpoint(backend)
api.add_resource(
instrument_facility_cycle_resource, "/instruments/<int:id_>/facilitycycles"
)
# spec.path(resource=instrument_facility_cycle_resource, api=api)

count_instrument_facility_cycle_resource = count_instrument_facility_cycles_endpoint(
backend
)
api.add_resource(
InstrumentsFacilityCyclesCount, "/instruments/<int:id_>/facilitycycles/count",
count_instrument_facility_cycle_resource,
"/instruments/<int:id_>/facilitycycles/count",
)
spec.path(resource=InstrumentsFacilityCyclesCount, api=api)
# spec.path(resource=count_instrument_facility_cycle_resource, api=api)

instrument_investigation_resource = instrument_investigation_endpoint(backend)
api.add_resource(
InstrumentsFacilityCyclesInvestigations,
instrument_investigation_resource,
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>/investigations",
)
spec.path(resource=InstrumentsFacilityCyclesInvestigations, api=api)
# spec.path(resource=instrument_investigation_resource, api=api)

count_instrument_investigation_resource = count_instrument_investigation_endpoint(
backend
)
api.add_resource(
InstrumentsFacilityCyclesInvestigationsCount,
count_instrument_investigation_resource,
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>/investigations"
"/count",
)
spec.path(resource=InstrumentsFacilityCyclesInvestigationsCount, api=api)
# spec.path(resource=count_instrument_investigation_resource, api=api)


def openapi_config(spec):
Expand All @@ -141,7 +166,7 @@ def specs():


if __name__ == "__main__":
api, spec = create_app_infrastructure()
api, spec = create_app_infrastructure(app)
create_api_endpoints(app, api, spec)
openapi_config(spec)
app.run(
Expand Down
10 changes: 5 additions & 5 deletions datagateway_api/src/resources/entities/entity_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
get_session_id_from_auth_header,
)

backend = create_backend(config.get_backend_type())
# backend = create_backend(config.get_backend_type())


def get_endpoint(name, entity_type):
def get_endpoint(name, entity_type, backend):
"""
Given an entity name generate a flask_restful Resource class.
In main.py these generated classes are registered with the api e.g
Expand Down Expand Up @@ -159,7 +159,7 @@ def patch(self):
return Endpoint


def get_id_endpoint(name, entity_type):
def get_id_endpoint(name, entity_type, backend):
"""
Given an entity name generate a flask_restful Resource class.
In main.py these generated classes are registered with the api e.g
Expand Down Expand Up @@ -289,7 +289,7 @@ def patch(self, id_):
return EndpointWithID


def get_count_endpoint(name, entity_type):
def get_count_endpoint(name, entity_type, backend):
"""
Given an entity name generate a flask_restful Resource class.
In main.py these generated classes are registered with the api e.g
Expand Down Expand Up @@ -342,7 +342,7 @@ def get(self):
return CountEndpoint


def get_find_one_endpoint(name, entity_type):
def get_find_one_endpoint(name, entity_type, backend):
"""
Given an entity name generate a flask_restful Resource class.
In main.py these generated classes are registered with the api e.g
Expand Down
Loading

0 comments on commit 9532d91

Please sign in to comment.