-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbase_client.py
More file actions
281 lines (226 loc) · 10.1 KB
/
Copy pathbase_client.py
File metadata and controls
281 lines (226 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from __future__ import annotations
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Callable, Generator, Iterable, Iterator
from typing import (
TYPE_CHECKING,
Any,
Generic,
Protocol,
TypeVar,
)
from apify_client._logging import WithLogDetailsClient
from apify_client._types import ListPage
from apify_client._utils import to_safe_id
# Conditional import only executed when type checking, otherwise we'd get circular dependency issues
if TYPE_CHECKING:
from apify_client import ApifyClient, ApifyClientAsync
from apify_client._http_client import HTTPClient, HTTPClientAsync
T = TypeVar('T')
class _BaseBaseClient(metaclass=WithLogDetailsClient):
resource_id: str | None
url: str
params: dict
http_client: HTTPClient | HTTPClientAsync
root_client: ApifyClient | ApifyClientAsync
def _url(self, path: str | None = None, *, public: bool = False) -> str:
url = f'{self.url}/{path}' if path is not None else self.url
if public:
if not url.startswith(self.root_client.base_url):
raise ValueError('API based URL has to start with `self.root_client.base_url`')
return url.replace(self.root_client.base_url, self.root_client.public_base_url, 1)
return url
def _params(self, **kwargs: Any) -> dict:
return {
**self.params,
**kwargs,
}
def _sub_resource_init_options(self, **kwargs: Any) -> dict:
options = {
'base_url': self.url,
'http_client': self.http_client,
'params': self.params,
'root_client': self.root_client,
}
return {
**options,
**kwargs,
}
class BaseClient(_BaseBaseClient):
"""Base class for sub-clients."""
http_client: HTTPClient
root_client: ApifyClient
def __init__(
self,
*,
base_url: str,
root_client: ApifyClient,
http_client: HTTPClient,
resource_id: str | None = None,
resource_path: str,
params: dict | None = None,
) -> None:
"""Initialize a new instance.
Args:
base_url: Base URL of the API server.
root_client: The ApifyClient instance under which this resource client exists.
http_client: The HTTPClient instance to be used in this client.
resource_id: ID of the manipulated resource, in case of a single-resource client.
resource_path: Path to the resource's endpoint on the API server.
params: Parameters to include in all requests from this client.
"""
if resource_path.endswith('/'):
raise ValueError('resource_path must not end with "/"')
self.base_url = base_url
self.root_client = root_client
self.http_client = http_client
self.params = params or {}
self.resource_path = resource_path
self.resource_id = resource_id
self.url = f'{self.base_url}/{self.resource_path}'
if self.resource_id is not None:
self.safe_id = to_safe_id(self.resource_id)
self.url = f'{self.url}/{self.safe_id}'
@staticmethod
def _list_iterable_from_callback(callback: Callable[..., ListPage[T]], **kwargs: Any) -> ListPageProtocol[T]:
"""Return object can be awaited or iterated over.
Not using total from the API response as it can change during iteration.
"""
chunk_size = kwargs.pop('chunk_size', 0) or 0
offset = kwargs.get('offset') or 0
limit = kwargs.get('limit') or 0
list_page = callback(**{**kwargs, 'limit': _min_for_limit_param(kwargs.get('limit'), chunk_size)})
def iterator() -> Iterator[T]:
current_page = list_page
for item in current_page.items:
yield item
fetched_items = len(current_page.items)
while (
current_page.items # If there are any items left to fetch
and (not limit or (limit > fetched_items)) # If there is limit to fetch, and it was not reached it yet.
):
new_kwargs = {
**kwargs,
'offset': offset + fetched_items,
'limit': chunk_size if not limit else _min_for_limit_param(limit - fetched_items, chunk_size),
}
current_page = callback(**new_kwargs)
for item in current_page.items:
yield item
fetched_items += len(current_page.items)
return IterableListPage[T](list_page, iterator())
class BaseClientAsync(_BaseBaseClient):
"""Base class for async sub-clients."""
http_client: HTTPClientAsync
root_client: ApifyClientAsync
def __init__(
self,
*,
base_url: str,
root_client: ApifyClientAsync,
http_client: HTTPClientAsync,
resource_id: str | None = None,
resource_path: str,
params: dict | None = None,
) -> None:
"""Initialize a new instance.
Args:
base_url: Base URL of the API server.
root_client: The ApifyClientAsync instance under which this resource client exists.
http_client: The HTTPClientAsync instance to be used in this client.
resource_id: ID of the manipulated resource, in case of a single-resource client.
resource_path: Path to the resource's endpoint on the API server.
params: Parameters to include in all requests from this client.
"""
if resource_path.endswith('/'):
raise ValueError('resource_path must not end with "/"')
self.base_url = base_url
self.root_client = root_client
self.http_client = http_client
self.params = params or {}
self.resource_path = resource_path
self.resource_id = resource_id
self.url = f'{self.base_url}/{self.resource_path}'
if self.resource_id is not None:
self.safe_id = to_safe_id(self.resource_id)
self.url = f'{self.url}/{self.safe_id}'
@staticmethod
def _list_iterable_from_callback(
callback: Callable[..., Awaitable[ListPage[T]]], **kwargs: Any
) -> ListPageProtocolAsync[T]:
"""Return object can be awaited or iterated over.
Not using total from the API response as it can change during iteration.
"""
chunk_size = kwargs.pop('chunk_size', 0) or 0
offset = kwargs.get('offset') or 0
limit = kwargs.get('limit') or 0
list_page_awaitable = callback(**{**kwargs, 'limit': _min_for_limit_param(kwargs.get('limit'), chunk_size)})
async def async_iterator() -> AsyncIterator[T]:
current_page = await list_page_awaitable
for item in current_page.items:
yield item
fetched_items = len(current_page.items)
while (
current_page.items # If there are any items left to fetch
and (not limit or (limit > fetched_items)) # If there is limit to fetch, and it was not reached it yet.
):
new_kwargs = {
**kwargs,
'offset': offset + fetched_items,
'limit': chunk_size if not limit else _min_for_limit_param(limit - fetched_items, chunk_size),
}
current_page = await callback(**new_kwargs)
for item in current_page.items:
yield item
fetched_items += len(current_page.items)
return IterableListPageAsync[T](list_page_awaitable, async_iterator())
def _min_for_limit_param(a: int | None, b: int | None) -> int | None:
"""Return minimum of two limit parameters, treating None or 0 as infinity. Return None for infinity."""
# API treats 0 as None for limit parameter, in this context API understands 0 as infinity.
if a == 0:
a = None
if b == 0:
b = None
if a is None:
return b
if b is None:
return a
return min(a, b)
class ListPageProtocol(Iterable[T], Protocol[T]):
"""Protocol for an object that can be both awaited and asynchronously iterated over."""
items: list[T]
"""List of returned objects on this page."""
count: int
"""Count of the returned objects on this page."""
offset: int
"""The limit on the number of returned objects offset specified in the API call."""
limit: int
"""The offset of the first object specified in the API call"""
total: int
"""Total number of objects matching the API call criteria."""
desc: bool
"""Whether the listing is descending or not."""
class ListPageProtocolAsync(AsyncIterable[T], Awaitable[ListPage[T]], Protocol[T]):
"""Protocol for an object that can be both awaited and asynchronously iterated over."""
class IterableListPage(ListPage[T], Generic[T]):
"""Can be called to get ListPage with items or iterated over to get individual items."""
def __init__(self, list_page: ListPage[T], iterator: Iterator[T]) -> None:
self.items = list_page.items
self.offset = list_page.offset
self.limit = list_page.limit
self.count = list_page.count
self.total = list_page.total
self.desc = list_page.desc
self._iterator = iterator
def __iter__(self) -> Iterator[T]:
"""Return an iterator over the items from API, possibly doing multiple API calls."""
return self._iterator
class IterableListPageAsync(Generic[T]):
"""Can be awaited to get ListPage with items or asynchronously iterated over to get individual items."""
def __init__(self, awaitable: Awaitable[ListPage[T]], async_iterator: AsyncIterator[T]) -> None:
self._awaitable = awaitable
self._async_iterator = async_iterator
def __aiter__(self) -> AsyncIterator[T]:
"""Return an asynchronous iterator over the items from API, possibly doing multiple API calls."""
return self._async_iterator
def __await__(self) -> Generator[Any, Any, ListPage[T]]:
"""Return an awaitable that resolves to the ListPage doing exactly one API call."""
return self._awaitable.__await__()