Skip to content

Commit

Permalink
Merge pull request #169 from stac-utils/pv/set-filter-lang-correctly-…
Browse files Browse the repository at this point in the history
…based-on-parameter-type

set filter-lang parameter correctly based on the filter parameter type
  • Loading branch information
Phil Varner authored May 25, 2022
2 parents 0b310f0 + 3eef61f commit e6a9567
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Relaxed upper bound on PySTAC dependency [#144](https://github.com/stac-utils/pystac-client/pull/144)
- Bumped PySTAC dependency to >= 1.4.0 [#147](https://github.com/stac-utils/pystac-client/pull/147)
- Search `filter-lang` defaults to `cql2-json` instead of `cql-json`
- Search `filter-lang` will be set to `cql2-json` if the `filter` is a dict, or `cql2-text` if it is a string

## Removed

- Client parameter `require_geojson_link` has been removed.

## [v0.3.2] - 2022-01-11

Expand Down
2 changes: 1 addition & 1 deletion pystac_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def get_all_items(self) -> Iterable["Item_Type"]:
else:
yield from super().get_items()

def search(self, require_geojson_link: bool = False, **kwargs: Any) -> ItemSearch:
def search(self, **kwargs: Any) -> ItemSearch:
"""Query the ``/search`` endpoint using the given parameters.
This method returns an :class:`~pystac_client.ItemSearch` instance, see that class's documentation
Expand Down
26 changes: 16 additions & 10 deletions pystac_client/item_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@
QueryLike = Union[Query, List[str]]

FilterLangLike = str

FilterLike = dict
FilterLike = Union[dict, str]

Sortby = List[str]
SortbyLike = Union[Sortby, str]

Fields = List[str]
FieldsLike = Union[Fields, str]

OP_MAP = {'>=': 'gte', '<=': 'lte', '=': 'eq', '>': 'gt', '<': 'lt'}


# from https://gist.github.com/angstwad/bf22d1822c38a92ec0a9#gistcomment-2622319
def dict_merge(dct: Dict, merge_dct: Dict, add_keys: bool = True) -> Dict:
Expand Down Expand Up @@ -138,7 +139,7 @@ class ItemSearch:
of the provided Collections will be searched
query: List or JSON of query parameters as per the STAC API `query` extension
filter: JSON of query parameters as per the STAC API `filter` extension
filter_lang: Language variant used in the filter body. Defaults to 'cql-json'.
filter_lang: Language variant used in the filter body. If `filter` is a dictionary or not provided, defaults to 'cql2-json'. If `filter` is a string, defaults to `cql2-text`.
sortby: A single field or list of fields to sort the response by
fields: A list of fields to return in the response. Note this may result in invalid JSON.
Use `get_all_items_as_dict` to avoid errors
Expand Down Expand Up @@ -221,8 +222,6 @@ def _format_query(value: List[QueryLike]) -> Optional[dict]:
if value is None:
return None

OP_MAP = {'>=': 'gte', '<=': 'lte', '=': 'eq', '>': 'gt', '<': 'lt'}

if isinstance(value, list):
query = {}
for q in value:
Expand All @@ -240,14 +239,21 @@ def _format_query(value: List[QueryLike]) -> Optional[dict]:

return query

def _format_filter_lang(self, filter: FilterLike, value: FilterLangLike) -> Optional[str]:
if filter is None:
@staticmethod
def _format_filter_lang(_filter: FilterLike, value: FilterLangLike) -> Optional[str]:
if _filter is None:
return None

if value is None:
return 'cql-json'
if value is not None:
return value

return value
if isinstance(_filter, str):
return 'cql2-text'

if isinstance(_filter, dict):
return 'cql2-json'

return None

def _format_filter(self, value: FilterLike) -> Optional[dict]:
if value is None:
Expand Down
18 changes: 13 additions & 5 deletions tests/test_item_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,22 @@ def test_intersects_json_string(self):
search = ItemSearch(url=SEARCH_URL, intersects=json.dumps(INTERSECTS_EXAMPLE))
assert search._parameters['intersects'] == INTERSECTS_EXAMPLE

def test_filter_lang_default(self):
# No filter_lang specified
def test_filter_lang_default_for_dict(self):
search = ItemSearch(url=SEARCH_URL, filter={})
assert search._parameters['filter-lang'] == 'cql-json'
assert search._parameters['filter-lang'] == 'cql2-json'

def test_filter_lang_default_for_str(self):
search = ItemSearch(url=SEARCH_URL, filter="")
assert search._parameters['filter-lang'] == 'cql2-text'

def test_filter_lang_cql2_text(self):
# Use specified filter_lang
search = ItemSearch(url=SEARCH_URL, filter_lang="cql2-text", filter={})
assert search._parameters['filter-lang'] == 'cql2-text'

def test_filter_lang(self):
def test_filter_lang_cql2_json(self):
# Use specified filter_lang
search = ItemSearch(url=SEARCH_URL, filter_lang="cql2-json", filter={})
search = ItemSearch(url=SEARCH_URL, filter_lang="cql2-json", filter="")
assert search._parameters['filter-lang'] == 'cql2-json'

def test_filter_lang_without_filter(self):
Expand Down

0 comments on commit e6a9567

Please sign in to comment.