Skip to content

Commit

Permalink
Merge pull request #284 from ral-facilities/feature/search-api-endpoi…
Browse files Browse the repository at this point in the history
…nt-definition-#257

Define Endpoints for Search API
  • Loading branch information
MRichards99 authored Nov 19, 2021
2 parents 14dfc2a + 896d3b8 commit 9e137c6
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 12 deletions.
90 changes: 84 additions & 6 deletions datagateway_api/src/api_start_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
from datagateway_api.src.resources.non_entities.sessions_endpoints import (
session_endpoints,
)
from datagateway_api.src.resources.search_api_endpoints import (
get_files_endpoint,
get_number_count_endpoint,
get_number_count_files_endpoint,
get_search_endpoint,
get_single_endpoint,
)
from datagateway_api.src.resources.table_endpoints.table_endpoints import (
count_instrument_facility_cycles_endpoint,
count_instrument_investigation_endpoint,
Expand Down Expand Up @@ -97,40 +104,58 @@ def create_api_endpoints(flask_app, api, spec):
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()}")
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_>")
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")
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",
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(session_endpoint_resource, "/sessions")
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",
instrument_facility_cycle_resource,
"/instruments/<int:id_>/facilitycycles",
endpoint="datagateway_isis_instrument_facility_cycle",
)
spec.path(resource=instrument_facility_cycle_resource, api=api)

Expand All @@ -140,6 +165,7 @@ def create_api_endpoints(flask_app, api, spec):
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)

Expand All @@ -149,6 +175,7 @@ def create_api_endpoints(flask_app, api, spec):
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)

Expand All @@ -159,6 +186,7 @@ def create_api_endpoints(flask_app, api, spec):
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)

Expand All @@ -167,6 +195,56 @@ def create_api_endpoints(flask_app, api, spec):
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
if True:
# TODO - Use config value when new config style is implemented
search_api_extension = "search_api"
search_api_entity_endpoints = ["datasets", "documents", "instruments"]

for entity_name in search_api_entity_endpoints:
get_search_endpoint_resource = get_search_endpoint(entity_name)
api.add_resource(
get_search_endpoint_resource,
f"/{search_api_extension}/{entity_name}",
endpoint=f"search_api_get_{entity_name}",
)
spec.path(resource=get_endpoint_resource, api=api)

get_single_endpoint_resource = get_single_endpoint(entity_name)
api.add_resource(
get_single_endpoint_resource,
f"/{search_api_extension}/{entity_name}/<int:pid>",
endpoint=f"search_api_get_single_{entity_name}",
)
spec.path(resource=get_single_endpoint_resource, api=api)

get_number_count_endpoint_resource = get_number_count_endpoint(entity_name)
api.add_resource(
get_number_count_endpoint_resource,
f"/{search_api_extension}/{entity_name}/count",
endpoint=f"search_api_count_{entity_name}",
)
spec.path(resource=get_number_count_endpoint_resource, api=api)

get_files_endpoint_resource = get_files_endpoint("datasets")
api.add_resource(
get_files_endpoint_resource,
f"/{search_api_extension}/datasets/<int:pid>/files",
endpoint="search_api_get_dataset_files",
)
spec.path(resource=get_files_endpoint_resource, api=api)

get_number_count_files_endpoint_resource = get_number_count_files_endpoint(
"datasets",
)
api.add_resource(
get_number_count_files_endpoint_resource,
f"/{search_api_extension}/datasets/<int:pid>/files/count",
endpoint="search_api_count_dataset_files",
)
spec.path(resource=get_number_count_files_endpoint_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
13 changes: 7 additions & 6 deletions datagateway_api/src/resources/search_api_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from flask_restful import Resource


# TODO - Might need kwargs on get_endpoint(), get_id_endpoint(), get_count_endpoint(),
# get_files_endpoint(), get_count_files_endpoint() for client handling?
def get_endpoint(name):
# TODO - Might need kwargs on get_search_endpoint(), get_single_endpoint(),
# get_number_count_endpoint(), get_files_endpoint(), get_number_count_files_endpoint()
# for client handling?
def get_search_endpoint(name):
"""
TODO - Add docstring
"""
Expand All @@ -30,7 +31,7 @@ def get(self):
return Endpoint


def get_id_endpoint(name):
def get_single_endpoint(name):
"""
TODO - Add docstring
"""
Expand All @@ -46,7 +47,7 @@ def get(self, pid):
return EndpointWithID


def get_count_endpoint(name):
def get_number_count_endpoint(name):
"""
TODO - Add docstring
"""
Expand Down Expand Up @@ -78,7 +79,7 @@ def get(self, pid):
return FilesEndpoint


def get_count_files_endpoint(name):
def get_number_count_files_endpoint(name):
"""
TODO - Add docstring
"""
Expand Down
8 changes: 8 additions & 0 deletions datagateway_api/src/swagger/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11015,5 +11015,13 @@ paths:
summary: Ping API connection method
tags:
- Ping
/search_api/datasets/{pid}: {}
/search_api/datasets/count: {}
/search_api/documents/{pid}: {}
/search_api/documents/count: {}
/search_api/instruments/{pid}: {}
/search_api/instruments/count: {}
/search_api/datasets/{pid}/files: {}
/search_api/datasets/{pid}/files/count: {}
security:
- session_id: []
45 changes: 45 additions & 0 deletions test/test_endpoint_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,51 @@ def test_entity_endpoints(self, flask_test_app, endpoint_ending, expected_method
["GET"],
id="count ISIS investigations",
),
pytest.param(
"/search_api/datasets", ["GET"], id="Search API search datasets",
),
pytest.param(
"/search_api/documents", ["GET"], id="Search API search documents",
),
pytest.param(
"/search_api/instruments", ["GET"], id="Search API search instruments",
),
pytest.param(
"/search_api/datasets/<int:pid>",
["GET"],
id="Search API get single dataset",
),
pytest.param(
"/search_api/documents/<int:pid>",
["GET"],
id="Search API get single document",
),
pytest.param(
"/search_api/instruments/<int:pid>",
["GET"],
id="Search API get single instrument",
),
pytest.param(
"/search_api/datasets/count", ["GET"], id="Search API dataset count",
),
pytest.param(
"/search_api/documents/count", ["GET"], id="Search API document count",
),
pytest.param(
"/search_api/instruments/count",
["GET"],
id="Search API instrument count",
),
pytest.param(
"/search_api/datasets/<int:pid>/files",
["GET"],
id="Search API get dataset files",
),
pytest.param(
"/search_api/datasets/<int:pid>/files/count",
["GET"],
id="Search API dataset files count",
),
],
)
def test_non_entity_endpoints(
Expand Down

0 comments on commit 9e137c6

Please sign in to comment.