summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNatanael Arndt <arndtn@gmail.com>2020-10-08 13:59:22 +0200
committerNatanael Arndt <arndtn@gmail.com>2020-10-08 13:59:22 +0200
commite82888c92c80a9985f8c31d1afebe79914cb55f9 (patch)
treea52289650553bc084a6d9ccbba6e70c992fa0795
parent7a53c615c528d0fc0af55fa6a5b925c330cb5239 (diff)
downloadrdflib-e82888c92c80a9985f8c31d1afebe79914cb55f9.tar.gz
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 cda79d7e..1e49833e 100644
--- a/rdflib/plugins/stores/sparqlconnector.py
+++ b/rdflib/plugins/stores/sparqlconnector.py
@@ -64,8 +64,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", "POST_FORM"')
self._method = method
@@ -73,7 +73,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
@@ -89,6 +89,7 @@ class SPARQLConnector(object):
args["headers"].update(headers)
if self.method == "GET":
+ params["query"] = query
args["params"].update(params)
qsa = "?" + urlencode(args["params"])
try:
@@ -97,8 +98,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: