Skip to content

Commit c0490a7

Browse files
committed
Add parameters argument to StacApiIO init
1 parent 30d793a commit c0490a7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pystac_client/stac_api_io.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636

3737

3838
class StacApiIO(DefaultStacIO):
39-
def __init__(self, headers: Optional[Dict] = None, conformance: Optional[List[str]] = None):
39+
def __init__(
40+
self,
41+
headers: Optional[Dict] = None,
42+
conformance: Optional[List[str]] = None,
43+
parameters: Optional[Dict] = None,
44+
):
4045
"""Initialize class for API IO
4146
4247
Args:
@@ -48,6 +53,7 @@ def __init__(self, headers: Optional[Dict] = None, conformance: Optional[List[st
4853
# TODO - this should super() to parent class
4954
self.session = Session()
5055
self.session.headers.update(headers or {})
56+
self.session.params.update(parameters or {})
5157

5258
self._conformance = conformance
5359

tests/test_stac_api_io.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from urllib.parse import parse_qs, urlsplit
2+
13
import pytest
24
from pystac_client.conformance import ConformanceClasses
35

@@ -75,3 +77,32 @@ def test_custom_headers(self, requests_mock):
7577
assert len(history) == 1
7678
assert header_name in history[0].headers
7779
assert history[0].headers[header_name] == header_value
80+
81+
def test_custom_query_params(self, requests_mock):
82+
"""Checks that query params passed to the init method are added to requests."""
83+
init_qp_name = "my-param"
84+
init_qp_value = "something"
85+
url = "https://some-url.com/some-file.json"
86+
stac_api_io = StacApiIO(parameters={init_qp_name: init_qp_value})
87+
88+
request_qp_name = "another-param"
89+
request_qp_value = "another_value"
90+
requests_mock.get(url, status_code=200, json={})
91+
92+
stac_api_io.read_json(url, parameters={request_qp_name: request_qp_value})
93+
94+
history = requests_mock.request_history
95+
assert len(history) == 1
96+
97+
actual_qs = urlsplit(history[0].url).query
98+
actual_qp = parse_qs(actual_qs)
99+
100+
# Check that the param from the init method is present
101+
assert init_qp_name in actual_qp
102+
assert len(actual_qp[init_qp_name]) == 1
103+
assert actual_qp[init_qp_name][0] == init_qp_value
104+
105+
# Check that the param from the request is present
106+
assert request_qp_name in actual_qp
107+
assert len(actual_qp[request_qp_name]) == 1
108+
assert actual_qp[request_qp_name][0] == request_qp_value

0 commit comments

Comments
 (0)