|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from collections.abc import Callable, Iterator |
| 3 | +from collections.abc import Callable, Iterator, AsyncIterator, Awaitable, Generator |
4 | 4 | from dataclasses import dataclass, InitVar, field |
5 | | -from typing import TYPE_CHECKING, Generic, TypeVar |
6 | | - |
| 5 | +from typing import TYPE_CHECKING, Generic, TypeVar, Self, Any |
7 | 6 |
|
| 7 | +from apify_client._iterable_list_page import HasItems |
8 | 8 | from apify_client._models_generated import ( |
9 | 9 | ActorShort, |
10 | 10 | BuildShort, |
|
28 | 28 |
|
29 | 29 | @dataclass |
30 | 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 |
| 31 | + _get_iterator: Callable[[], Iterator[T]] = field(repr=False, compare=False) |
37 | 32 |
|
38 | 33 | def __iter__(self) -> Iterator[T]: |
39 | 34 | return self._get_iterator() |
40 | 35 |
|
| 36 | +@dataclass |
| 37 | +class IterablePageAsync(Generic[T]): |
| 38 | + _get_async_iterator: Callable[[], AsyncIterator[T]] = field(repr=False, compare=False) |
| 39 | + |
| 40 | + def __aiter__(self) -> AsyncIterator[T]: |
| 41 | + return self._get_async_iterator() |
| 42 | + |
| 43 | +@dataclass |
| 44 | +class AwaitablePage(Generic[T]): |
| 45 | + _awaitable_first_page: Awaitable[PaginatedPage[T]] = field(repr=False, compare=False) |
| 46 | + |
| 47 | + def __await__(self) -> Generator[Any, Any, PaginatedPage[T]]: |
| 48 | + return self._awaitable_first_page.__await__() |
| 49 | + |
41 | 50 |
|
42 | 51 | @dataclass |
43 | | -class PaginatedPage: |
| 52 | +class PageWithItems(Generic[T]): |
| 53 | + items: list[T] |
| 54 | + |
| 55 | +@dataclass |
| 56 | +class PageWithTotal(Generic[T]): |
| 57 | + total: int |
| 58 | + |
| 59 | +@dataclass |
| 60 | +class PageWithLimit(Generic[T]): |
| 61 | + limit: int |
| 62 | + |
| 63 | +@dataclass |
| 64 | +class PageWithCount(Generic[T]): |
44 | 65 | count: int |
| 66 | + |
| 67 | +@dataclass |
| 68 | +class PaginatedPage(PageWithItems[T], PageWithTotal, PageWithLimit, PageWithCount): |
45 | 69 | offset: int |
46 | | - limit: int |
47 | | - total: int |
48 | 70 | desc: bool |
49 | 71 |
|
50 | 72 | @dataclass |
51 | | -class ListPageOfActors(PaginatedPage, IterablePage[ActorShort]): ... |
| 73 | +class PaginatedPageOnlyTotal(PageWithItems[T], PageWithTotal): ... |
52 | 74 |
|
53 | 75 | @dataclass |
54 | | -class ListPageOfBuilds(PaginatedPage, IterablePage[BuildShort]): ... |
| 76 | +class ListPageOfActors(PaginatedPage[ActorShort], IterablePage[ActorShort]): ... |
55 | 77 |
|
56 | 78 | @dataclass |
57 | | -class ListPageOfDatasets(PaginatedPage, IterablePage[DatasetListItem]): ... |
| 79 | +class ListPageOfBuilds(PaginatedPage[BuildShort], IterablePage[BuildShort]): ... |
58 | 80 |
|
59 | 81 | @dataclass |
60 | | -class ListPageOfKeyValueStores(PaginatedPage, IterablePage[KeyValueStore]): ... |
| 82 | +class ListPageOfDatasets(PaginatedPage[DatasetListItem], IterablePage[DatasetListItem]): ... |
61 | 83 |
|
62 | 84 | @dataclass |
63 | | -class ListPageOfRequestQueues(PaginatedPage, IterablePage[RequestQueueShort]): ... |
| 85 | +class ListPageOfKeyValueStores(PaginatedPage[KeyValueStore], IterablePage[KeyValueStore]): ... |
64 | 86 |
|
65 | 87 | @dataclass |
66 | | -class ListPageOfRuns(PaginatedPage, IterablePage[RunShort]): ... |
| 88 | +class ListPageOfRequestQueues(PaginatedPage[RequestQueueShort], IterablePage[RequestQueueShort]): ... |
67 | 89 |
|
68 | 90 | @dataclass |
69 | | -class ListPageOfSchedules(PaginatedPage, IterablePage[ScheduleShort]): ... |
| 91 | +class ListPageOfRuns(PaginatedPage[RunShort], IterablePage[RunShort]): ... |
70 | 92 |
|
71 | 93 | @dataclass |
72 | | -class ListPageOfStoreActors(PaginatedPage, IterablePage[StoreListActor]): ... |
| 94 | +class ListPageOfSchedules(PaginatedPage[ScheduleShort], IterablePage[ScheduleShort]): ... |
73 | 95 |
|
74 | 96 | @dataclass |
75 | | -class ListPageOfTasks(PaginatedPage, IterablePage[TaskShort]): ... |
| 97 | +class ListPageOfStoreActors(PaginatedPage[StoreListActor], IterablePage[StoreListActor]): ... |
76 | 98 |
|
77 | 99 | @dataclass |
78 | | -class ListPageOfWebhookDispatches(PaginatedPage, IterablePage[WebhookDispatch]): ... |
| 100 | +class ListPageOfTasks(PaginatedPage[TaskShort], IterablePage[TaskShort]): ... |
79 | 101 |
|
80 | 102 | @dataclass |
81 | | -class ListPageOfWebhooks(PaginatedPage, IterablePage[WebhookShort]): ... |
| 103 | +class ListPageOfWebhookDispatches(PaginatedPage[WebhookDispatch], IterablePage[WebhookDispatch]): ... |
82 | 104 |
|
83 | 105 | @dataclass |
84 | | -class ListPageOfEnvVars(IterablePage[EnvVar]): |
85 | | - total: int |
| 106 | +class ListPageOfWebhookDispatchesAsync(IterablePageAsync[WebhookDispatch], AwaitablePage[WebhookDispatch]): ... |
| 107 | + |
| 108 | +@dataclass |
| 109 | +class ListPageOfWebhooks(PaginatedPage[WebhookShort], IterablePage[WebhookShort]): ... |
86 | 110 |
|
| 111 | +@dataclass |
| 112 | +class ListPageOfEnvVars(IterablePage[EnvVar], PaginatedPageOnlyTotal): ... |
87 | 113 |
|
88 | 114 | @dataclass |
89 | | -class ListPageOfVersions(IterablePage[Version]): |
90 | | - total: int |
| 115 | +class ListPageOfVersions(IterablePage[Version], PaginatedPageOnlyTotal): ... |
91 | 116 |
|
92 | 117 | @dataclass |
93 | 118 | class ListPageOfRequests(IterablePage[Request]): |
|
0 commit comments