Skip to content

Commit

Permalink
Merge pull request #285 from ral-facilities/add-datagateway-and-panos…
Browse files Browse the repository at this point in the history
…c-modes-#256

Add DataGateway and PaNOSC Modes
  • Loading branch information
VKTB authored Nov 22, 2021
2 parents fd53e88 + c068bf3 commit 5fd00c4
Show file tree
Hide file tree
Showing 19 changed files with 540 additions and 359 deletions.
42 changes: 26 additions & 16 deletions datagateway_api/config.json.example
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
{
"backend": "db",
"client_cache_size": 5,
"client_pool_init_size": 2,
"client_pool_max_size": 5,
"db_url": "mysql+pymysql://icatdbuser:icatdbuserpw@localhost:3306/icatdb",
"flask_reloader": false,
"icat_url": "https://localhost:8181",
"icat_check_cert": false,
"log_level": "WARN",
"log_location": "/home/runner/work/datagateway-api/datagateway-api/logs.log",
"debug_mode": false,
"generate_swagger": false,
"host": "127.0.0.1",
"port": "5000",
"test_user_credentials": {"username": "root", "password": "pw"},
"test_mechanism": "simple"
"datagateway_api": {
"extension": "/datagateway-api",
"backend": "db",
"client_cache_size": 5,
"client_pool_init_size": 2,
"client_pool_max_size": 5,
"db_url": "mysql+pymysql://icatdbuser:icatdbuserpw@localhost:3306/icatdb",
"icat_url": "https://localhost:8181",
"icat_check_cert": false
},
"search_api": {
"extension": "/search-api",
"icat_url": "https://localhost:8181",
"icat_check_cert": false,
"client_pool_init_size": 2,
"client_pool_max_size": 5
},
"flask_reloader": false,
"log_level": "WARN",
"log_location": "/home/runner/work/datagateway-api/datagateway-api/logs.log",
"debug_mode": false,
"generate_swagger": false,
"host": "127.0.0.1",
"port": "5000",
"test_user_credentials": {"username": "root", "password": "pw"},
"test_mechanism": "simple"
}
230 changes: 125 additions & 105 deletions datagateway_api/src/api_start_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
from flask_restful import Api
from flask_swagger_ui import get_swaggerui_blueprint

from datagateway_api.src.common.config import APIConfigOptions, config
from datagateway_api.src.datagateway_api.backends import create_backend
from datagateway_api.src.datagateway_api.database.helpers import db
from datagateway_api.src.common.config import config

# Only attempt to create a DataGateway API backend if the datagateway_api object
# is present in the config. This ensures that the API does not error on startup
# due to an AttributeError exception being thrown if the object is missing.
if config.datagateway_api is not None:
from datagateway_api.src.datagateway_api.backends import create_backend
from datagateway_api.src.datagateway_api.database.helpers import db # noqa: I202
from datagateway_api.src.datagateway_api.icat.icat_client_pool import create_client_pool
from datagateway_api.src.resources.entities.entity_endpoint import (
get_count_endpoint,
Expand Down Expand Up @@ -68,132 +73,147 @@ def create_app_infrastructure(flask_app):
flask_app.url_map.strict_slashes = False
api = CustomErrorHandledApi(flask_app)

try:
backend_type = flask_app.config["TEST_BACKEND"]
config.set_backend_type(backend_type)
except KeyError:
backend_type = config.get_config_value(APIConfigOptions.BACKEND)
if config.datagateway_api is not None:
try:
backend_type = flask_app.config["TEST_BACKEND"]
config.datagateway_api.set_backend_type(backend_type)
except KeyError:
backend_type = config.datagateway_api.backend

if backend_type == "db":
flask_app.config["SQLALCHEMY_DATABASE_URI"] = config.get_config_value(
APIConfigOptions.DB_URL,
)
flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(flask_app)
if backend_type == "db":
flask_app.config["SQLALCHEMY_DATABASE_URI"] = config.datagateway_api.db_url
flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(flask_app)

initialise_spec(spec)

return (api, spec)


def create_api_endpoints(flask_app, api, spec):
try:
backend_type = flask_app.config["TEST_BACKEND"]
config.set_backend_type(backend_type)
except KeyError:
backend_type = config.get_config_value(APIConfigOptions.BACKEND)

backend = create_backend(backend_type)

icat_client_pool = None
if backend_type == "python_icat":
# Create client pool
icat_client_pool = create_client_pool()

for entity_name in endpoints:
get_endpoint_resource = get_endpoint(
entity_name, endpoints[entity_name], backend, client_pool=icat_client_pool,
if config.datagateway_api is not None:
try:
backend_type = flask_app.config["TEST_BACKEND"]
config.datagateway_api.set_backend_type(backend_type)
except KeyError:
backend_type = config.datagateway_api.backend

backend = create_backend(backend_type)

icat_client_pool = None
if backend_type == "python_icat":
# Create client pool
icat_client_pool = create_client_pool()

for entity_name in endpoints:
get_endpoint_resource = get_endpoint(
entity_name,
endpoints[entity_name],
backend,
client_pool=icat_client_pool,
)
api.add_resource(
get_endpoint_resource,
f"/{entity_name.lower()}",
endpoint=f"datagateway_get_{entity_name}",
)
spec.path(resource=get_endpoint_resource, api=api)

get_id_endpoint_resource = get_id_endpoint(
entity_name,
endpoints[entity_name],
backend,
client_pool=icat_client_pool,
)
api.add_resource(
get_id_endpoint_resource,
f"/{entity_name.lower()}/<int:id_>",
endpoint=f"datagateway_get_id_{entity_name}",
)
spec.path(resource=get_id_endpoint_resource, api=api)

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

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

# Session endpoint
session_endpoint_resource = session_endpoints(
backend, client_pool=icat_client_pool,
)
api.add_resource(
get_endpoint_resource,
f"/{entity_name.lower()}",
endpoint=f"datagateway_get_{entity_name}",
session_endpoint_resource, "/sessions", endpoint="datagateway_sessions",
)
spec.path(resource=get_endpoint_resource, api=api)
spec.path(resource=session_endpoint_resource, api=api)

get_id_endpoint_resource = get_id_endpoint(
entity_name, endpoints[entity_name], backend, client_pool=icat_client_pool,
# Table specific endpoints
instrument_facility_cycle_resource = instrument_facility_cycles_endpoint(
backend, client_pool=icat_client_pool,
)
api.add_resource(
get_id_endpoint_resource,
f"/{entity_name.lower()}/<int:id_>",
endpoint=f"datagateway_get_id_{entity_name}",
instrument_facility_cycle_resource,
"/instruments/<int:id_>/facilitycycles",
endpoint="datagateway_isis_instrument_facility_cycle",
)
spec.path(resource=get_id_endpoint_resource, api=api)
spec.path(resource=instrument_facility_cycle_resource, api=api)

get_count_endpoint_resource = get_count_endpoint(
entity_name, endpoints[entity_name], backend, client_pool=icat_client_pool,
count_instrument_facility_cycle_res = count_instrument_facility_cycles_endpoint(
backend, client_pool=icat_client_pool,
)
api.add_resource(
get_count_endpoint_resource,
f"/{entity_name.lower()}/count",
endpoint=f"datagateway_count_{entity_name}",
count_instrument_facility_cycle_res,
"/instruments/<int:id_>/facilitycycles/count",
endpoint="datagateway_isis_count_instrument_facility_cycle",
)
spec.path(resource=get_count_endpoint_resource, api=api)
spec.path(resource=count_instrument_facility_cycle_res, api=api)

get_find_one_endpoint_resource = get_find_one_endpoint(
entity_name, endpoints[entity_name], backend, client_pool=icat_client_pool,
instrument_investigation_resource = instrument_investigation_endpoint(
backend, client_pool=icat_client_pool,
)
api.add_resource(
get_find_one_endpoint_resource,
f"/{entity_name.lower()}/findone",
endpoint=f"datagateway_findone_{entity_name}",
instrument_investigation_resource,
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>"
"/investigations",
endpoint="datagateway_isis_instrument_investigation",
)
spec.path(resource=get_find_one_endpoint_resource, api=api)

# Session endpoint
session_endpoint_resource = session_endpoints(backend, client_pool=icat_client_pool)
api.add_resource(
session_endpoint_resource, "/sessions", endpoint="datagateway_sessions",
)
spec.path(resource=session_endpoint_resource, api=api)

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

count_instrument_facility_cycle_res = count_instrument_facility_cycles_endpoint(
backend, client_pool=icat_client_pool,
)
api.add_resource(
count_instrument_facility_cycle_res,
"/instruments/<int:id_>/facilitycycles/count",
endpoint="datagateway_isis_count_instrument_facility_cycle",
)
spec.path(resource=count_instrument_facility_cycle_res, api=api)
spec.path(resource=instrument_investigation_resource, api=api)

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

count_instrument_investigation_resource = count_instrument_investigation_endpoint(
backend, client_pool=icat_client_pool,
)
api.add_resource(
count_instrument_investigation_resource,
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>/investigations"
"/count",
endpoint="datagateway_isis_count_instrument_investigation",
)
spec.path(resource=count_instrument_investigation_resource, api=api)
count_instrument_investigation_res = count_instrument_investigation_endpoint(
backend, client_pool=icat_client_pool,
)
api.add_resource(
count_instrument_investigation_res,
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>"
"/investigations/count",
endpoint="datagateway_isis_count_instrument_investigation",
)
spec.path(resource=count_instrument_investigation_res, api=api)

# Ping endpoint
ping_resource = ping_endpoint(backend, client_pool=icat_client_pool)
api.add_resource(ping_resource, "/ping")
spec.path(resource=ping_resource, api=api)
# Ping endpoint
ping_resource = ping_endpoint(backend, client_pool=icat_client_pool)
api.add_resource(ping_resource, "/ping")
spec.path(resource=ping_resource, api=api)

# Search API endpoints
# TODO - make conditional respect new config style when implemented
Expand Down Expand Up @@ -249,7 +269,7 @@ def create_api_endpoints(flask_app, api, spec):
def openapi_config(spec):
# Reorder paths (e.g. get, patch, post) so openapi.yaml only changes when there's a
# change to the Swagger docs, rather than changing on each startup
if config.get_config_value(APIConfigOptions.GENERATE_SWAGGER):
if config.generate_swagger:
log.debug("Reordering OpenAPI docs to alphabetical order")
for entity_data in spec._paths.values():
for endpoint_name in sorted(entity_data.keys()):
Expand Down
Loading

0 comments on commit 5fd00c4

Please sign in to comment.