-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from ternaustralia/edmond/vocab-viewer
Speed improvement - get labels and internal resource statuses from a list of URIs in one request
- Loading branch information
Showing
7 changed files
with
166 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
ARG ALPINE_VERSION=3.13 | ||
ARG ALPINE_VERSION=3.16 | ||
ARG LINKEDDATA_API_VERSION | ||
|
||
# BUILD and install code | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
from . import sparql | ||
from . import label | ||
from . import curie | ||
from . import internal_resource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from jinja2 import Template | ||
|
||
from linkeddata_api.vocab_viewer import nrm | ||
|
||
|
||
def _get_from_list_query(uris: list[str]) -> str: | ||
template = Template( | ||
""" | ||
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> | ||
SELECT distinct ?uri ?internal | ||
WHERE { | ||
VALUES (?uri) { | ||
(<http://example.com>) | ||
{% for uri in uris %} | ||
(<{{ uri }}>) | ||
{% endfor %} | ||
} | ||
bind(exists{ ?uri ?p ?o } as ?internal) | ||
} | ||
""" | ||
) | ||
return template.render(uris=uris) | ||
|
||
|
||
def get_from_list( | ||
uris: list[str], | ||
sparql_endpoint: str = "https://graphdb.tern.org.au/repositories/dawe_vocabs_core", | ||
) -> dict[str, str]: | ||
query = _get_from_list_query(uris) | ||
|
||
result = nrm.sparql.post(query, sparql_endpoint) | ||
|
||
return_results = {} | ||
|
||
try: | ||
rows = result["results"]["bindings"] | ||
for row in rows: | ||
uri = str(row["uri"]["value"]) | ||
internal = str(row["internal"]["value"]) | ||
return_results[uri] = True if internal == "true" else False | ||
|
||
except KeyError as err: | ||
raise nrm.exceptions.SPARQLResultJSONError( | ||
f"Unexpected SPARQL result set.\n{result}\n{err}" | ||
) from err | ||
|
||
return return_results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters