Skip to content

Commit

Permalink
Merge branch 'feature/search-api-endpoint-definition-#257' into respe…
Browse files Browse the repository at this point in the history
…ct-config-for-search-api-endpoints-#283
  • Loading branch information
Viktor Bozhinov committed Nov 15, 2021
1 parent c23b0e5 commit 0b685e8
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 12 deletions.
86 changes: 80 additions & 6 deletions datagateway_api/src/api_start_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 @@ -105,7 +112,11 @@ def create_api_endpoints(flask_app, api, spec):
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(
Expand All @@ -115,7 +126,9 @@ def create_api_endpoints(flask_app, api, spec):
client_pool=icat_client_pool,
)
api.add_resource(
get_id_endpoint_resource, f"/{entity_name.lower()}/<int:id_>",
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)

Expand All @@ -126,7 +139,9 @@ def create_api_endpoints(flask_app, api, spec):
client_pool=icat_client_pool,
)
api.add_resource(
get_count_endpoint_resource, f"/{entity_name.lower()}/count",
get_count_endpoint_resource,
f"/{entity_name.lower()}/count",
endpoint=f"datagateway_count_{entity_name}",
)
spec.path(resource=get_count_endpoint_resource, api=api)

Expand All @@ -137,23 +152,29 @@ def create_api_endpoints(flask_app, api, spec):
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 @@ -163,6 +184,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 @@ -173,6 +195,7 @@ def create_api_endpoints(flask_app, api, spec):
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 @@ -183,6 +206,7 @@ def create_api_endpoints(flask_app, api, spec):
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)

Expand All @@ -191,6 +215,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_search_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 @@ -11013,5 +11013,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: []
1 change: 1 addition & 0 deletions test/datagateway_api/icat/filters/test_order_filter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from typing_extensions import OrderedDict


from datagateway_api.src.common.exceptions import FilterError
from datagateway_api.src.datagateway_api.filter_order_handler import FilterOrderHandler
from datagateway_api.src.datagateway_api.icat.filters import PythonICATOrderFilter
Expand Down
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 0b685e8

Please sign in to comment.