diff --git a/datagateway_api/common/backend.py b/datagateway_api/common/backend.py index 7177fc3c..b5bb1948 100644 --- a/datagateway_api/common/backend.py +++ b/datagateway_api/common/backend.py @@ -6,6 +6,15 @@ class Backend(ABC): Abstact base class for implementations of a backend to inherit from """ + @abstractmethod + def ping(self): + """ + Endpoint requiring no authentication to check the API is alive and does a basic + check to ensure the connection method to ICAT is working + :returns: String to tell user the API is OK + """ + pass + @abstractmethod def login(self, credentials): """ diff --git a/datagateway_api/common/constants.py b/datagateway_api/common/constants.py index bbc2ba77..aeee5726 100644 --- a/datagateway_api/common/constants.py +++ b/datagateway_api/common/constants.py @@ -6,3 +6,4 @@ class Constants: PYTHON_ICAT_DISTNCT_CONDITION = "!= null" TEST_MOD_CREATE_DATETIME = datetime(2000, 1, 1, tzinfo=tzlocal()) + PING_OK_RESPONSE = "DataGateway API OK" diff --git a/datagateway_api/src/api_start_utils.py b/datagateway_api/src/api_start_utils.py index 25cbe69a..01c4d81c 100644 --- a/datagateway_api/src/api_start_utils.py +++ b/datagateway_api/src/api_start_utils.py @@ -18,6 +18,7 @@ get_id_endpoint, ) from datagateway_api.src.resources.entities.entity_endpoint_dict import endpoints +from datagateway_api.src.resources.non_entities.ping_endpoint import ping_endpoint from datagateway_api.src.resources.non_entities.sessions_endpoints import ( session_endpoints, ) @@ -161,6 +162,11 @@ def create_api_endpoints(flask_app, api, spec): ) spec.path(resource=count_instrument_investigation_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) + def openapi_config(spec): # Reorder paths (e.g. get, patch, post) so openapi.yaml only changes when there's a diff --git a/datagateway_api/src/resources/non_entities/ping_endpoint.py b/datagateway_api/src/resources/non_entities/ping_endpoint.py new file mode 100644 index 00000000..5b595095 --- /dev/null +++ b/datagateway_api/src/resources/non_entities/ping_endpoint.py @@ -0,0 +1,39 @@ +from flask_restful import Resource + + +def ping_endpoint(backend, **kwargs): + """ + Generate a flask_restful Resource class using the configured backend. In main.py + these generated classes are registered with the api e.g. + `api.add_resource(get_endpoint("Datafiles", DATAFILE), "/datafiles")` + + :param backend: The backend instance used for processing requests + :type backend: :class:`DatabaseBackend` or :class:`PythonICATBackend` + :return: The generated ping endpoint class + """ + + class Ping(Resource): + def get(self): + """ + Pings the connection method to ensure the API is responsive + :return: String: A standard OK message, 200 + --- + summary: Ping API connection method + description: Pings the API's connection method to check responsiveness + tags: + - Ping + responses: + 200: + description: Success - the API is responsive on the backend configured + content: + application/json: + schema: + type: string + description: OK message + example: DataGateway API OK + 500: + description: Pinging the API's connection method has gone wrong + """ + return backend.ping(**kwargs), 200 + + return Ping