diff --git a/cads_api_client/catalogue.py b/cads_api_client/catalogue.py index 894f733..57f2335 100644 --- a/cads_api_client/catalogue.py +++ b/cads_api_client/catalogue.py @@ -1,6 +1,13 @@ +from __future__ import annotations + import datetime from typing import Any, Dict, List, Optional +try: + from typing import Self +except ImportError: + from typing_extensions import Self + import attrs import requests @@ -12,10 +19,10 @@ class Collections(processing.ApiResponse): def collection_ids(self) -> List[str]: return [collection["id"] for collection in self.json["collections"]] - def next(self) -> Optional[processing.ApiResponse]: + def next(self) -> Optional[Self]: return self.from_rel_href(rel="next") - def prev(self) -> Optional[processing.ApiResponse]: + def prev(self) -> Optional[Self]: return self.from_rel_href(rel="prev") diff --git a/cads_api_client/processing.py b/cads_api_client/processing.py index 369a2e2..0df3e91 100644 --- a/cads_api_client/processing.py +++ b/cads_api_client/processing.py @@ -7,6 +7,11 @@ import urllib from typing import Any, Dict, List, Optional, Type, TypeVar +try: + from typing import Self +except ImportError: + from typing_extensions import Self + import attrs import multiurl import requests @@ -89,7 +94,7 @@ def get_link_href(self, **kwargs: str) -> str: raise RuntimeError(f"link not found or not unique {kwargs}") return links[0]["href"] - def from_rel_href(self, rel: str) -> Optional[ApiResponse]: + def from_rel_href(self, rel: str) -> Optional[Self]: rels = self.get_links(rel=rel) assert len(rels) <= 1 if len(rels) == 1: diff --git a/environment.yml b/environment.yml index 771ee9e..067ceca 100644 --- a/environment.yml +++ b/environment.yml @@ -11,3 +11,4 @@ dependencies: - attrs - multiurl - requests +- typing-extensions diff --git a/pyproject.toml b/pyproject.toml index c8cacc4..9cecafa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,7 +15,7 @@ classifiers = [ "Programming Language :: Python :: 3.11", "Topic :: Scientific/Engineering" ] -dependencies = ["attrs", "multiurl", "requests"] +dependencies = ["attrs", "multiurl", "requests", "typing-extensions"] description = "CADS API Python client" dynamic = ["version"] license = {file = "LICENSE"} diff --git a/tests/integration_test_10_catalogue.py b/tests/integration_test_10_catalogue.py index 9c57cbe..3d578fb 100644 --- a/tests/integration_test_10_catalogue.py +++ b/tests/integration_test_10_catalogue.py @@ -4,7 +4,7 @@ def test_collections(api_root_url: str) -> None: cat = catalogue.Catalogue(f"{api_root_url}/catalogue") - res = cat.collections() + res: catalogue.Collections | None = cat.collections() assert isinstance(res, catalogue.Collections) assert "collections" in res.json @@ -12,9 +12,14 @@ def test_collections(api_root_url: str) -> None: assert "links" in res.json assert isinstance(res.json["links"], list) - expected_collection_id = "reanalysis-era5-single-levels" + collection_ids = res.collection_ids() + while len(collection_ids) != res.json["numberMatched"]: + res = res.next() + assert res is not None + collection_ids.extend(res.collection_ids()) - assert expected_collection_id in res.collection_ids() + expected_collection_id = "reanalysis-era5-single-levels" + assert expected_collection_id in collection_ids def test_collections_limit(api_root_url: str) -> None: