-
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 #50 from ternaustralia/edmond/entrypoint-update
Refactor entrypoint endpoint to support pagination. Add tern vocabs entrypoint
- Loading branch information
Showing
6 changed files
with
226 additions
and
36 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
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,2 +1,3 @@ | ||
from . import exceptions | ||
from . import nrm | ||
from . import tern |
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,116 @@ | ||
from jinja2 import Template | ||
|
||
from linkeddata_api import data | ||
from linkeddata_api.domain import schema | ||
from linkeddata_api.domain.viewer.entrypoints.utils import ( | ||
ceiling_division, | ||
get_optional_value, | ||
) | ||
|
||
|
||
def get_count(sparql_endpoint: str) -> int: | ||
query = """ | ||
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> | ||
PREFIX dcterms: <http://purl.org/dc/terms/> | ||
PREFIX owl: <http://www.w3.org/2002/07/owl#> | ||
SELECT (COUNT(*) AS ?count) | ||
FROM <http://www.ontotext.com/explicit> | ||
FROM <http://linked.data.gov.au/def/tern-cv/> | ||
WHERE { | ||
VALUES (?vocabularyType) { | ||
(skos:ConceptScheme) | ||
(skos:Collection) | ||
} | ||
?uri a ?vocabularyType ; | ||
skos:prefLabel ?_label . | ||
OPTIONAL { ?uri dcterms:description ?_description } | ||
OPTIONAL { ?uri dcterms:created ?_created } | ||
OPTIONAL { ?uri dcterms:modified ?_modified } | ||
FILTER NOT EXISTS { | ||
?uri owl:deprecated true . | ||
} | ||
} | ||
""" | ||
|
||
result = data.sparql.post(query, sparql_endpoint).json() | ||
|
||
return int(result["results"]["bindings"][0]["count"]["value"]) | ||
|
||
|
||
def get( | ||
sparql_endpoint: str, | ||
page: int, | ||
) -> schema.EntrypointItems: | ||
"""Get | ||
Raises RequestError and SPARQLResultJSONError | ||
""" | ||
limit = 20 | ||
|
||
query = Template( | ||
""" | ||
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> | ||
PREFIX dcterms: <http://purl.org/dc/terms/> | ||
PREFIX owl: <http://www.w3.org/2002/07/owl#> | ||
SELECT | ||
?uri | ||
(SAMPLE(?_label) as ?label) | ||
(SAMPLE(?_description) as ?description) | ||
(SAMPLE(?_created) as ?created) | ||
(SAMPLE(?_modified) as ?modified) | ||
FROM <http://www.ontotext.com/explicit> | ||
FROM <http://linked.data.gov.au/def/tern-cv/> | ||
WHERE { | ||
VALUES (?vocabularyType) { | ||
(skos:ConceptScheme) | ||
(skos:Collection) | ||
} | ||
?uri a ?vocabularyType ; | ||
skos:prefLabel ?_label . | ||
OPTIONAL { ?uri dcterms:description ?_description } | ||
OPTIONAL { ?uri dcterms:created ?_created } | ||
OPTIONAL { ?uri dcterms:modified ?_modified } | ||
FILTER NOT EXISTS { | ||
?uri owl:deprecated true . | ||
} | ||
} | ||
GROUP by ?uri | ||
ORDER by ?label | ||
LIMIT {{ limit }} | ||
OFFSET {{ offset }} | ||
""" | ||
).render(limit=20, offset=(page - 1) * limit) | ||
|
||
result = data.sparql.post(query, sparql_endpoint).json() | ||
|
||
count = get_count(sparql_endpoint) | ||
more_pages_exist = False | ||
if count > (page * limit): | ||
more_pages_exist = True | ||
|
||
total_pages = ceiling_division(count, limit) | ||
|
||
vocabs = [] | ||
|
||
for row in result["results"]["bindings"]: | ||
vocabs.append( | ||
schema.EntrypointItem( | ||
id=str(row["uri"]["value"]), | ||
label=str(row["label"]["value"]), | ||
description=get_optional_value(row, "description"), | ||
created=get_optional_value(row, "created"), | ||
modified=get_optional_value(row, "modified"), | ||
) | ||
) | ||
|
||
return schema.EntrypointItems( | ||
items=vocabs, | ||
more_pages_exists=more_pages_exist, | ||
items_count=count, | ||
limit=limit, | ||
total_pages=total_pages, | ||
) |
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,9 @@ | ||
from typing import Optional, Union | ||
|
||
|
||
def get_optional_value(row: dict, key: str) -> Optional[str]: | ||
return row.get(key)["value"] if row.get(key) else None | ||
|
||
|
||
def ceiling_division(a: Union[int, float], b: Union[int, float]) -> int: | ||
return int(-(a // -b)) |
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