summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Car <nicholas.car@surroundaustralia.com>2020-12-27 21:10:09 +1000
committerGitHub <noreply@github.com>2020-12-27 21:10:09 +1000
commit7e2c8e3c93916298aeabdf8968e1b3b8ff14f96a (patch)
tree30b37f712eb4d8f4645e136ff677573f4fa79585
parent3343a1453e10408c422f83c57f5ea4ff3f684f11 (diff)
parent647f34acf9d3508003fcf3e68baa80eb3adb3ae4 (diff)
downloadrdflib-7e2c8e3c93916298aeabdf8968e1b3b8ff14f96a.tar.gz
Merge pull request #1185 from white-gecko/fix_sparqlstore_post
Fix usage of default-graph for POST and introduce POST_FORM
-rw-r--r--rdflib/plugins/stores/sparqlconnector.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/rdflib/plugins/stores/sparqlconnector.py b/rdflib/plugins/stores/sparqlconnector.py
index 13571caa..ffd8b30a 100644
--- a/rdflib/plugins/stores/sparqlconnector.py
+++ b/rdflib/plugins/stores/sparqlconnector.py
@@ -66,8 +66,8 @@ class SPARQLConnector(object):
@method.setter
def method(self, method):
- if method not in ("GET", "POST"):
- raise SPARQLConnectorException('Method must be "GET" or "POST"')
+ if method not in ("GET", "POST", "POST_FORM"):
+ raise SPARQLConnectorException('Method must be "GET", "POST", or "POST_FORM"')
self._method = method
@@ -75,7 +75,7 @@ class SPARQLConnector(object):
if not self.query_endpoint:
raise SPARQLConnectorException("Query endpoint not set!")
- params = {"query": query}
+ params = {}
# this test ensures we don't have a useless (BNode) default graph URI, which calls to Graph().query() will add
if default_graph is not None and type(default_graph) != BNode:
params["default-graph-uri"] = default_graph
@@ -91,6 +91,7 @@ class SPARQLConnector(object):
args["headers"].update(headers)
if self.method == "GET":
+ params["query"] = query
args["params"].update(params)
qsa = "?" + urlencode(args["params"])
try:
@@ -99,8 +100,16 @@ class SPARQLConnector(object):
raise ValueError("You did something wrong formulating either the URI or your SPARQL query")
elif self.method == "POST":
args["headers"].update({"Content-Type": "application/sparql-query"})
+ qsa = "?" + urlencode(params)
+ try:
+ res = urlopen(Request(self.query_endpoint + qsa, data=query.encode(), headers=args["headers"]))
+ except HTTPError as e:
+ return e.code, str(e), None
+ elif self.method == "POST_FORM":
+ params["query"] = query
+ args["params"].update(params)
try:
- res = urlopen(Request(self.query_endpoint, data=query.encode(), headers=args["headers"]))
+ res = urlopen(Request(self.query_endpoint, data=urlencode(args["params"]).encode(), headers=args["headers"]))
except HTTPError as e:
return e.code, str(e), None
else: