Skip to content

Add option to pass cookies to WMTS #995

New issue

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

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

Already on GitHub? # to your account

Merged
merged 2 commits into from
May 22, 2025
Merged
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
14 changes: 8 additions & 6 deletions owslib/wmts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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':
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
25 changes: 25 additions & 0 deletions tests/test_wmts_cookies.py
Original file line number Diff line number Diff line change
@@ -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'