-
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 #51 from ternaustralia/edmond/entrypoint-update
Add v2 API. Add JSON resource rendering with pagination for properties and their values with JSON profile support
- Loading branch information
Showing
29 changed files
with
1,405 additions
and
33 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
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
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,7 @@ | ||
from .blueprint import bp | ||
|
||
# import all sub modules with views registered with blueprint | ||
from . import ontology_viewer | ||
from . import version_info | ||
from . import rdf_tools | ||
from . import viewer |
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,3 @@ | ||
from flask_tern.openapi import OpenApiBlueprint | ||
|
||
bp = OpenApiBlueprint("api_v2", __name__) |
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 @@ | ||
from . import classes |
1 change: 1 addition & 0 deletions
1
src/linkeddata_api/views/api_v2/ontology_viewer/classes/__init__.py
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 @@ | ||
from . import flat |
18 changes: 18 additions & 0 deletions
18
src/linkeddata_api/views/api_v2/ontology_viewer/classes/flat/__init__.py
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,18 @@ | ||
from flask import request | ||
from flask_tern import openapi | ||
from flask_tern.logging import create_audit_event, log_audit | ||
|
||
from linkeddata_api.domain.pydantic_jsonify import jsonify | ||
from linkeddata_api.views.api_v2.blueprint import bp | ||
from . import crud | ||
|
||
|
||
@bp.route("/ontology_viewer/classes/flat") | ||
@openapi.validate(validate_response=False) | ||
def classes_flat_get(): | ||
# TODO: add log audit. | ||
|
||
ontology_id = request.args.get("ontology_id") | ||
classes = crud.get(ontology_id) | ||
|
||
return jsonify(classes) |
89 changes: 89 additions & 0 deletions
89
src/linkeddata_api/views/api_v2/ontology_viewer/classes/flat/crud.py
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,89 @@ | ||
from typing import List | ||
|
||
import requests | ||
from jinja2 import Template | ||
from werkzeug.exceptions import HTTPException | ||
from werkzeug.wrappers import Response | ||
from flask_tern.cache import cache | ||
|
||
from . import schema | ||
|
||
|
||
ontology_id_mapping = { | ||
"tern-ontology": { | ||
"sparql_endpoint": "https://graphdb.tern.org.au/repositories/knowledge_graph_core", | ||
"named_graph": "https://w3id.org/tern/ontologies/tern/", | ||
} | ||
} | ||
|
||
|
||
query_template = Template( | ||
""" | ||
# Get a list of classes ordered by label. | ||
# Only one string or langString of type "en" is retrieved as label. | ||
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
PREFIX owl: <http://www.w3.org/2002/07/owl#> | ||
PREFIX sh: <http://www.w3.org/ns/shacl#> | ||
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> | ||
SELECT distinct ?id (SAMPLE(?_label) as ?label) | ||
FROM <http://www.ontotext.com/explicit> | ||
FROM <{{ named_graph }}> | ||
WHERE { | ||
{ | ||
?_class a sh:NodeShape . | ||
?_class sh:targetClass ?id . | ||
FILTER(!isBlank(?id)) | ||
{ | ||
?id rdfs:label ?_label . | ||
} | ||
UNION { | ||
?id skos:prefLabel ?_label . | ||
} | ||
} | ||
} | ||
GROUP BY ?id | ||
ORDER BY ?label | ||
""" | ||
) | ||
|
||
|
||
@cache.memoize() | ||
def get(ontology_id: str) -> List[schema.ClassItem]: | ||
try: | ||
mapping = ontology_id_mapping[ontology_id] | ||
except KeyError as err: | ||
description = f"Unknown ontology ID '{ontology_id}'. Valid ontology IDs: {list(ontology_id_mapping.keys())}" | ||
raise HTTPException( | ||
description=description, response=Response(description, status=404) | ||
) from err | ||
|
||
query = query_template.render(named_graph=mapping["named_graph"]) | ||
headers = { | ||
"accept": "application/sparql-results+json", | ||
"content-type": "application/sparql-query", | ||
} | ||
|
||
r = requests.post( | ||
url=mapping["sparql_endpoint"], headers=headers, data=query, timeout=60 | ||
) | ||
|
||
try: | ||
r.raise_for_status() | ||
except requests.exceptions.HTTPError as err: | ||
raise HTTPException( | ||
description=err.response.text, | ||
response=Response(err.response.text, status=502), | ||
) from err | ||
|
||
resultset = r.json() | ||
classes = [] | ||
for row in resultset["results"]["bindings"]: | ||
classes.append( | ||
schema.ClassItem(id=row["id"]["value"], label=row["label"]["value"]) | ||
) | ||
|
||
return classes |
6 changes: 6 additions & 0 deletions
6
src/linkeddata_api/views/api_v2/ontology_viewer/classes/flat/schema.py
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,6 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class ClassItem(BaseModel): | ||
id: str | ||
label: str |
Oops, something went wrong.