aboutsummaryrefslogtreecommitdiff
path: root/gn3/api
diff options
context:
space:
mode:
authorMunyoki Kilyungi2023-10-24 10:54:48 +0300
committerBonfaceKilz2023-10-27 13:45:32 +0300
commit747d550d7bdbb150b806cbd4b08caf6075d51697 (patch)
treed5c0cb34dc9c0d244dfe32e4e62acefbc755eb67 /gn3/api
parent78717642b076e341da1ba9c373c723d0a2b8ae0e (diff)
downloadgenenetwork3-747d550d7bdbb150b806cbd4b08caf6075d51697.tar.gz
Implement "GET /species".
* gn3/api/metadata.py (list_species): New end-point. Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn3/api')
-rw-r--r--gn3/api/metadata.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py
index 1f99c57..eb7a599 100644
--- a/gn3/api/metadata.py
+++ b/gn3/api/metadata.py
@@ -620,3 +620,45 @@ CONSTRUCT {
context)
except (RemoteDisconnected, URLError):
return jsonify({})
+
+
+@metadata.route("/species", methods=["GET"])
+def list_species():
+ """List all species"""
+ try:
+ sparql = SPARQLWrapper(current_app.config.get("SPARQL_ENDPOINT"))
+ sparql.setQuery(Template("""
+$prefix
+
+CONSTRUCT {
+ ?species ?predicate ?object .
+} WHERE {
+ ?species ^skos:member gnc:Species ;
+ ?predicate ?object .
+ VALUES ?predicate {
+ rdfs:label skos:prefLabel
+ skos:altLabel gnt:shortName
+ gnt:family skos:notation
+ }
+
+}
+""").substitute(prefix=RDF_PREFIXES))
+ results = sparql.queryAndConvert()
+ results = json.loads(
+ results.serialize(format="json-ld")
+ )
+ return jsonld.compact(results, {
+ "@context": PREFIXES | {
+ "data": "@graph",
+ "type": "@type",
+ "id": "@id",
+ "name": "rdfs:label",
+ "family": "gnt:family",
+ "shortName": "gnt:shortName",
+ "alternateName": "skos:altLabel",
+ "taxonomicId": "skos:notation",
+ "fullName": "skos:prefLabel",
+ },
+ })
+ except (RemoteDisconnected, URLError):
+ return jsonify({})