Skip to content

Commit

Permalink
Merge pull request #128 from mmcfarland/mjm/filter-lang-override
Browse files Browse the repository at this point in the history
Allow providing filter-lang to search
  • Loading branch information
matthewhanson authored Jan 4, 2022
2 parents 3d2a2e8 + 2e24e62 commit 06ef534
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- `Client.search` accepts an optional `filter_lang` argument for `filter` requests [#128](https://github.com/stac-utils/pystac-client/pull/128)

### Fixed
- Values from `parameters` and `headers` arguments to `Client.open` and `Client.from_file` are now also used in requests made from `CollectionClient` instances
fetched from the same API ([#126](https://github.com/stac-utils/pystac-client/pull/126))
Expand Down
17 changes: 14 additions & 3 deletions pystac_client/item_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
Query = dict
QueryLike = Union[Query, List[str]]

FilterLangLike = str

FilterLike = dict

Sortby = List[str]
Expand Down Expand Up @@ -135,6 +137,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'.
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 All @@ -154,6 +157,7 @@ def __init__(self,
collections: Optional[CollectionsLike] = None,
query: Optional[QueryLike] = None,
filter: Optional[FilterLike] = None,
filter_lang: Optional[FilterLangLike] = None,
sortby: Optional[SortbyLike] = None,
fields: Optional[FieldsLike] = None,
max_items: Optional[int] = None,
Expand Down Expand Up @@ -187,13 +191,11 @@ def __init__(self,
'intersects': self._format_intersects(intersects),
'query': self._format_query(query),
'filter': self._format_filter(filter),
'filter-lang': self._format_filter_lang(filter, filter_lang),
'sortby': self._format_sortby(sortby),
'fields': self._format_fields(fields)
}

if params['filter'] is not None:
params['filter-lang'] = 'cql-json'

self._parameters = {k: v for k, v in params.items() if v is not None}

def get_parameters(self):
Expand Down Expand Up @@ -237,6 +239,15 @@ 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:
return None

if value is None:
return 'cql-json'

return value

def _format_filter(self, value: FilterLike) -> Optional[dict]:
if value is None:
return None
Expand Down
15 changes: 15 additions & 0 deletions tests/test_item_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@ 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
search = ItemSearch(url=SEARCH_URL, filter={})
assert search._parameters['filter-lang'] == 'cql-json'

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

def test_filter_lang_without_filter(self):
# No filter provided
search = ItemSearch(url=SEARCH_URL)
assert 'filter-lang' not in search._parameters


class TestItemSearch:
@pytest.fixture(scope='function')
Expand Down

0 comments on commit 06ef534

Please sign in to comment.