diff --git a/owslib/wmts.py b/owslib/wmts.py index 7e537515..b7655283 100644 --- a/owslib/wmts.py +++ b/owslib/wmts.py @@ -136,7 +136,7 @@ def __getitem__(self, name): raise KeyError("No content named %s" % name) def __init__(self, url, version='1.0.0', xml=None, username=None, password=None, - parse_remote_metadata=False, vendor_kwargs=None, headers=None, auth=None, + parse_remote_metadata=False, vendor_kwargs=None, headers=None, auth=None, cookies=None, timeout=30): """Initialize. @@ -174,12 +174,13 @@ def __init__(self, url, version='1.0.0', xml=None, username=None, password=None, self.vendor_kwargs = vendor_kwargs self._capabilities = None self.headers = headers + self.cookies = cookies self.auth = auth or Authentication(username, password) self.timeout = timeout or 30 # Authentication handled by Reader reader = WMTSCapabilitiesReader( - self.version, url=self.url, headers=self.headers, auth=self.auth) + self.version, url=self.url, headers=self.headers, auth=self.auth, cookies=self.cookies) if xml is not None: # read from stored xml self._capabilities = reader.readString(xml) else: # read from server @@ -456,7 +457,7 @@ def gettile(self, base_url=None, layer=None, style=None, format=None, resurl = self.buildTileResource( layer, style, format, tilematrixset, tilematrix, row, column, **vendor_kwargs) - u = openURL(resurl, headers=self.headers, auth=self.auth, timeout=self.timeout) + u = openURL(resurl, headers=self.headers, cookies=self.cookies, auth=self.auth, timeout=self.timeout) return u # KVP implemetation @@ -482,7 +483,7 @@ def gettile(self, base_url=None, layer=None, style=None, format=None, base_url = get_verbs[0].get('url') except StopIteration: pass - u = openURL(base_url, data, headers=self.headers, auth=self.auth, timeout=self.timeout) + u = openURL(base_url, data, headers=self.headers, cookies=self.cookies, auth=self.auth, timeout=self.timeout) # check for service exceptions, and return if u.info()['Content-Type'] == 'application/vnd.ogc.se_xml': @@ -887,7 +888,7 @@ class WMTSCapabilitiesReader: """Read and parse capabilities document into a lxml.etree infoset """ - def __init__(self, version='1.0.0', url=None, un=None, pw=None, headers=None, auth=None): + def __init__(self, version='1.0.0', url=None, un=None, pw=None, headers=None, auth=None, cookies=None): """Initialize""" self.version = version self._infoset = None @@ -899,6 +900,7 @@ def __init__(self, version='1.0.0', url=None, un=None, pw=None, headers=None, au auth.password = pw self.auth = auth or Authentication(un, pw) self.headers = headers + self.cookies = cookies def capabilities_url(self, service_url, vendor_kwargs=None): """Return a capabilities url @@ -933,7 +935,7 @@ def read(self, service_url, vendor_kwargs=None): # now split it up again to use the generic openURL function... spliturl = getcaprequest.split('?') - u = openURL(spliturl[0], spliturl[1], method='Get', headers=self.headers, auth=self.auth) + u = openURL(spliturl[0], spliturl[1], method='Get', cookies=self.cookies, headers=self.headers, auth=self.auth) return getXMLTree(u) def readString(self, st): diff --git a/tests/test_wmts_cookies.py b/tests/test_wmts_cookies.py new file mode 100644 index 00000000..8c9a1ef4 --- /dev/null +++ b/tests/test_wmts_cookies.py @@ -0,0 +1,25 @@ +from tests.utils import scratch_file +from tests.utils import service_ok + +from owslib.wmts import WebMapTileService + +import pytest + +SERVICE_URL_EXAMPLE = 'https://mapsneu.wien.gv.at/basemapneu/1.0.0/WMTSCapabilities.xml' + + +@pytest.mark.online +@pytest.mark.skipif(not service_ok(SERVICE_URL_EXAMPLE, timeout=20), + reason="WMTS service is unreachable") +def test_wmts_cookies(): + from owslib.wmts import WebMapTileService + cookies = { + 'cookie1': 'example1', + 'cookie2': 'example2' + } + wmts = WebMapTileService(SERVICE_URL_EXAMPLE, cookies=cookies) + tile = wmts.gettile(layer="bmaporthofoto30cm", tilematrix="10", row=357, column=547) + tile_raw_cookies = tile._response.request.headers['Cookie'] + tile_cookies = (dict(i.split('=',1) for i in tile_raw_cookies.split('; '))) + assert tile_cookies.get('cookie1') == 'example1' + assert tile_cookies.get('cookie2') == 'example2' \ No newline at end of file