diff options
author | Munyoki Kilyungi | 2023-10-27 16:23:08 +0300 |
---|---|---|
committer | BonfaceKilz | 2023-10-27 16:55:16 +0300 |
commit | f77389c5878b2d90028977c91d23dc16db117c46 (patch) | |
tree | 9e590c1c426c251236a46a41c47f479caf669c65 | |
parent | b24f7c6143dd1182819aa74d87d728c6df6df9cd (diff) | |
download | genenetwork3-f77389c5878b2d90028977c91d23dc16db117c46.tar.gz |
Refactor to use new rdf.query_* abstractions.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r-- | gn3/api/metadata.py | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py index 299f82d..33231a1 100644 --- a/gn3/api/metadata.py +++ b/gn3/api/metadata.py @@ -10,7 +10,10 @@ from flask import current_app from pyld import jsonld # type: ignore from gn3.db.rdf import RDF_PREFIXES -from gn3.db.rdf import sparql_construct_query +from gn3.db.rdf import (sparql_construct_query, + query_frame_and_compact, + query_and_compact, + query_and_frame) BASE_CONTEXT = { @@ -886,10 +889,9 @@ CONSTRUCT { @metadata.route("/groups/<name>", methods=["GET"]) def fetch_group_by_species(name): - """Fetch the list of groups""" + """Fetch the list of groups (I.e. Inbredsets)""" try: - results = sparql_construct_query( - query=Template(""" + _query=Template(""" $prefix CONSTRUCT { @@ -907,10 +909,8 @@ CONSTRUCT { } } -""").substitute(prefix=RDF_PREFIXES, name=name), - endpoint=current_app.config.get("SPARQL_ENDPOINT") - ) - return jsonld.compact(results, { +""").substitute(prefix=RDF_PREFIXES, name=name) + _context = { "@context": BASE_CONTEXT | { "skos": "http://www.w3.org/2004/02/skos/core#", "gnt": "http://genenetwork.org/term/", @@ -923,16 +923,20 @@ CONSTRUCT { "geneticType": "gnt:geneticType", "fullName": "skos:prefLabel", }, - }) + } + return query_and_compact( + _query, _context, + current_app.config.get("SPARQL_ENDPOINT") + ) except (RemoteDisconnected, URLError): return jsonify({}) + @metadata.route("/probesets/<name>", methods=["GET"]) def probesets(name): - """Fetch a probesets's metadata given it's name""" + """Fetch a probeset's metadata given it's name""" try: - results = sparql_construct_query( - query=Template(""" + _query = Template(""" $prefix CONSTRUCT { @@ -948,12 +952,8 @@ CONSTRUCT { ?chip rdfs:label ?chipName . } . } -""").substitute(prefix=RDF_PREFIXES, name=name), - endpoint=current_app.config.get("SPARQL_ENDPOINT") - ) - if not results: - return jsonify({}) - frame = { +""").substitute(prefix=RDF_PREFIXES, name=name) + _context = { "@context": BASE_CONTEXT | { "gnt": "http://genenetwork.org/term/", "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", @@ -987,7 +987,9 @@ CONSTRUCT { "chebi": "gnt:hasChebiId", }, } - return jsonld.compact(results, frame) - # return jsonld.compact(jsonld.frame(results, frame), frame) + return query_and_compact( + _query, _context, + current_app.config.get("SPARQL_ENDPOINT") + ) except (RemoteDisconnected, URLError): return jsonify({}) |