Skip to content

Commit

Permalink
Merge pull request #18 from eea/develop
Browse files Browse the repository at this point in the history
Develop #100666
  • Loading branch information
avoinea authored Dec 11, 2018
2 parents 4209c44 + 995a5de commit 9c03277
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
7 changes: 6 additions & 1 deletion docs/HISTORY.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

3.3 - (2018-12-11)
---------------------
* Feature: when building the request from an endpoint that followed redirects
the query works fine now
[alecghica refs #100666]

3.2 - (2018-06-22)
-----------------------
* Change: updated URLs pointing to eea.europa.eu with https://
Expand Down Expand Up @@ -167,4 +173,3 @@ Changelog
------------------
* First version available on PyPI
[roug, moregale]

10 changes: 6 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@

setup(name=NAME,
version=VERSION,
description='Python API to query a SPARQL endpoint',
long_description = open('README.rst').read() + "\n\n" +
open(os.path.join("docs", "HISTORY.txt")).read(),
classifiers=[
'Environment :: Console',
'Intended Audience :: Developers',
"Programming Language :: Python",
'Operating System :: OS Independent',
'Topic :: Software Development :: Libraries :: Python Modules',
],
description='Python API to query a SPARQL endpoint',
long_description = open('README.rst').read() + "\n\n" +
open(os.path.join("docs", "HISTORY.txt")).read(),
],
keywords="Sparql Client",
author='European Environment Agency: IDM2 A-Team',
author_email='[email protected]',
url='https://github.com/eea/sparql-client',
license="MPL",
py_modules =['sparql'],
install_requires=[
'eventlet',
Expand Down
26 changes: 22 additions & 4 deletions sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ def _read_response(self, response, buf, timeout):
else:
buf.write(response.read())

def _build_response(self, query, opener, buf, timeout):
request = self._build_request(query)
return self._get_response(opener, request, buf,
timeout if timeout > 0 else None)

def _request(self, statement, timeout=0):
"""
Builds the query string, then opens a connection to the endpoint
Expand All @@ -498,12 +503,14 @@ def _request(self, statement, timeout=0):
query = self._queryString(statement)
buf = tempfile.NamedTemporaryFile()

opener = urllib2.build_opener()
opener = urllib2.build_opener(RedirectHandler)
opener.addheaders = self.headers().items()

request = self._build_request(query)
response = self._get_response(opener, request, buf,
timeout if timeout > 0 else None)
try:
response = self._build_response(query, opener, buf, timeout)
except SparqlException, error:
self.endpoint = error.message
response = self._build_response(query, opener, buf, timeout)

self._read_response(response, buf, timeout)

Expand Down Expand Up @@ -544,6 +551,17 @@ def _queryString(self, statement):
return urlencode(args)


class RedirectHandler(urllib2.HTTPRedirectHandler):
"""
Subclass the HTTPRedirectHandler to re-contruct request when follow redirect
"""
def redirect_request(self, req, fp, code, msg, headers, newurl):
if code in (301, 302, 303, 307):
raise SparqlException(code, newurl)
else:
return req


class _ResultsParser(object):
"""
Parse the XML result.
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2
3.3

0 comments on commit 9c03277

Please sign in to comment.