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

Add methods for setting parameter options #75

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
115 changes: 115 additions & 0 deletions cmr/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,121 @@ def bearer_token(self, bearer_token: str) -> Self:
self.headers.update({"Authorization": f"Bearer {bearer_token}"})

return self

def option_ignore_case(self, parameter: str, value: bool = True) -> Self:
"""
Set this query's `ignore_case` parameter option for the specified parameter.

See `CMR Search API Search Options`_.

.. _CMR Search API Search Options:
https://cmr.earthdata.nasa.gov/search/site/docs/search/api.html#parameter-options

:param parameter: search parameter to specify `ignore_case` option for
:param value: specify `True` (default) to retrieve results matching _any_ of the
values set for the parameter; `False` to retrieve _only_ results matching
_all_ values set for the parameter
:returns: self
:raises: `ValueError` if invalid parameter provided
"""

if not parameter:
return self

valid_parameters = dict(getmembers(self, predicate=ismethod)).keys()

if parameter not in valid_parameters:
raise ValueError(f"Unknown parameter: {parameter}")

self.options[parameter] = {"ignore_case": value}

return self

def option_and(self, parameter: str, value: bool = True) -> Self:
"""
Set this query's `and` parameter option for the specified parameter.

See `CMR Search API Search Options`_.

.. _CMR Search API Search Options:
https://cmr.earthdata.nasa.gov/search/site/docs/search/api.html#parameter-options

:param parameter: search parameter to specify `and` option for
:param value: specify `True` (default) to retrieve results matching _any_ of the
values set for the parameter; `False` to retrieve _only_ results matching
_all_ values set for the parameter
:returns: self
:raises: `ValueError` if invalid parameter provided
"""

if not parameter:
return self

valid_parameters = dict(getmembers(self, predicate=ismethod)).keys()

if parameter not in valid_parameters:
raise ValueError(f"Unknown parameter: {parameter}")

self.options[parameter] = {"and": value}

return self

def option_or(self, parameter: str, value: bool = True) -> Self:
"""
Set this query's `or` parameter option for the specified parameter.

See `CMR Search API Search Options`_.

.. _CMR Search API Search Options:
https://cmr.earthdata.nasa.gov/search/site/docs/search/api.html#parameter-options

:param parameter: search parameter to specify `or` option for
:param value: specify `True` (default) to retrieve results matching _any_ of the
values set for the parameter; `False` to retrieve _only_ results matching
_all_ values set for the parameter
:returns: self
:raises: `ValueError` if invalid parameter provided
"""
if not parameter:
return self

valid_parameters = dict(getmembers(self, predicate=ismethod)).keys()

if parameter not in valid_parameters:
raise ValueError(f"Unknown parameter: {parameter}")

self.options[parameter] = {"or": value}

return self

def option_pattern(self, parameter: str, value: bool = True) -> Self:
"""
Set this query's `pattern` parameter option for the specified parameter.

See `CMR Search API Search Options`_.

.. _CMR Search API Search Options:
https://cmr.earthdata.nasa.gov/search/site/docs/search/api.html#parameter-options

:param parameter: search parameter to specify `pattern` option for
:param value: specify `True` (default) to retrieve results matching _any_ of the
values set for the parameter; `False` to retrieve _only_ results matching
_all_ values set for the parameter
:returns: self
:raises: `ValueError` if invalid parameter provided
"""

if not parameter:
return self

valid_parameters = dict(getmembers(self, predicate=ismethod)).keys()

if parameter not in valid_parameters:
raise ValueError(f"Unknown parameter: {parameter}")

self.options[parameter] = {"pattern": value}

return self


class GranuleCollectionBaseQuery(Query):
Expand Down
Loading