Skip to content
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

Allow query parameters #118

Merged
merged 5 commits into from
Nov 18, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Adds `--block-network` option to all test commands to ensure no network requests are made during unit tests
[#119](https://github.com/stac-utils/pystac-client/pull/119)
- `parameters` argument to `StacApiIO`, `Client.open`, and `Client.from_file` to allow query string parameters to be passed to all requests
[#118](https://github.com/stac-utils/pystac-client/pull/118)

### Fixed

Expand Down
26 changes: 16 additions & 10 deletions pystac_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ def __repr__(self):
return '<Client id={}>'.format(self.id)

@classmethod
def open(cls,
url: str,
headers: Dict[str, str] = None,
ignore_conformance: bool = False) -> "Client":
def open(
cls,
url: str,
headers: Dict[str, str] = None,
parameters: Optional[Dict[str, Any]] = None,
ignore_conformance: bool = False,
) -> "Client":
"""Opens a STAC Catalog or API
This function will read the root catalog of a STAC Catalog or API

Expand All @@ -44,7 +47,7 @@ def open(cls,
Return:
catalog : A :class:`Client` instance for this Catalog/API
"""
cat = cls.from_file(url, headers=headers)
cat = cls.from_file(url, headers=headers, parameters=parameters)
search_link = cat.get_links('search')
# if there is a search link, but no conformsTo advertised, ignore conformance entirely
# NOTE: this behavior to be deprecated as implementations become conformant
Expand All @@ -54,17 +57,20 @@ def open(cls,
return cat

@classmethod
def from_file(cls,
href: str,
stac_io: Optional[pystac.StacIO] = None,
headers: Optional[Dict] = {}) -> "Client":
def from_file(
cls,
href: str,
stac_io: Optional[pystac.StacIO] = None,
headers: Optional[Dict] = {},
parameters: Optional[Dict] = None,
) -> "Client":
"""Open a STAC Catalog/API

Returns:
Client: A Client (PySTAC Catalog) of the root Catalog for this Catalog/API
"""
if stac_io is None:
stac_io = StacApiIO(headers=headers)
stac_io = StacApiIO(headers=headers, parameters=parameters)

cat = super().from_file(href, stac_io)

Expand Down
11 changes: 10 additions & 1 deletion pystac_client/stac_api_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,27 @@


class StacApiIO(DefaultStacIO):
def __init__(self, headers: Optional[Dict] = None, conformance: Optional[List[str]] = None):
def __init__(
self,
headers: Optional[Dict] = None,
conformance: Optional[List[str]] = None,
parameters: Optional[Dict] = None,
):
"""Initialize class for API IO

Args:
headers : Optional dictionary of headers to include in all requests
conformance : Optional list of `Conformance Classes
<https://github.com/radiantearth/stac-api-spec/blob/master/overview.md#conformance-classes>`__.
parameters: Optional dictionary of query string parameters to include in all requests.

Return:
StacApiIO : StacApiIO instance
"""
# TODO - this should super() to parent class
self.session = Session()
self.session.headers.update(headers or {})
self.session.params.update(parameters or {})

self._conformance = conformance

Expand Down
Loading