Skip to content

Commit 52bb46f

Browse files
asherggAsher Gross-Gellman
and
Asher Gross-Gellman
authored
Add option to pass cookies to WMTS (#995)
* Add option to pass cookies to WMTS * wmts cookies test --------- Co-authored-by: Asher Gross-Gellman <asher@eliabs.com>
1 parent 1d011de commit 52bb46f

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

owslib/wmts.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def __getitem__(self, name):
136136
raise KeyError("No content named %s" % name)
137137

138138
def __init__(self, url, version='1.0.0', xml=None, username=None, password=None,
139-
parse_remote_metadata=False, vendor_kwargs=None, headers=None, auth=None,
139+
parse_remote_metadata=False, vendor_kwargs=None, headers=None, auth=None, cookies=None,
140140
timeout=30):
141141
"""Initialize.
142142
@@ -174,12 +174,13 @@ def __init__(self, url, version='1.0.0', xml=None, username=None, password=None,
174174
self.vendor_kwargs = vendor_kwargs
175175
self._capabilities = None
176176
self.headers = headers
177+
self.cookies = cookies
177178
self.auth = auth or Authentication(username, password)
178179
self.timeout = timeout or 30
179180

180181
# Authentication handled by Reader
181182
reader = WMTSCapabilitiesReader(
182-
self.version, url=self.url, headers=self.headers, auth=self.auth)
183+
self.version, url=self.url, headers=self.headers, auth=self.auth, cookies=self.cookies)
183184
if xml is not None: # read from stored xml
184185
self._capabilities = reader.readString(xml)
185186
else: # read from server
@@ -456,7 +457,7 @@ def gettile(self, base_url=None, layer=None, style=None, format=None,
456457
resurl = self.buildTileResource(
457458
layer, style, format, tilematrixset, tilematrix,
458459
row, column, **vendor_kwargs)
459-
u = openURL(resurl, headers=self.headers, auth=self.auth, timeout=self.timeout)
460+
u = openURL(resurl, headers=self.headers, cookies=self.cookies, auth=self.auth, timeout=self.timeout)
460461
return u
461462

462463
# KVP implemetation
@@ -482,7 +483,7 @@ def gettile(self, base_url=None, layer=None, style=None, format=None,
482483
base_url = get_verbs[0].get('url')
483484
except StopIteration:
484485
pass
485-
u = openURL(base_url, data, headers=self.headers, auth=self.auth, timeout=self.timeout)
486+
u = openURL(base_url, data, headers=self.headers, cookies=self.cookies, auth=self.auth, timeout=self.timeout)
486487

487488
# check for service exceptions, and return
488489
if u.info()['Content-Type'] == 'application/vnd.ogc.se_xml':
@@ -887,7 +888,7 @@ class WMTSCapabilitiesReader:
887888
"""Read and parse capabilities document into a lxml.etree infoset
888889
"""
889890

890-
def __init__(self, version='1.0.0', url=None, un=None, pw=None, headers=None, auth=None):
891+
def __init__(self, version='1.0.0', url=None, un=None, pw=None, headers=None, auth=None, cookies=None):
891892
"""Initialize"""
892893
self.version = version
893894
self._infoset = None
@@ -899,6 +900,7 @@ def __init__(self, version='1.0.0', url=None, un=None, pw=None, headers=None, au
899900
auth.password = pw
900901
self.auth = auth or Authentication(un, pw)
901902
self.headers = headers
903+
self.cookies = cookies
902904

903905
def capabilities_url(self, service_url, vendor_kwargs=None):
904906
"""Return a capabilities url
@@ -933,7 +935,7 @@ def read(self, service_url, vendor_kwargs=None):
933935

934936
# now split it up again to use the generic openURL function...
935937
spliturl = getcaprequest.split('?')
936-
u = openURL(spliturl[0], spliturl[1], method='Get', headers=self.headers, auth=self.auth)
938+
u = openURL(spliturl[0], spliturl[1], method='Get', cookies=self.cookies, headers=self.headers, auth=self.auth)
937939
return getXMLTree(u)
938940

939941
def readString(self, st):

tests/test_wmts_cookies.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from tests.utils import scratch_file
2+
from tests.utils import service_ok
3+
4+
from owslib.wmts import WebMapTileService
5+
6+
import pytest
7+
8+
SERVICE_URL_EXAMPLE = 'https://mapsneu.wien.gv.at/basemapneu/1.0.0/WMTSCapabilities.xml'
9+
10+
11+
@pytest.mark.online
12+
@pytest.mark.skipif(not service_ok(SERVICE_URL_EXAMPLE, timeout=20),
13+
reason="WMTS service is unreachable")
14+
def test_wmts_cookies():
15+
from owslib.wmts import WebMapTileService
16+
cookies = {
17+
'cookie1': 'example1',
18+
'cookie2': 'example2'
19+
}
20+
wmts = WebMapTileService(SERVICE_URL_EXAMPLE, cookies=cookies)
21+
tile = wmts.gettile(layer="bmaporthofoto30cm", tilematrix="10", row=357, column=547)
22+
tile_raw_cookies = tile._response.request.headers['Cookie']
23+
tile_cookies = (dict(i.split('=',1) for i in tile_raw_cookies.split('; ')))
24+
assert tile_cookies.get('cookie1') == 'example1'
25+
assert tile_cookies.get('cookie2') == 'example2'

0 commit comments

Comments
 (0)