Skip to content
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
13 changes: 8 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [v0.3.5] - 2022-05-26

### Fixed

- Search against earth-search v0 failed with message "object of type 'Link' has no len()" [#179](https://github.com/stac-utils/pystac-client/pull/179)
## [Unreleased] - TBD

### Added

Expand All @@ -21,6 +17,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed

- Search sortby parameter now has correct typing and correctly handles both GET and POST JSON parameter formats. [#175](https://github.com/stac-utils/pystac-client/pull/175)
- Search fields parameter now has correct typing and correctly handles both GET and POST JSON parameter formats. [#184](https://github.com/stac-utils/pystac-client/pull/184)

## [v0.3.5] - 2022-05-26

### Fixed

- Search against earth-search v0 failed with message "object of type 'Link' has no len()" [#179](https://github.com/stac-utils/pystac-client/pull/179)

## [v0.3.4] - 2022-05-18

Expand Down
46 changes: 37 additions & 9 deletions pystac_client/item_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from datetime import datetime as datetime_
from datetime import timezone
from functools import lru_cache
from itertools import chain
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Optional, Tuple, Union

from dateutil.relativedelta import relativedelta
Expand Down Expand Up @@ -61,8 +62,8 @@
Sortby = List[Dict[str, str]]
SortbyLike = Union[Sortby, str, List[str]]

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

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

Expand Down Expand Up @@ -267,7 +268,9 @@ def get_parameters(self) -> Dict[str, Any]:
if "intersects" in params:
params["intersects"] = json.dumps(params["intersects"])
if "sortby" in params:
params["sortby"] = self.sortby_json_to_str(params["sortby"])
params["sortby"] = self.sortby_dict_to_str(params["sortby"])
if "fields" in params:
params["fields"] = self.fields_dict_to_str(params["fields"])
return params
else:
raise Exception(f"Unsupported method {self.method}")
Expand Down Expand Up @@ -455,11 +458,11 @@ def _format_sortby(self, value: Optional[SortbyLike]) -> Optional[Sortby]:
self._stac_io.assert_conforms_to(ConformanceClasses.SORT)

if isinstance(value, str):
return [self.sortby_part_to_json(part) for part in value.split(",")]
return [self.sortby_part_to_dict(part) for part in value.split(",")]

if isinstance(value, list):
if value and isinstance(value[0], str):
return [self.sortby_part_to_json(v) for v in value]
return [self.sortby_part_to_dict(v) for v in value]
elif value and isinstance(value[0], dict):
return value

Expand All @@ -468,7 +471,7 @@ def _format_sortby(self, value: Optional[SortbyLike]) -> Optional[Sortby]:
)

@staticmethod
def sortby_part_to_json(part: str) -> Dict[str, str]:
def sortby_part_to_dict(part: str) -> Dict[str, str]:
if part.startswith("-"):
return {"field": part[1:], "direction": "desc"}
elif part.startswith("+"):
Expand All @@ -477,7 +480,7 @@ def sortby_part_to_json(part: str) -> Dict[str, str]:
return {"field": part, "direction": "asc"}

@staticmethod
def sortby_json_to_str(sortby: Sortby) -> str:
def sortby_dict_to_str(sortby: Sortby) -> str:
return ",".join(
[
f"{'+' if sort['direction'] == 'asc' else '-'}{sort['field']}"
Expand All @@ -492,9 +495,34 @@ def _format_fields(self, value: Optional[FieldsLike]) -> Optional[Fields]:
self._stac_io.assert_conforms_to(ConformanceClasses.FIELDS)

if isinstance(value, str):
return tuple(value.split(","))
return self.fields_to_dict(value.split(","))
if isinstance(value, list):
return self.fields_to_dict(value)
if isinstance(value, dict):
return value

return tuple(value)
raise Exception(
"sortby must be of type None, str, List[str], or List[Dict[str, str]"
)

@staticmethod
def fields_to_dict(fields: List[str]) -> Fields:
includes: List[str] = []
excludes: List[str] = []
for field in fields:
if field.startswith("-"):
excludes.append(field[1:])
elif field.startswith("+"):
includes.append(field[1:])
else:
includes.append(field)
return {"includes": includes, "excludes": excludes}

@staticmethod
def fields_dict_to_str(fields: Fields) -> str:
includes = [f"+{x}" for x in fields.get("includes", [])]
excludes = [f"-{x}" for x in fields.get("excludes", [])]
return ",".join(chain(includes, excludes))

@staticmethod
def _format_intersects(value: Optional[IntersectsLike]) -> Optional[Intersects]:
Expand Down
46 changes: 46 additions & 0 deletions tests/test_item_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,52 @@ def test_sortby(self) -> None:
with pytest.raises(Exception):
ItemSearch(url=SEARCH_URL, sortby=[1])

def test_fields(self):

with pytest.raises(Exception):
ItemSearch(url=SEARCH_URL, fields=1)

with pytest.raises(Exception):
ItemSearch(url=SEARCH_URL, fields=[1])

search = ItemSearch(url=SEARCH_URL, fields="id,collection,+foo,-bar")
assert search.get_parameters()["fields"] == {
"excludes": ["bar"],
"includes": ["id", "collection", "foo"],
}

search = ItemSearch(url=SEARCH_URL, fields=["id", "collection", "+foo", "-bar"])
assert search.get_parameters()["fields"] == {
"excludes": ["bar"],
"includes": ["id", "collection", "foo"],
}

search = ItemSearch(
url=SEARCH_URL,
fields={"excludes": ["bar"], "includes": ["id", "collection"]},
)
assert search.get_parameters()["fields"] == {
"excludes": ["bar"],
"includes": ["id", "collection"],
}

search = ItemSearch(
url=SEARCH_URL, method="GET", fields="id,collection,+foo,-bar"
)
assert search.get_parameters()["fields"] == "+id,+collection,+foo,-bar"

search = ItemSearch(
url=SEARCH_URL, method="GET", fields=["id", "collection", "+foo", "-bar"]
)
assert search.get_parameters()["fields"] == "+id,+collection,+foo,-bar"

search = ItemSearch(
url=SEARCH_URL,
method="GET",
fields={"excludes": ["bar"], "includes": ["id", "collection"]},
)
assert search.get_parameters()["fields"] == "+id,+collection,-bar"


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