diff --git a/src/linkeddata_api/domain/viewer/resource/__init__.py b/src/linkeddata_api/domain/viewer/resource/__init__.py index 0f5c7c8..42dfece 100644 --- a/src/linkeddata_api/domain/viewer/resource/__init__.py +++ b/src/linkeddata_api/domain/viewer/resource/__init__.py @@ -9,7 +9,7 @@ from . import json -def _handle_json_response(uri: str, sparql_endpoint: str) -> domain.schema.Resource: +def _handle_json_response(uri: str, sparql_endpoint: str) -> str: try: result = json.get(uri, sparql_endpoint) except (RequestError, SPARQLNotFoundError, SPARQLResultJSONError) as err: @@ -33,7 +33,7 @@ def _handle_rdf_response( graph.parse(data=response.text, format=format_) if len(graph) == 0: - return "Resource not found", 404 + raise SPARQLNotFoundError(f"Resource with URI {uri} not found.") if not include_incoming_relationships: graph.remove((None, None, URIRef(uri))) diff --git a/src/linkeddata_api/views/api_v1/viewer/resource.py b/src/linkeddata_api/views/api_v1/viewer/resource.py index 47a88a5..0b30f72 100644 --- a/src/linkeddata_api/views/api_v1/viewer/resource.py +++ b/src/linkeddata_api/views/api_v1/viewer/resource.py @@ -29,6 +29,10 @@ def get_resource(): True if include_incoming_relationships == "true" else False ) + if uri is None or sparql_endpoint is None: + err_msg = "Required query parameters 'uri' or 'sparql_endpoint' was not provided." + raise HTTPException(err_msg, Response(err_msg, 404)) + logger.info( """ GET /viewer/resource @@ -65,4 +69,8 @@ def get_resource(): response=Response(str(err), mimetype="text/plain", status=500), ) from err - return Response(result, mimetype=format_) + return Response( + result, + mimetype=format_, + headers={"cache-control": "max-age=600, s-maxage=3600"}, + ) diff --git a/tests/api_v1/resource/test_resource_describe.py b/tests/api_v1/resource/test_resource_describe.py index b08a62b..98e7905 100644 --- a/tests/api_v1/resource/test_resource_describe.py +++ b/tests/api_v1/resource/test_resource_describe.py @@ -8,7 +8,7 @@ @pytest.fixture def url() -> str: - return "/api/v1.0/resource" + return "/api/v1.0/viewer/resource" value = """ @@ -56,7 +56,7 @@ def url() -> str: "text/turtle", "text/turtle", "https://linked.data.gov.au/def/nrm", - "dawe_vocabs_core", + "https://graphdb.tern.org.au/repositories/dawe_vocabs_core", "false", value, 22, @@ -68,10 +68,10 @@ def url() -> str: "text/turtle", "text/html", "https://linked.data.gov.au/def/nrm/not-exist", - "dawe_vocabs_core", + "https://graphdb.tern.org.au/repositories/dawe_vocabs_core", "false", "", - 22, + None, ), # RDF4J repository does not exist ( @@ -80,10 +80,10 @@ def url() -> str: "text/turtle", "text/html", "https://linked.data.gov.au/def/nrm", - "dawe_vocabs_core-not-exist", + "https://graphdb.tern.org.au/repositories/dawe_vocabs_core-not-exist", "false", "", - 22, + None, ), # Include incoming relationships ( @@ -92,10 +92,10 @@ def url() -> str: "text/turtle", "text/turtle", "https://linked.data.gov.au/def/nrm", - "dawe_vocabs_core", + "https://graphdb.tern.org.au/repositories/dawe_vocabs_core", "true", value, - 23, + 3179, ), # No accepted format, default to text/turtle ( @@ -104,7 +104,7 @@ def url() -> str: "", "text/turtle", "https://linked.data.gov.au/def/nrm", - "dawe_vocabs_core", + "https://graphdb.tern.org.au/repositories/dawe_vocabs_core", "false", value, 22, @@ -134,11 +134,11 @@ def test_describe( response: TestResponse = client.get( url, query_string={ - "repository_id": repository_id, + "sparql_endpoint": repository_id, "uri": uri, "include_incoming_relationships": include_incoming_relationships, + "format": accept_format, }, - headers={"accept": accept_format}, ) assert response.status_code == response_status_code assert expected_format in response.headers.get("content-type")