Skip to content

Commit

Permalink
refactored code and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mwigham committed Jan 22, 2024
1 parent b4d6f21 commit 4d27b40
Show file tree
Hide file tree
Showing 22 changed files with 952 additions and 969 deletions.
2 changes: 0 additions & 2 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ APP_PORT: 5309
APP_VERSION: "v1.2"
DEBUG: True

STORAGE_BASE_URL: "http://flexstore.beng.example.com:1234"

ENABLED_ENDPOINTS: # allow all by default
- "resource"
- "dataset"
Expand Down
359 changes: 0 additions & 359 deletions resource/data_catalog_unit_test.ttl

This file was deleted.

2 changes: 1 addition & 1 deletion src/apis/dataset/DataCatalogLODHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class DataCatalogLODHandler:
"""Handles requests from the beng-lod server for data catalogs, datasets, datadownloads.
The only data model/ontology this data is available in is schema.org.
The only data model/ontology this data is available in is schema.org.
The source for the data catalog is a (Turtle) file in /resource.
"""

Expand Down
144 changes: 86 additions & 58 deletions src/apis/dataset/dataset_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@
from apis.dataset.DataCatalogLODHandler import DataCatalogLODHandler
from util.mime_type_util import MimeType
from models.ResourceURILevel import ResourceURILevel
from util.ld_util import (
generate_lod_resource_uri,
get_lod_resource_from_rdf_store,
json_ld_structured_data_for_resource,
json_header_from_rdf_graph,
json_iri_iri_from_rdf_graph,
json_iri_lit_from_rdf_graph,
json_iri_bnode_from_rdf_graph,
)
import util.ld_util
from util.APIUtil import APIUtil


Expand All @@ -35,21 +27,29 @@ def _get_lod_view_resource(
:param resource_url: The URI for the resource.
"""
logger.info(f"Getting RDF for resource {resource_url} from triple store.")
rdf_graph = get_lod_resource_from_rdf_store(
rdf_graph = util.ld_util.get_lod_resource_from_rdf_store(
resource_url, sparql_endpoint, nisv_organisation_uri
)
if rdf_graph:
if rdf_graph != None:
logger.info(f"Generating HTML page for {resource_url}.")
return render_template(
"resource.html",
resource_uri=resource_url,
structured_data=json_ld_structured_data_for_resource(
structured_data=util.ld_util.json_ld_structured_data_for_resource(
rdf_graph, resource_url
),
json_header=util.ld_util.json_header_from_rdf_graph(
rdf_graph, resource_url
),
json_iri_iri=util.ld_util.json_iri_iri_from_rdf_graph(
rdf_graph, resource_url
),
json_iri_lit=util.ld_util.json_iri_lit_from_rdf_graph(
rdf_graph, resource_url
),
json_iri_bnode=util.ld_util.json_iri_bnode_from_rdf_graph(
rdf_graph, resource_url
),
json_header=json_header_from_rdf_graph(rdf_graph, resource_url),
json_iri_iri=json_iri_iri_from_rdf_graph(rdf_graph, resource_url),
json_iri_lit=json_iri_lit_from_rdf_graph(rdf_graph, resource_url),
json_iri_bnode=json_iri_bnode_from_rdf_graph(rdf_graph, resource_url),
nisv_sparql_endpoint=sparql_endpoint,
)
return None
Expand Down Expand Up @@ -80,7 +80,7 @@ def get(self, number=None):
"""Get the RDF for the Dataset, including its DataDownloads.
All triples for the Dataset and its DataDownloads are included.
"""
dataset_uri = generate_lod_resource_uri(
dataset_uri = util.ld_util.generate_lod_resource_uri(
ResourceURILevel.DATASET, number, current_app.config["BENG_DATA_DOMAIN"]
)
# check if resource exists
Expand All @@ -97,7 +97,7 @@ def get(self, number=None):
best_match = request.accept_mimetypes.best_match(
lod_server_supported_mime_types
)
mime_type = MimeType.JSON_LD
mime_type = None
if best_match is not None:
mime_type = MimeType(best_match)

Expand All @@ -110,23 +110,34 @@ def get(self, number=None):
current_app.config.get("URI_NISV_ORGANISATION"),
)
if html_page:
return APIUtil.toSuccessResponse(html_page)
return Response(html_page, mimetype=mime_type.value)
else:
logger.error(f"Could not generate the HTML page for {dataset_uri}.")
return APIUtil.toErrorResponse(
"internal_server_error",
"Could not generate an HTML view for this resource",
)

# other content formats
logger.info(f"Get the serialization for dataset {dataset_uri}.")
res_string = DataCatalogLODHandler(current_app.config).get_dataset(
dataset_uri, mime_format=mime_type.to_ld_format()
if mime_type:
# other content formats
logger.info(f"Get the serialization for dataset {dataset_uri}.")
res_string = DataCatalogLODHandler(current_app.config).get_dataset(
dataset_uri, mime_format=mime_type.to_ld_format()
)
if res_string:
return Response(res_string, mimetype=mime_type.value)
logger.error(
f"Could not fetch the serialization for dataset {dataset_uri}."
)
return APIUtil.toErrorResponse(
"bad_request", "Invalid URI or return format"
)

logger.error("Not a proper mime type in the request.")
return APIUtil.toErrorResponse(
"internal_server_error",
"Error: No mime type detected...",
)
if res_string:
return Response(res_string, mimetype=mime_type.value)
logger.error(f"Could not fetch the serialization for dataset {dataset_uri}.")
return APIUtil.toErrorResponse("bad_request", "Invalid URI or return format")

def is_dataset(self, dataset_uri: str) -> bool:
return DataCatalogLODHandler(current_app.config).is_dataset(dataset_uri)
Expand Down Expand Up @@ -160,7 +171,7 @@ def get(self, number=None):
"""Get the RDF for the DataCatalog, including its Datasets.
All triples describing the DataCatalog and its Datasets are included.
"""
data_catalog_uri = generate_lod_resource_uri(
data_catalog_uri = util.ld_util.generate_lod_resource_uri(
ResourceURILevel.DATACATALOG,
number,
current_app.config["BENG_DATA_DOMAIN"],
Expand All @@ -180,7 +191,7 @@ def get(self, number=None):
best_match = request.accept_mimetypes.best_match(
lod_server_supported_mime_types
)
mime_type = MimeType.JSON_LD
mime_type = None
if best_match is not None:
mime_type = MimeType(best_match)

Expand All @@ -193,7 +204,7 @@ def get(self, number=None):
current_app.config.get("URI_NISV_ORGANISATION"),
)
if html_page:
return APIUtil.toSuccessResponse(html_page)
return Response(html_page, mimetype=mime_type.value)
else:
logger.error(
f"Could not generate proper HTML page for data catalog: {data_catalog_uri}."
Expand All @@ -202,20 +213,28 @@ def get(self, number=None):
"internal_server_error",
"Could not generate an HTML view for this resource",
)
if mime_type:
# other mime types
logger.info(
f"Getting the RDF in proper serialization format for data catalog: {data_catalog_uri}."
)
res_string = DataCatalogLODHandler(current_app.config).get_data_catalog(
data_catalog_uri, mime_format=mime_type.to_ld_format()
)
if res_string:
return Response(res_string, mimetype=mime_type.value)
logger.error(
f"Error in fetching the serialization for data catalog: {data_catalog_uri}."
)
return APIUtil.toErrorResponse(
"bad_request", "Invalid URI or return format"
)

# other mime types
logger.info(
f"Getting the RDF in proper serialization format for data catalog: {data_catalog_uri}."
)
res_string = DataCatalogLODHandler(current_app.config).get_data_catalog(
data_catalog_uri, mime_format=mime_type.to_ld_format()
logger.error("Not a proper mime type in the request.")
return APIUtil.toErrorResponse(
"internal_server_error",
"Error: No mime type detected...",
)
if res_string:
return Response(res_string, mimetype=mime_type.value)
logger.error(
f"Error in fetching the serialization for data catalog: {data_catalog_uri}."
)
return APIUtil.toErrorResponse("bad_request", "Invalid URI or return format")

def is_data_catalog(self, data_catalog_uri: str) -> bool:
return DataCatalogLODHandler(current_app.config).is_data_catalog(
Expand Down Expand Up @@ -251,7 +270,7 @@ class LODDataDownloadAPI(LODDataAPI):
@api.produces([mt.value for mt in MimeType])
def get(self, number=None):
"""Get the RDF for the DataDownload."""
data_download_uri = generate_lod_resource_uri(
data_download_uri = util.ld_util.generate_lod_resource_uri(
ResourceURILevel.DATADOWNLOAD,
number,
current_app.config["BENG_DATA_DOMAIN"],
Expand All @@ -268,7 +287,7 @@ def get(self, number=None):
best_match = request.accept_mimetypes.best_match(
lod_server_supported_mime_types
)
mime_type = MimeType.JSON_LD
mime_type = None
if best_match is not None:
mime_type = MimeType(best_match)

Expand All @@ -281,29 +300,38 @@ def get(self, number=None):
current_app.config.get("URI_NISV_ORGANISATION"),
)
if html_page:
return make_response(html_page, 200)
return Response(html_page, mimetype=mime_type.value)
else:
logger.info(
logger.error(
f"Could not generate HTML page for data download: {data_download_uri}."
)
return APIUtil.toErrorResponse(
"internal_server_error",
"Could not generate an HTML view for this resource",
)

# other return formats
logger.info(
f"Getting the RDF in proper serialization format for data download: {data_download_uri}."
)
res_string = DataCatalogLODHandler(current_app.config).get_data_download(
data_download_uri, mime_format=mime_type.to_ld_format()
)
if res_string:
return Response(res_string, mimetype=mime_type.value)
logger.error(
f"Error in fetching the serialization for data download: {data_download_uri}."
if mime_type:
# other return formats
logger.info(
f"Getting the RDF in proper serialization format for data download: {data_download_uri}."
)
res_string = DataCatalogLODHandler(current_app.config).get_data_download(
data_download_uri, mime_format=mime_type.to_ld_format()
)
if res_string:
return Response(res_string, mimetype=mime_type.value)
logger.error(
f"Error in fetching the serialization for data download: {data_download_uri}."
)
return APIUtil.toErrorResponse(
"bad_request", "Invalid URI or return format"
)

logger.error("Not a proper mime type in the request.")
return APIUtil.toErrorResponse(
"internal_server_error",
"Error: No mime type detected...",
)
return APIUtil.toErrorResponse("bad_request", "Invalid URI or return format")

def is_data_download(self, data_download_uri: str) -> bool:
return DataCatalogLODHandler(current_app.config).is_data_download(
Expand Down
57 changes: 34 additions & 23 deletions src/apis/gtaa/gtaa_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
from flask_restx import Namespace, Resource
from util.mime_type_util import MimeType
from util.APIUtil import APIUtil
from util.ld_util import (
get_lod_resource_from_rdf_store,
json_ld_structured_data_for_resource,
json_header_from_rdf_graph,
json_iri_iri_from_rdf_graph,
json_iri_lit_from_rdf_graph,
json_iri_bnode_from_rdf_graph,
)
import util.ld_util


logger = logging.getLogger()
Expand Down Expand Up @@ -45,7 +38,7 @@ def get(self, identifier):
best_match = request.accept_mimetypes.best_match(
lod_server_supported_mime_types
)
mime_type = MimeType.JSON_LD
mime_type = None
if best_match is not None:
mime_type = MimeType(best_match)

Expand All @@ -70,14 +63,26 @@ def get(self, identifier):
logger.info(
f"Getting the RDF in proper serialization format for GTAA resource: {gtaa_uri}."
)
return self._get_lod_gtaa(
data = self._get_lod_gtaa(
gtaa_uri,
mime_type,
current_app.config.get("SPARQL_ENDPOINT"),
current_app.config.get("URI_NISV_ORGANISATION"),
)
if data:
headers = {"Content-Type": mime_type.value}
return Response(data, mimetype=mime_type.value, headers=headers)
else:
logger.error(f"Could not generate LOD for resource {gtaa_uri}.")
return APIUtil.toErrorResponse(
"internal_server_error",
"Could not generate LOD for this resource",
)
logger.error("Not a proper mime type in the request.")
return Response("Error: No mime type detected...")
return APIUtil.toErrorResponse(
"internal_server_error",
"Error: No mime type detected...",
)

def _get_lod_gtaa(
self,
Expand All @@ -91,19 +96,17 @@ def _get_lod_gtaa(
:param mime_type: the mime_type, or serialization the resource is requested in.
:param sparql_endpoint: endpoint URL
:param nisv_organisation_uri: URI for the publisher
:return: RDF data in a response object
:return: RDF data
"""
mt_ld_format = mime_type.to_ld_format()
ld_type = mt_ld_format if mt_ld_format is not None else "json-ld"

rdf_graph = get_lod_resource_from_rdf_store(
rdf_graph = util.ld_util.get_lod_resource_from_rdf_store(
gtaa_uri, sparql_endpoint, nisv_organisation_uri
)
if rdf_graph:
if rdf_graph is not None:
# serialize using the mime_type
resp = rdf_graph.serialize(format=ld_type)
headers = {"Content-Type": mime_type.value}
return Response(resp, mimetype=mime_type.value, headers=headers)
return rdf_graph.serialize(format=ld_type)
logger.error(
f"Could not get the data for GTAA resource {gtaa_uri} from triple store at {sparql_endpoint}."
)
Expand All @@ -115,20 +118,28 @@ def _get_lod_view_gtaa(
"""Handler that, given a URI, gets RDF from the SPARQL endpoint and generates an HTML page.
:param resource_url: The URI for the resource.
"""
rdf_graph = get_lod_resource_from_rdf_store(
rdf_graph = util.ld_util.get_lod_resource_from_rdf_store(
resource_url, sparql_endpoint, nisv_organisation_uri
)
if rdf_graph:
return render_template(
"resource.html",
resource_uri=resource_url,
structured_data=json_ld_structured_data_for_resource(
structured_data=util.ld_util.json_ld_structured_data_for_resource(
rdf_graph, resource_url
),
json_header=util.ld_util.json_header_from_rdf_graph(
rdf_graph, resource_url
),
json_iri_iri=util.ld_util.json_iri_iri_from_rdf_graph(
rdf_graph, resource_url
),
json_iri_lit=util.ld_util.json_iri_lit_from_rdf_graph(
rdf_graph, resource_url
),
json_iri_bnode=util.ld_util.json_iri_bnode_from_rdf_graph(
rdf_graph, resource_url
),
json_header=json_header_from_rdf_graph(rdf_graph, resource_url),
json_iri_iri=json_iri_iri_from_rdf_graph(rdf_graph, resource_url),
json_iri_lit=json_iri_lit_from_rdf_graph(rdf_graph, resource_url),
json_iri_bnode=json_iri_bnode_from_rdf_graph(rdf_graph, resource_url),
nisv_sparql_endpoint=sparql_endpoint,
)
logger.error(
Expand Down
Loading

0 comments on commit 4d27b40

Please sign in to comment.