diff --git a/src/linkeddata_api/data/sparql.py b/src/linkeddata_api/data/sparql.py index cdde33e..1df6cbe 100644 --- a/src/linkeddata_api/data/sparql.py +++ b/src/linkeddata_api/data/sparql.py @@ -1,8 +1,10 @@ import requests from . import exceptions +from linkeddata_api.log_time import log_time +@log_time def post( query: str, sparql_endpoint: str, accept: str = "application/sparql-results+json" ) -> requests.Response: @@ -21,7 +23,9 @@ def post( "content-type": "application/sparql-query", } - response = requests.post(url=sparql_endpoint, headers=headers, data=query, timeout=60) + response = requests.post( + url=sparql_endpoint, headers=headers, data=query, timeout=60 + ) try: response.raise_for_status() @@ -51,7 +55,9 @@ def get( } params = {"query": query} - response = requests.get(url=sparql_endpoint, headers=headers, params=params, timeout=60) + response = requests.get( + url=sparql_endpoint, headers=headers, params=params, timeout=60 + ) try: response.raise_for_status() diff --git a/src/linkeddata_api/domain/curie.py b/src/linkeddata_api/domain/curie.py index de4e99b..57e3a44 100644 --- a/src/linkeddata_api/domain/curie.py +++ b/src/linkeddata_api/domain/curie.py @@ -16,6 +16,8 @@ "https://w3id.org/tern/ontologies/tern/": "tern", "http://www.w3.org/2002/07/owl#": "owl", "http://www.w3.org/2001/XMLSchema#": "xsd", + "http://rdfs.org/ns/void#": "void", + "http://www.w3.org/ns/prov#": "prov", } # Don't find curies for these - speeds up request processing. diff --git a/src/linkeddata_api/domain/label.py b/src/linkeddata_api/domain/label.py index f41fa74..67e7e20 100644 --- a/src/linkeddata_api/domain/label.py +++ b/src/linkeddata_api/domain/label.py @@ -3,8 +3,10 @@ from jinja2 import Template from linkeddata_api import data +from linkeddata_api.log_time import log_time +@log_time def get( uri: str, sparql_endpoint: str, @@ -24,20 +26,8 @@ def get( SELECT DISTINCT ?label WHERE { - { - BIND(<{{ uri }}> as ?uri) - VALUES (?labelProperty) { - (skos:prefLabel) - (rdfs:label) - (dcterms:title) - (schema:name) - (sdo:name) - (dcterms:identifier) - } - ?uri ?labelProperty ?label . - } {% if uri.startswith('http://linked.data.gov.au/def/tern-cv/') %} - UNION { + { SERVICE { BIND(<{{ uri }}> as ?uri) VALUES (?labelProperty) { @@ -51,6 +41,19 @@ def get( ?uri ?labelProperty ?label . } } + {% else %} + { + BIND(<{{ uri }}> as ?uri) + VALUES (?labelProperty) { + (skos:prefLabel) + (rdfs:label) + (dcterms:title) + (schema:name) + (sdo:name) + (dcterms:identifier) + } + ?uri ?labelProperty ?label . + } {% endif %} } LIMIT 1 @@ -73,6 +76,15 @@ def get( def _get_from_list_query(uris: list[str]) -> str: # TODO: Currently, we try and fetch from TERN's controlled vocabularies. # We may want to also fetch with a SERVICE query from other repositories in the future. + tern_cv_uris = [ + uri for uri in uris if uri.startswith("http://linked.data.gov.au/def/tern-cv/") + ] + uris = [ + uri + for uri in uris + if not uri.startswith("http://linked.data.gov.au/def/tern-cv/") + ] + template = Template( """ PREFIX skos: @@ -83,26 +95,16 @@ def _get_from_list_query(uris: list[str]) -> str: SELECT DISTINCT ?uri ?label WHERE { - {% for uri in uris %} { SELECT DISTINCT ?uri ?label WHERE { { - BIND(<{{ uri }}> as ?uri) - VALUES (?labelProperty) { - (skos:prefLabel) - (rdfs:label) - (dcterms:title) - (schema:name) - (sdo:name) - (dcterms:identifier) + VALUES (?uri) { + {% for uri in uris %} + (<{{ uri }}>) + {% endfor %} } - ?uri ?labelProperty ?label . - } - {% if uri.startswith('http://linked.data.gov.au/def/tern-cv/') %} - UNION { - SERVICE { - BIND(<{{ uri }}> as ?uri) + { VALUES (?labelProperty) { (skos:prefLabel) (rdfs:label) @@ -114,19 +116,37 @@ def _get_from_list_query(uris: list[str]) -> str: ?uri ?labelProperty ?label . } } - {% endif %} } - LIMIT 1 } - {% if not loop.last %}UNION{% endif %} - {% endfor %} + UNION { + SELECT DISTINCT ?uri ?label + WHERE { + VALUES (?uri) { + {% for uri in tern_cv_uris %} + (<{{ uri }}>) + {% endfor %} + } + SERVICE { + VALUES (?labelProperty) { + (skos:prefLabel) + (rdfs:label) + (dcterms:title) + (schema:name) + (sdo:name) + (dcterms:identifier) + } + ?uri ?labelProperty ?label . + } + } + } } """ ) - query = template.render(uris=uris) + query = template.render(uris=uris, tern_cv_uris=tern_cv_uris) return query +@log_time def get_from_list( uris: list[str], sparql_endpoint: str,