Skip to content

Commit c5122e9

Browse files
committed
Tedious type safety
1 parent c8a42b8 commit c5122e9

3 files changed

Lines changed: 121 additions & 7 deletions

File tree

‎src/apify_client/_iterable_list_page.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ def __await__(self) -> Generator[Any, Any, ListPage[T]]:
130130

131131
def build_iterable_list_page(
132132
callback: Callable[..., HasItems[T]],
133+
first_page: HasItems[T],
133134
**kwargs: Any,
134-
) -> IterableListPage[T]:
135+
) -> Callable[[], Iterator[T]]:
135136
"""Build an `IterableListPage` from a paginated sync callback.
136137
137138
The callback is invoked once immediately to fetch the first page, and again lazily during
@@ -158,9 +159,6 @@ def build_iterable_list_page(
158159
chunk_size = kwargs.pop('chunk_size', 0) or 0
159160
offset = kwargs.get('offset') or 0
160161
limit = kwargs.get('limit') or 0
161-
162-
first_page = callback(**{**kwargs, 'limit': _min_for_limit_param(kwargs.get('limit'), chunk_size)})
163-
164162
def get_iterator() -> Iterator[T]:
165163
current_page = first_page
166164
yield from current_page.items
@@ -176,7 +174,7 @@ def get_iterator() -> Iterator[T]:
176174
yield from current_page.items
177175
fetched_items += getattr(current_page, 'count', len(current_page.items))
178176

179-
return IterableListPage(first_page, get_iterator)
177+
return get_iterator
180178

181179

182180
def build_iterable_list_page_async(
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Callable, Iterator
4+
from dataclasses import dataclass, InitVar, field
5+
from typing import TYPE_CHECKING, Generic, TypeVar
6+
7+
8+
from apify_client._models_generated import (
9+
ActorShort,
10+
BuildShort,
11+
DatasetListItem,
12+
EnvVar,
13+
KeyValueStore,
14+
KeyValueStoreKey,
15+
Request,
16+
RequestQueueShort,
17+
RunShort,
18+
ScheduleShort,
19+
StoreListActor,
20+
TaskShort,
21+
Version,
22+
WebhookDispatch,
23+
WebhookShort,
24+
)
25+
26+
T = TypeVar('T')
27+
28+
29+
@dataclass
30+
class IterablePage(Generic[T]):
31+
items: list[T]
32+
get_iterator: InitVar[Callable[[], Iterator[T]]]
33+
_get_iterator: Callable[[], Iterator[T]] = field(init=False, repr=False, compare=False)
34+
35+
def __post_init__(self, get_iterator: Callable[[], Iterator[T]]) -> None:
36+
self._get_iterator = get_iterator
37+
38+
def __iter__(self) -> Iterator[T]:
39+
return self._get_iterator()
40+
41+
42+
@dataclass
43+
class PaginatedPage:
44+
count: int
45+
offset: int
46+
limit: int
47+
total: int
48+
desc: bool
49+
50+
@dataclass
51+
class ListPageOfActors(PaginatedPage, IterablePage[ActorShort]): ...
52+
53+
@dataclass
54+
class ListPageOfBuilds(PaginatedPage, IterablePage[BuildShort]): ...
55+
56+
@dataclass
57+
class ListPageOfDatasets(PaginatedPage, IterablePage[DatasetListItem]): ...
58+
59+
@dataclass
60+
class ListPageOfKeyValueStores(PaginatedPage, IterablePage[KeyValueStore]): ...
61+
62+
@dataclass
63+
class ListPageOfRequestQueues(PaginatedPage, IterablePage[RequestQueueShort]): ...
64+
65+
@dataclass
66+
class ListPageOfRuns(PaginatedPage, IterablePage[RunShort]): ...
67+
68+
@dataclass
69+
class ListPageOfSchedules(PaginatedPage, IterablePage[ScheduleShort]): ...
70+
71+
@dataclass
72+
class ListPageOfStoreActors(PaginatedPage, IterablePage[StoreListActor]): ...
73+
74+
@dataclass
75+
class ListPageOfTasks(PaginatedPage, IterablePage[TaskShort]): ...
76+
77+
@dataclass
78+
class ListPageOfWebhookDispatches(PaginatedPage, IterablePage[WebhookDispatch]): ...
79+
80+
@dataclass
81+
class ListPageOfWebhooks(PaginatedPage, IterablePage[WebhookShort]): ...
82+
83+
@dataclass
84+
class ListPageOfEnvVars(IterablePage[EnvVar]):
85+
total: int
86+
87+
88+
@dataclass
89+
class ListPageOfVersions(IterablePage[Version]):
90+
total: int
91+
92+
@dataclass
93+
class ListPageOfRequests(IterablePage[Request]):
94+
limit: int
95+
count: int | None = None
96+
exclusive_start_id: str | None = None
97+
cursor: str | None = None
98+
next_cursor: str | None = None
99+
100+
@dataclass
101+
class ListPageOfKeys(IterablePage[KeyValueStoreKey]):
102+
count: int
103+
limit: int
104+
is_truncated: bool
105+
exclusive_start_key: str | None = None
106+
next_exclusive_start_key: str | None = None

‎src/apify_client/_resource_clients/webhook_dispatch_collection.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
build_iterable_list_page_async,
1111
)
1212
from apify_client._models_generated import ListOfWebhookDispatches, WebhookDispatchList
13+
from apify_client._pagination_classes import ListPageOfWebhookDispatches
1314
from apify_client._resource_clients._resource_client import ResourceClient, ResourceClientAsync
1415

1516
if TYPE_CHECKING:
@@ -43,7 +44,7 @@ def list(
4344
offset: int | None = None,
4445
desc: bool | None = None,
4546
timeout: Timeout = 'medium',
46-
) -> IterableListPage[WebhookDispatch]:
47+
) -> ListPageOfWebhookDispatches:
4748
"""List all webhook dispatches of a user.
4849
4950
The returned page also supports iteration: `for item in client.list(...)` yields individual
@@ -65,7 +66,16 @@ def _callback(**kwargs: Any) -> ListOfWebhookDispatches:
6566
result = self._list(timeout=timeout, **kwargs)
6667
return WebhookDispatchList.model_validate(result).data
6768

68-
return build_iterable_list_page(_callback, limit=limit, offset=offset, desc=desc)
69+
first_page = _callback(limit=limit, offset=offset, desc=desc)
70+
get_iterator = build_iterable_list_page(_callback,first_page, limit=limit, offset=offset, desc=desc)
71+
72+
return ListPageOfWebhookDispatches(get_iterator=get_iterator,
73+
items=first_page.items,
74+
count=first_page.count,
75+
offset=first_page.offset,
76+
limit=first_page.limit,
77+
total=first_page.total,
78+
desc=first_page.desc)
6979

7080

7181
@docs_group('Resource clients')

0 commit comments

Comments
 (0)