Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

test pagination #45

Merged
merged 3 commits into from
Mar 28, 2024
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
11 changes: 9 additions & 2 deletions cads_api_client/catalogue.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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")


Expand Down
7 changes: 6 additions & 1 deletion cads_api_client/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ dependencies:
- attrs
- multiurl
- requests
- typing-extensions
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
11 changes: 8 additions & 3 deletions tests/integration_test_10_catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
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
assert isinstance(res.json["collections"], list)
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:
Expand Down