diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt index 07fc7cd..fd10eec 100644 --- a/docs/HISTORY.txt +++ b/docs/HISTORY.txt @@ -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:// @@ -167,4 +173,3 @@ Changelog ------------------ * First version available on PyPI [roug, moregale] - diff --git a/setup.py b/setup.py index 54811f8..d5c413a 100644 --- a/setup.py +++ b/setup.py @@ -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='eea-edw-a-team-alerts@googlegroups.com', url='https://github.com/eea/sparql-client', + license="MPL", py_modules =['sparql'], install_requires=[ 'eventlet', diff --git a/sparql.py b/sparql.py index a0f538e..3989372 100755 --- a/sparql.py +++ b/sparql.py @@ -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 @@ -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) @@ -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. diff --git a/version.txt b/version.txt index a3ec5a4..eb39e53 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -3.2 +3.3