Skip to content

Update stac_api_io w/ headers, etc #439

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

Merged
merged 3 commits into from
Mar 6, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed

- Fix parse fail when header has multiple '=' characters [#440](https://github.com/stac-utils/pystac-client/pull/440)
- `Client.open` and `Client.from_file` now apply `headers`, etc to existing `stac_io` instances ([#439](https://github.com/stac-utils/pystac-client/pull/439))

## [v0.6.0] - 2023-01-27

Expand Down
6 changes: 6 additions & 0 deletions pystac_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ def from_file( # type: ignore
parameters=parameters,
request_modifier=request_modifier,
)
else:
stac_io.update(
headers=headers,
parameters=parameters,
request_modifier=request_modifier,
)

client: Client = super().from_file(href, stac_io) # type: ignore

Expand Down
25 changes: 22 additions & 3 deletions pystac_client/stac_api_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,30 @@ def __init__(
"""
# TODO - this should super() to parent class
self.session = Session()
self.session.headers.update(headers or {})
self.session.params.update(parameters or {}) # type: ignore

self._conformance = conformance
self.update(
headers=headers, parameters=parameters, request_modifier=request_modifier
)

def update(
self,
headers: Optional[Dict[str, str]] = None,
parameters: Optional[Dict[str, Any]] = None,
request_modifier: Optional[Callable[[Request], Union[Request, None]]] = None,
) -> None:
"""Updates this StacApi's headers, parameters, and/or request_modifer.

Args:
headers : Optional dictionary of headers to include in all requests
parameters: Optional dictionary of query string parameters to
include in all requests.
request_modifier: Optional callable that can be used to modify Request
objects before they are sent. If provided, the callable receives a
`request.Request` and must either modify the object directly or return
a new / modified request instance.
"""
self.session.headers.update(headers or {})
self.session.params.update(parameters or {}) # type: ignore
self._req_modifier = request_modifier

def read_text(self, source: pystac.link.HREF, *args: Any, **kwargs: Any) -> str:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pystac_client._utils import Modifiable
from pystac_client.conformance import ConformanceClasses
from pystac_client.errors import ClientTypeError, IgnoredResultWarning
from pystac_client.stac_api_io import StacApiIO

from .helpers import STAC_URLS, TEST_DATA, read_data_file

Expand Down Expand Up @@ -446,6 +447,19 @@ def test_opening_a_collection(self) -> None:
with pytest.raises(ClientTypeError):
Client.open(path)

def test_headers_with_custom_stac_io(self, requests_mock: Mocker) -> None:
pc_root_dict = read_data_file("planetary-computer-root.json", parse_json=True)
requests_mock.get(
STAC_URLS["PLANETARY-COMPUTER"],
status_code=200,
json=pc_root_dict,
request_headers={"ski": "pow", "shred": "gnar"},
)
stac_io = StacApiIO(headers={"ski": "pow"})
_ = Client.open(
STAC_URLS["PLANETARY-COMPUTER"], headers={"shred": "gnar"}, stac_io=stac_io
)


class TestAPISearch:
@pytest.fixture(scope="function")
Expand Down