From 304529f658980687f8a8561f7b71777f0f1af1a5 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Wed, 11 Sep 2024 11:51:04 +0300 Subject: 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 --- gn3/db/rdf/__init__.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'gn3') 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 , 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 -- cgit v1.2.3