clean up sparqlwrapper-lite
all passing any kwargs to requests
@@ -22,12 +22,16 @@ class SPARQLWrapper(object):
this class deals with nitty gritty details of talking to a SPARQL server
"""
- def __init__(self, query_endpoint=None, update_endpoint=None, returnFormat='xml', method='GET', timeout=None):
+ def __init__(self, query_endpoint=None, update_endpoint=None, returnFormat='xml', method='GET', **kwargs):
+
+ """
+ Any additional keyword arguments will be passed to requests, and can be used to setup timesouts, basic auth, etc.
+ """
self.returnFormat = returnFormat
self.query_endpoint = query_endpoint
self.update_endpoint = update_endpoint
- self.timeout = timeout
+ self.kwargs = kwargs
self.method = method
# it is recommended to have one session object per thread/process. This assures that is the case.
@@ -50,7 +54,6 @@ class SPARQLWrapper(object):
@method.setter
def method(self, method):
if method not in ('GET', 'POST'):
- print 'cake'
raise SPARQLWrapperException('Method must be "GET" or "POST"')
self._method = method
@@ -66,13 +69,17 @@ class SPARQLWrapper(object):
headers = { 'Accept': _response_mime_types[self.returnFormat] }
- args = dict(url=self.query_endpoint,
- headers=headers,
- timeout=self.timeout)
+ args = dict(self.kwargs)
+ args.update(url=self.query_endpoint)
+
+ # merge params/headers dicts
+ args.setdefault('params', {})
+ args.setdefault('headers', {})
+ args['headers'].update(headers)
if self.method == 'GET':
- args['params'] = params
+ args['params'].update(params)
elif self.method == 'POST':
args['data'] = params
else:
@@ -81,6 +88,7 @@ class SPARQLWrapper(object):
res = self.session.request(self.method, **args)
res.raise_for_status()
+
return Result.parse(BytesIO(res.content), content_type=res.headers['Content-type'])
def update(self, update, default_graph=None):
@@ -93,16 +101,22 @@ class SPARQLWrapper(object):
headers = { 'Accept': _response_mime_types[self.returnFormat] }
- res = self.session.post(self.update_endpoint,
- params=params,
- data=update.encode('utf-8'),
- headers=headers,
- timeout=self.timeout)
+ args = dict(self.kwargs)
+
+ args.update(url=self.update_endpoint,
+ data=update.encode('utf-8'))
+
+ # merge params/headers dicts
+ args.setdefault('params', {})
+ args['params'].update(params)
+ args.setdefault('headers', {})
+ args['headers'].update(headers)
+
+ res = self.session.post(**args)
res.raise_for_status()
def close(self):
- pass
- #self.session.close()
+ self.session.close()
