Skip to content

Commit

Permalink
#241: Add non-backend specific infrastructure for /ping
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Aug 13, 2021
1 parent 0c57614 commit 1cf4972
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions datagateway_api/common/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
1 change: 1 addition & 0 deletions datagateway_api/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 6 additions & 0 deletions datagateway_api/src/api_start_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions datagateway_api/src/resources/non_entities/ping_endpoint.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1cf4972

Please sign in to comment.