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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Search `filter-lang` defaults to `cql2-json` instead of `cql-json` [#169](https://github.com/stac-utils/pystac-client/pull/169)
- Search `filter-lang` will be set to `cql2-json` if the `filter` is a dict, or `cql2-text` if it is a string [#169](https://github.com/stac-utils/pystac-client/pull/169)
- Search parameter `intersects` is now typed to only accept a str, dict, or object that implements `__geo_interface__` [#169](https://github.com/stac-utils/pystac-client/pull/169)
- Better error message when trying to open a Collection with `Client.open` [#222](https://github.com/stac-utils/pystac-client/pull/222)


### Deprecated
Expand Down
20 changes: 20 additions & 0 deletions pystac_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pystac_client.collection_client import CollectionClient
from pystac_client.conformance import ConformanceClasses
from pystac_client.errors import ClientTypeError
from pystac_client.exceptions import APIError
from pystac_client.item_search import ItemSearch
from pystac_client.stac_api_io import StacApiIO
Expand Down Expand Up @@ -93,6 +94,25 @@ def from_file(

return cat

@classmethod
def from_dict(
cls,
d: Dict[str, Any],
href: Optional[str] = None,
root: Optional[pystac.Catalog] = None,
migrate: bool = False,
preserve_dict: bool = True,
) -> "Client":
try:
return super().from_dict(
d=d, href=href, root=root, migrate=migrate, preserve_dict=preserve_dict
)
except pystac.STACTypeError:
raise ClientTypeError(
f"Could not open Client (href={href}), "
f"expected type=Catalog, found type={d.get('type', None)}"
)

@lru_cache()
def get_collection(self, collection_id: str) -> CollectionClient:
"""Get a single collection from this Catalog/API
Expand Down
4 changes: 4 additions & 0 deletions pystac_client/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ClientTypeError(Exception):
"""Raised when trying to open a Client on a non-catalog STAC Object."""

pass
6 changes: 6 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from pystac_client import Client
from pystac_client.conformance import ConformanceClasses
from pystac_client.errors import ClientTypeError

from .helpers import STAC_URLS, TEST_DATA, read_data_file

Expand Down Expand Up @@ -326,6 +327,11 @@ def test_get_collections_without_conformance(self, requests_mock):
assert len(history) == 2
assert history[1].url == pc_collection_href

def test_opening_a_collection(self) -> None:
path = str(TEST_DATA / "planetary-computer-aster-l1t-collection.json")
with pytest.raises(ClientTypeError):
Client.open(path)


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