aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
authorMunyoki Kilyungi2024-09-11 11:51:04 +0300
committerJohn Nduli Kilyungi2024-09-18 08:32:34 +0300
commit304529f658980687f8a8561f7b71777f0f1af1a5 (patch)
treeba2bfb95402543ed764407dce67e1917fa0f5c38 /gn3
parent679bfa89863bae7ea89426e0e0e15e746d4b36fa (diff)
downloadgenenetwork3-304529f658980687f8a8561f7b71777f0f1af1a5.tar.gz
Add new method for updating RDF Graph content.
* gn3/db/rdf/__init__.py: Import POST, DIGEST, JSON. (update_rdf): New function. Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn3')
-rw-r--r--gn3/db/rdf/__init__.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/gn3/db/rdf/__init__.py b/gn3/db/rdf/__init__.py
index 6984497..94a5776 100644
--- a/gn3/db/rdf/__init__.py
+++ b/gn3/db/rdf/__init__.py
@@ -6,7 +6,7 @@ creating contexts to be used by jsonld when framing and/or compacting.
"""
import json
-from SPARQLWrapper import SPARQLWrapper
+from SPARQLWrapper import SPARQLWrapper, POST, DIGEST, JSON
from pyld import jsonld # type: ignore
@@ -190,3 +190,20 @@ def query_and_frame(query: str, context: dict, endpoint: str) -> dict:
"""Frame the results given a context"""
results = sparql_construct_query(query, endpoint)
return jsonld.frame(results, context)
+
+
+def update_rdf(
+ query: str, sparql_user: str, sparql_password: str, sparql_auth_uri: str
+) -> str:
+ """Update RDF Graph content---INSERT/DELETE---in a Graph Store
+ using HTTP. Example return:
+ 'Insert into <http://genenetwork.org>, 14 (or less) triples -- done'
+ """
+ sparql = SPARQLWrapper(sparql_auth_uri)
+ sparql.setHTTPAuth(DIGEST)
+ sparql.setCredentials(sparql_user, sparql_password)
+ sparql.setMethod(POST)
+ sparql.setReturnFormat(JSON)
+ sparql.setQuery(query)
+ _r = sparql.queryAndConvert()["results"] # type: ignore
+ return _r["bindings"][0]["callret-0"]["value"] # type: ignore