-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from ral-facilities/feature/ping-endpoint-#241
Ping Endpoint
- Loading branch information
Showing
10 changed files
with
157 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
datagateway_api/src/resources/non_entities/ping_endpoint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from sqlalchemy.exc import SQLAlchemyError | ||
|
||
from datagateway_api.common.backends import create_backend | ||
from datagateway_api.common.constants import Constants | ||
from datagateway_api.common.exceptions import DatabaseError | ||
|
||
|
||
class TestICATPing: | ||
def test_valid_ping(self, flask_test_app_db): | ||
test_response = flask_test_app_db.get("/ping") | ||
|
||
assert test_response.json == Constants.PING_OK_RESPONSE | ||
|
||
def test_invalid_ping(self): | ||
with patch( | ||
"sqlalchemy.engine.reflection.Inspector.get_table_names", | ||
side_effect=SQLAlchemyError("Mocked Exception"), | ||
): | ||
with pytest.raises(DatabaseError): | ||
backend = create_backend("db") | ||
backend.ping() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from unittest.mock import patch | ||
|
||
from icat.exception import ICATError | ||
import pytest | ||
|
||
from datagateway_api.common.backends import create_backend | ||
from datagateway_api.common.constants import Constants | ||
from datagateway_api.common.exceptions import PythonICATError | ||
from datagateway_api.common.icat.icat_client_pool import create_client_pool | ||
|
||
|
||
class TestICATPing: | ||
def test_valid_ping(self, flask_test_app_icat): | ||
test_response = flask_test_app_icat.get("/ping") | ||
|
||
assert test_response.json == Constants.PING_OK_RESPONSE | ||
|
||
def test_invalid_ping(self): | ||
with patch( | ||
"icat.client.Client.getEntityNames", | ||
side_effect=ICATError("Mocked Exception"), | ||
): | ||
with pytest.raises(PythonICATError): | ||
backend = create_backend("python_icat") | ||
client_pool = create_client_pool() | ||
backend.ping(client_pool=client_pool) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters