Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy support in OWSLib #927

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions owslib/catalogue/csw2.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_namespaces():
class CatalogueServiceWeb(object):
""" csw request class """
def __init__(self, url, lang='en-US', version='2.0.2', timeout=10, skip_caps=False,
username=None, password=None, auth=None, headers=None):
username=None, password=None, auth=None, headers=None, proxies=None):
"""

Construct and process a GetCapabilities request
Expand Down Expand Up @@ -75,6 +75,7 @@ def __init__(self, url, lang='en-US', version='2.0.2', timeout=10, skip_caps=Fal
self.timeout = timeout
self.auth = auth or Authentication(username, password)
self.headers = headers
self.proxies = proxies
self.service = 'CSW'
self.exceptionreport = None
self.owscommon = ows.OwsCommon('1.0.0')
Expand Down Expand Up @@ -684,7 +685,7 @@ def _invoke(self):
self.request = '%s%s' % (bind_url(request_url), self.request)
self.response = openURL(
self.request, None, 'Get', timeout=self.timeout, auth=self.auth,
headers=self.headers).read()
headers=self.headers, proxies=self.proxies).read()
else:
self.request = cleanup_namespaces(self.request)
# Add any namespaces used in the "typeNames" attribute of the
Expand Down
5 changes: 3 additions & 2 deletions owslib/catalogue/csw3.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_namespaces():
class CatalogueServiceWeb(object):
""" csw request class """
def __init__(self, url, lang='en-US', version='3.0.0', timeout=10, skip_caps=False,
username=None, password=None, auth=None, headers=None):
username=None, password=None, auth=None, headers=None, proxies=None):
"""

Construct and process a GetCapabilities request
Expand Down Expand Up @@ -75,6 +75,7 @@ def __init__(self, url, lang='en-US', version='3.0.0', timeout=10, skip_caps=Fal
self.timeout = timeout
self.auth = auth or Authentication(username, password)
self.headers = headers
self.proxies = proxies
self.service = 'CSW'
self.exceptionreport = None
self.owscommon = ows.OwsCommon('2.0.0')
Expand Down Expand Up @@ -581,7 +582,7 @@ def _invoke(self):
if self.headers:
headers_.update(self.headers)
self.response = openURL(
self.request, None, 'Get', timeout=self.timeout, auth=self.auth, headers=headers_
self.request, None, 'Get', timeout=self.timeout, auth=self.auth, headers=headers_, proxies=self.proxies).read()
).read()
else:
self.request = cleanup_namespaces(self.request)
Expand Down
8 changes: 4 additions & 4 deletions owslib/coverage/wcs100.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ def __getitem__(self, name):
else:
raise KeyError("No content named %s" % name)

def __init__(self, url, xml, cookies, auth=None, timeout=30, headers=None):
super(WebCoverageService_1_0_0, self).__init__(auth=auth, timeout=timeout, headers=headers)
def __init__(self, url, xml, cookies, auth=None, timeout=30, headers=None, proxies=None):
super(WebCoverageService_1_0_0, self).__init__(auth=auth, timeout=timeout, headers=headers, proxies=proxies)
self.version = '1.0.0'
self.url = url
self.cookies = cookies
self.timeout = timeout
# initialize from saved capability document or access the server
reader = WCSCapabilitiesReader(self.version, self.cookies, self.auth, headers=self.headers)
reader = WCSCapabilitiesReader(self.version, self.cookies, self.auth, headers=self.headers, proxies=self.proxies)
if xml:
self._capabilities = reader.readString(xml)
else:
Expand Down Expand Up @@ -153,7 +153,7 @@ def getCoverage(self, identifier=None, bbox=None, time=None, format=None, crs=No
data = urlencode(request)
log.debug('WCS 1.0.0 DEBUG: Second part of URL: %s' % data)

u = openURL(base_url, data, method, self.cookies, auth=self.auth, timeout=timeout, headers=self.headers)
u = openURL(base_url, data, method, self.cookies, auth=self.auth, timeout=timeout, headers=self.headers, proxies=self.proxies)
return u

def getOperationByName(self, name):
Expand Down
4 changes: 2 additions & 2 deletions owslib/coverage/wcs110.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, url, xml, cookies, auth=None, timeout=30, headers=None):
self.cookies = cookies
self.timeout = timeout
# initialize from saved capability document or access the server
reader = WCSCapabilitiesReader(self.version, self.cookies, self.auth, headers=self.headers)
reader = WCSCapabilitiesReader(self.version, self.cookies, self.auth, headers=self.headers, proxies=self.proxies)
if xml:
self._capabilities = reader.readString(xml)
else:
Expand Down Expand Up @@ -205,7 +205,7 @@ def getCoverage(self, identifier=None, bbox=None, time=None, format=None, store=
# encode and request
data = urlencode(request)

u = openURL(base_url, data, method, self.cookies, auth=self.auth, timeout=timeout, headers=self.headers)
u = openURL(base_url, data, method, self.cookies, auth=self.auth, timeout=timeout, headers=self.headers, proxies=self.proxies)
return u

def getOperationByName(self, name):
Expand Down
4 changes: 2 additions & 2 deletions owslib/coverage/wcs200.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, url, xml, cookies, auth=None, timeout=30, headers=None):
self.timeout = timeout
self.ows_common = OwsCommon(version="2.0.0")
# initialize from saved capability document or access the server
reader = WCSCapabilitiesReader(self.version, self.cookies, self.auth, headers=self.headers)
reader = WCSCapabilitiesReader(self.version, self.cookies, self.auth, headers=self.headers, proxies=self.proxies)
if xml:
self._capabilities = reader.readString(xml)
else:
Expand Down Expand Up @@ -214,7 +214,7 @@ def getCoverage(
data += param_list_to_url_string(sizes, 'size')
log.debug("WCS 2.0.0 DEBUG: Second part of URL: %s" % data)

u = openURL(base_url, data, method, self.cookies, auth=self.auth, timeout=timeout, headers=self.headers)
u = openURL(base_url, data, method, self.cookies, auth=self.auth, timeout=timeout, headers=self.headers, proxies=self.proxies)
return u

def getOperationByName(self, name):
Expand Down
4 changes: 2 additions & 2 deletions owslib/coverage/wcs201.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, url, xml, cookies, auth=None, timeout=30, headers=None):
self.timeout = timeout
self.ows_common = OwsCommon(version="2.0.1")
# initialize from saved capability document or access the server
reader = WCSCapabilitiesReader(self.version, self.cookies, self.auth, headers=self.headers)
reader = WCSCapabilitiesReader(self.version, self.cookies, self.auth, headers=self.headers, proxies=self.proxies)
if xml:
self._capabilities = reader.readString(xml)
else:
Expand Down Expand Up @@ -215,7 +215,7 @@ def getCoverage(

log.debug("WCS 2.0.1 DEBUG: Second part of URL: %s" % data)

u = openURL(base_url, data, method, self.cookies, auth=self.auth, timeout=timeout, headers=self.headers)
u = openURL(base_url, data, method, self.cookies, auth=self.auth, timeout=timeout, headers=self.headers, proxies=self.proxies)
return u

def getOperationByName(self, name):
Expand Down
15 changes: 9 additions & 6 deletions owslib/coverage/wcsBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ def __new__(self, url, xml, cookies, auth=None, timeout=30, headers=None):
self._describeCoverage = {} # cache for DescribeCoverage responses
return obj

def __init__(self, auth=None, timeout=30, headers=None):
def __init__(self, auth=None, timeout=30, headers=None, proxies=None):
self.auth = auth or Authentication()
self.headers = headers
self.timeout = timeout
self.proxies = proxies

def getDescribeCoverage(self, identifier):
''' returns a describe coverage document - checks the internal cache to see if it has been fetched before '''
if identifier not in list(self._describeCoverage.keys()):
reader = DescribeCoverageReader(
self.version, identifier, self.cookies, self.auth, self.timeout, self.headers)
self.version, identifier, self.cookies, self.auth, self.timeout, self.headers, proxies=self.proxies)
self._describeCoverage[identifier] = reader.read(self.url)
return self._describeCoverage[identifier]

Expand All @@ -71,7 +72,7 @@ class WCSCapabilitiesReader(object):
"""Read and parses WCS capabilities document into a lxml.etree infoset
"""

def __init__(self, version=None, cookies=None, auth=None, timeout=30, headers=None):
def __init__(self, version=None, cookies=None, auth=None, timeout=30, headers=None, proxies=None):
"""Initialize
@type version: string
@param version: WCS Version parameter e.g '1.0.0'
Expand All @@ -80,6 +81,7 @@ def __init__(self, version=None, cookies=None, auth=None, timeout=30, headers=No
self._infoset = None
self.cookies = cookies
self.headers = headers
self.proxies = proxies
self.timeout = timeout
self.auth = auth or Authentication()

Expand Down Expand Up @@ -117,7 +119,7 @@ def read(self, service_url, timeout=30):
@return: An elementtree tree representation of the capabilities document
"""
request = self.capabilities_url(service_url)
u = openURL(request, timeout=timeout, cookies=self.cookies, auth=self.auth, headers=self.headers)
u = openURL(request, timeout=timeout, cookies=self.cookies, auth=self.auth, headers=self.headers, proxies=self.proxies)
return etree.fromstring(u.read())

def readString(self, st):
Expand All @@ -132,7 +134,7 @@ class DescribeCoverageReader(object):
"""Read and parses WCS DescribeCoverage document into a lxml.etree infoset
"""

def __init__(self, version, identifier, cookies, auth=None, timeout=30, headers=None):
def __init__(self, version, identifier, cookies, auth=None, timeout=30, headers=None, proxies=None):
"""Initialize
@type version: string
@param version: WCS Version parameter e.g '1.0.0'
Expand All @@ -142,6 +144,7 @@ def __init__(self, version, identifier, cookies, auth=None, timeout=30, headers=
self.identifier = identifier
self.cookies = cookies
self.headers = headers
self.proxies = proxies
self.timeout = timeout
self.auth = auth or Authentication()

Expand Down Expand Up @@ -196,5 +199,5 @@ def read(self, service_url, timeout=30):
"""

request = self.descCov_url(service_url)
u = openURL(request, cookies=self.cookies, timeout=timeout, auth=self.auth, headers=self.headers)
u = openURL(request, cookies=self.cookies, timeout=timeout, auth=self.auth, headers=self.headers, proxies=self.proxies)
return etree.fromstring(u.read())
6 changes: 3 additions & 3 deletions owslib/csw.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


def CatalogueServiceWeb(url, lang='en-US', version='2.0.2', timeout=10, skip_caps=False,
username=None, password=None, auth=None, headers=None):
username=None, password=None, auth=None, headers=None, proxies=None):
"""
CSW factory function, returns a version specific CatalogueServiceWeb object

Expand Down Expand Up @@ -59,12 +59,12 @@ def CatalogueServiceWeb(url, lang='en-US', version='2.0.2', timeout=10, skip_cap
return csw2.CatalogueServiceWeb(
clean_url, lang=lang, version=version, timeout=timeout,
skip_caps=skip_caps, username=username, password=password,
auth=auth, headers=headers)
auth=auth, headers=headers, proxies=proxies)
if version == '3.0.0':
return csw3.CatalogueServiceWeb(
clean_url, lang=lang, version=version, timeout=timeout,
skip_caps=skip_caps, username=username, password=password,
auth=auth, headers=headers)
auth=auth, headers=headers, proxies=proxies)

raise NotImplementedError('The CSW version ({}) you requested is'
' not implemented. Please use 2.0.2 or'
Expand Down
8 changes: 5 additions & 3 deletions owslib/feature/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class WFSCapabilitiesReader(object):
"""Read and parse capabilities document into a lxml.etree infoset
"""

def __init__(self, version="1.0", username=None, password=None, headers=None, auth=None):
def __init__(self, version="1.0", username=None, password=None, headers=None, auth=None, proxies=None):
"""Initialize"""
self.headers = headers
self.proxies = proxies
if auth:
if username:
auth.username = username
Expand Down Expand Up @@ -51,7 +52,7 @@ def read(self, url, timeout=30):
A timeout value (in seconds) for the request.
"""
request = self.capabilities_url(url)
u = openURL(request, timeout=timeout, headers=self.headers, auth=self.auth)
u = openURL(request, timeout=timeout, headers=self.headers, auth=self.auth, proxies=self.proxies)
return etree.fromstring(u.read())

def readString(self, st):
Expand All @@ -68,9 +69,10 @@ def readString(self, st):


class AbstractContentMetadata(object):
def __init__(self, headers=None, auth=None):
def __init__(self, headers=None, auth=None, proxies=None):
self.auth = auth or Authentication()
self.headers = headers
self.proxies = proxies

def get_metadata(self):
return [
Expand Down
8 changes: 4 additions & 4 deletions owslib/feature/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def get_schema(
url, typename, version="1.0.0", timeout=30, headers=None, username=None, password=None, auth=None
url, typename, version="1.0.0", timeout=30, headers=None, username=None, password=None, auth=None, proxies=None
):
"""Parses DescribeFeatureType response and creates schema compatible
with :class:`fiona`
Expand All @@ -47,7 +47,7 @@ def get_schema(
auth = Authentication(username, password)
url = _get_describefeaturetype_url(url, version, typename)
root = _get_remote_describefeaturetype(url, timeout=timeout,
headers=headers, auth=auth)
headers=headers, auth=auth, proxies=proxies)

if ":" in typename:
typename = typename.split(":")[1]
Expand Down Expand Up @@ -168,7 +168,7 @@ def _get_describefeaturetype_url(url, version, typename):
return url.split("?")[0] + "?" + urlqs


def _get_remote_describefeaturetype(url, timeout, headers, auth):
def _get_remote_describefeaturetype(url, timeout, headers, auth, proxies=None):
"""Gets the DescribeFeatureType response from the remote server.

:param str url: url of the service
Expand All @@ -177,5 +177,5 @@ def _get_remote_describefeaturetype(url, timeout, headers, auth):

:return etree.Element with the root of the DescribeFeatureType response
"""
res = openURL(url, timeout=timeout, headers=headers, auth=auth)
res = openURL(url, timeout=timeout, headers=headers, auth=auth, proxies=proxies)
return etree.fromstring(res.read())
17 changes: 10 additions & 7 deletions owslib/feature/wfs100.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def __init__(
parse_remote_metadata=False,
timeout=30,
headers=None,
proxies=None,
username=None,
password=None,
auth=None,
Expand All @@ -146,9 +147,10 @@ def __init__(
self.version = version
self.timeout = timeout
self.headers = headers
self.proxies = proxies
self.auth = auth or Authentication(username, password)
self._capabilities = None
reader = WFSCapabilitiesReader(self.version, headers=self.headers, auth=self.auth)
reader = WFSCapabilitiesReader(self.version, headers=self.headers, auth=self.auth, proxies=self.proxies)
if xml:
self._capabilities = reader.readString(xml)
else:
Expand Down Expand Up @@ -180,7 +182,7 @@ def _buildMetadata(self, parse_remote_metadata=False):
features = self._capabilities.findall(nspath("FeatureTypeList/FeatureType"))
for feature in features:
cm = ContentMetadata(
feature, featuretypelist, parse_remote_metadata, auth=self.auth
feature, featuretypelist, parse_remote_metadata, auth=self.auth, proxies=self.proxies
)
self.contents[cm.id] = cm

Expand All @@ -193,10 +195,10 @@ def getcapabilities(self):
"""Request and return capabilities document from the WFS as a
file-like object.
NOTE: this is effectively redundant now"""
reader = WFSCapabilitiesReader(self.version, auth=self.auth)
reader = WFSCapabilitiesReader(self.version, auth=self.auth, proxies=self.proxies)
return openURL(
reader.capabilities_url(self.url), timeout=self.timeout,
headers=self.headers, auth=self.auth
headers=self.headers, auth=self.auth, proxies=self.proxies
)

def items(self):
Expand Down Expand Up @@ -298,7 +300,7 @@ def getfeature(
data = urlencode(request)
log.debug("Making request: %s?%s" % (base_url, data))
u = openURL(base_url, data, method, timeout=self.timeout,
headers=self.headers, auth=self.auth)
headers=self.headers, auth=self.auth, proxies=self.proxies)

# check for service exceptions, rewrap, and return
# We're going to assume that anything with a content-length > 32k
Expand Down Expand Up @@ -379,14 +381,15 @@ class ContentMetadata(AbstractContentMetadata):
"""

def __init__(
self, elem, parent, parse_remote_metadata=False, timeout=30, auth=None
self, elem, parent, parse_remote_metadata=False, timeout=30, auth=None, proxies=None
):
"""."""
super(ContentMetadata, self).__init__(auth)
self.id = testXMLValue(elem.find(nspath("Name")))
self.title = testXMLValue(elem.find(nspath("Title")))
self.abstract = testXMLValue(elem.find(nspath("Abstract")))
self.keywords = [f.text for f in elem.findall(nspath("Keywords"))]
self.proxies=proxies

# bboxes
self.boundingBox = None
Expand Down Expand Up @@ -453,7 +456,7 @@ def parse_remote_metadata(self, timeout=30):
):
try:
content = openURL(
metadataUrl["url"], timeout=timeout, headers=self.headers, auth=self.auth
metadataUrl["url"], timeout=timeout, headers=self.headers, auth=self.auth, proxies=self.proxies
)
doc = etree.fromstring(content.read())
if metadataUrl["type"] == "FGDC":
Expand Down
Loading