forked from apify/apify-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
503 lines (408 loc) · 19.6 KB
/
client.py
File metadata and controls
503 lines (408 loc) · 19.6 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
from __future__ import annotations
import warnings
from apify_client._http_client import HTTPClient, HTTPClientAsync
from apify_client._statistics import Statistics
from apify_client.clients import (
ActorClient,
ActorClientAsync,
ActorCollectionClient,
ActorCollectionClientAsync,
BuildClient,
BuildClientAsync,
BuildCollectionClient,
BuildCollectionClientAsync,
DatasetClient,
DatasetClientAsync,
DatasetCollectionClient,
DatasetCollectionClientAsync,
KeyValueStoreClient,
KeyValueStoreClientAsync,
KeyValueStoreCollectionClient,
KeyValueStoreCollectionClientAsync,
LogClient,
LogClientAsync,
RequestQueueClient,
RequestQueueClientAsync,
RequestQueueCollectionClient,
RequestQueueCollectionClientAsync,
RunClient,
RunClientAsync,
RunCollectionClient,
RunCollectionClientAsync,
ScheduleClient,
ScheduleClientAsync,
ScheduleCollectionClient,
ScheduleCollectionClientAsync,
StoreCollectionClient,
StoreCollectionClientAsync,
TaskClient,
TaskClientAsync,
TaskCollectionClient,
TaskCollectionClientAsync,
UserClient,
UserClientAsync,
WebhookClient,
WebhookClientAsync,
WebhookCollectionClient,
WebhookCollectionClientAsync,
WebhookDispatchClient,
WebhookDispatchClientAsync,
WebhookDispatchCollectionClient,
WebhookDispatchCollectionClientAsync,
)
DEFAULT_API_URL = 'https://api.apify.com'
DEFAULT_TIMEOUT = 360
API_VERSION = 'v2'
class _BaseApifyClient:
http_client: HTTPClient | HTTPClientAsync
def __init__(
self,
token: str | None = None,
*,
api_url: str | None = None,
api_public_url: str | None = None,
max_retries: int | None = 8,
min_delay_between_retries_millis: int | None = 500,
timeout_secs: int | None = DEFAULT_TIMEOUT,
) -> None:
"""Initialize a new instance.
Args:
token: The Apify API token.
api_url: The URL of the Apify API server to which to connect. Defaults to https://api.apify.com. It can
be an internal URL that is not globally accessible, in such case `api_public_url` should be set as well.
api_public_url: The globally accessible URL of the Apify API server. It should be set only if the `api_url`
is an internal URL that is not globally accessible.
max_retries: How many times to retry a failed request at most.
min_delay_between_retries_millis: How long will the client wait between retrying requests
(increases exponentially from this value).
timeout_secs: The socket timeout of the HTTP requests sent to the Apify API.
"""
self.token = token
api_url = (api_url or DEFAULT_API_URL).rstrip('/')
self.base_url = f'{api_url}/{API_VERSION}'
api_public_url = (api_public_url or DEFAULT_API_URL).rstrip('/')
self.public_base_url = f'{api_public_url}/{API_VERSION}'
self.max_retries = max_retries or 8
self.min_delay_between_retries_millis = min_delay_between_retries_millis or 500
self.timeout_secs = timeout_secs or DEFAULT_TIMEOUT
def _options(self) -> dict:
return {
'root_client': self,
'base_url': self.base_url,
'http_client': self.http_client,
}
def _check_custom_headers(self, headers: dict) -> None:
default_headers = {'Accept', 'Authorization', 'Accept-Encoding', 'User-Agent'}
overwrite_headers = [key for key in headers if key.title() in default_headers]
if overwrite_headers:
warnings.warn(
f'{", ".join(overwrite_headers)} headers of {self.__class__.__name__} was overridden with an '
'explicit value. A wrong header value can lead to API errors, it is recommended to use the default '
f'value for following headers: {", ".join(default_headers)}.',
category=UserWarning,
stacklevel=2,
)
class ApifyClient(_BaseApifyClient):
"""The Apify API client."""
http_client: HTTPClient
def __init__(
self,
token: str | None = None,
*,
api_url: str | None = None,
api_public_url: str | None = None,
max_retries: int | None = 8,
min_delay_between_retries_millis: int | None = 500,
timeout_secs: int | None = DEFAULT_TIMEOUT,
headers: dict | None = None,
) -> None:
"""Initialize a new instance.
Args:
token: The Apify API token.
api_url: The URL of the Apify API server to which to connect. Defaults to https://api.apify.com. It can
be an internal URL that is not globally accessible, in such case `api_public_url` should be set as well.
api_public_url: The globally accessible URL of the Apify API server. It should be set only if the `api_url`
is an internal URL that is not globally accessible.
max_retries: How many times to retry a failed request at most.
min_delay_between_retries_millis: How long will the client wait between retrying requests
(increases exponentially from this value).
timeout_secs: The socket timeout of the HTTP requests sent to the Apify API.
headers: Set headers to client for all requests.
"""
super().__init__(
token,
api_url=api_url,
api_public_url=api_public_url,
max_retries=max_retries,
min_delay_between_retries_millis=min_delay_between_retries_millis,
timeout_secs=timeout_secs,
)
self.stats = Statistics()
if headers:
self._check_custom_headers(headers)
self.http_client = HTTPClient(
token=token,
max_retries=self.max_retries,
min_delay_between_retries_millis=self.min_delay_between_retries_millis,
timeout_secs=self.timeout_secs,
stats=self.stats,
headers=headers,
)
def actor(self, actor_id: str) -> ActorClient:
"""Retrieve the sub-client for manipulating a single Actor.
Args:
actor_id: ID of the Actor to be manipulated.
"""
return ActorClient(resource_id=actor_id, **self._options())
def actors(self) -> ActorCollectionClient:
"""Retrieve the sub-client for manipulating Actors."""
return ActorCollectionClient(**self._options())
def build(self, build_id: str) -> BuildClient:
"""Retrieve the sub-client for manipulating a single Actor build.
Args:
build_id: ID of the Actor build to be manipulated.
"""
return BuildClient(resource_id=build_id, **self._options())
def builds(self) -> BuildCollectionClient:
"""Retrieve the sub-client for querying multiple builds of a user."""
return BuildCollectionClient(**self._options())
def run(self, run_id: str) -> RunClient:
"""Retrieve the sub-client for manipulating a single Actor run.
Args:
run_id: ID of the Actor run to be manipulated.
"""
return RunClient(resource_id=run_id, **self._options())
def runs(self) -> RunCollectionClient:
"""Retrieve the sub-client for querying multiple Actor runs of a user."""
return RunCollectionClient(**self._options())
def dataset(self, dataset_id: str) -> DatasetClient:
"""Retrieve the sub-client for manipulating a single dataset.
Args:
dataset_id: ID of the dataset to be manipulated.
"""
return DatasetClient(resource_id=dataset_id, **self._options())
def datasets(self) -> DatasetCollectionClient:
"""Retrieve the sub-client for manipulating datasets."""
return DatasetCollectionClient(**self._options())
def key_value_store(self, key_value_store_id: str) -> KeyValueStoreClient:
"""Retrieve the sub-client for manipulating a single key-value store.
Args:
key_value_store_id: ID of the key-value store to be manipulated.
"""
return KeyValueStoreClient(resource_id=key_value_store_id, **self._options())
def key_value_stores(self) -> KeyValueStoreCollectionClient:
"""Retrieve the sub-client for manipulating key-value stores."""
return KeyValueStoreCollectionClient(**self._options())
def request_queue(self, request_queue_id: str, *, client_key: str | None = None) -> RequestQueueClient:
"""Retrieve the sub-client for manipulating a single request queue.
Args:
request_queue_id: ID of the request queue to be manipulated.
client_key: A unique identifier of the client accessing the request queue.
"""
return RequestQueueClient(resource_id=request_queue_id, client_key=client_key, **self._options())
def request_queues(self) -> RequestQueueCollectionClient:
"""Retrieve the sub-client for manipulating request queues."""
return RequestQueueCollectionClient(**self._options())
def webhook(self, webhook_id: str) -> WebhookClient:
"""Retrieve the sub-client for manipulating a single webhook.
Args:
webhook_id: ID of the webhook to be manipulated.
"""
return WebhookClient(resource_id=webhook_id, **self._options())
def webhooks(self) -> WebhookCollectionClient:
"""Retrieve the sub-client for querying multiple webhooks of a user."""
return WebhookCollectionClient(**self._options())
def webhook_dispatch(self, webhook_dispatch_id: str) -> WebhookDispatchClient:
"""Retrieve the sub-client for accessing a single webhook dispatch.
Args:
webhook_dispatch_id: ID of the webhook dispatch to access.
"""
return WebhookDispatchClient(resource_id=webhook_dispatch_id, **self._options())
def webhook_dispatches(self) -> WebhookDispatchCollectionClient:
"""Retrieve the sub-client for querying multiple webhook dispatches of a user."""
return WebhookDispatchCollectionClient(**self._options())
def schedule(self, schedule_id: str) -> ScheduleClient:
"""Retrieve the sub-client for manipulating a single schedule.
Args:
schedule_id: ID of the schedule to be manipulated.
"""
return ScheduleClient(resource_id=schedule_id, **self._options())
def schedules(self) -> ScheduleCollectionClient:
"""Retrieve the sub-client for manipulating schedules."""
return ScheduleCollectionClient(**self._options())
def log(self, build_or_run_id: str) -> LogClient:
"""Retrieve the sub-client for retrieving logs.
Args:
build_or_run_id: ID of the Actor build or run for which to access the log.
"""
return LogClient(resource_id=build_or_run_id, **self._options())
def task(self, task_id: str) -> TaskClient:
"""Retrieve the sub-client for manipulating a single task.
Args:
task_id: ID of the task to be manipulated.
"""
return TaskClient(resource_id=task_id, **self._options())
def tasks(self) -> TaskCollectionClient:
"""Retrieve the sub-client for manipulating tasks."""
return TaskCollectionClient(**self._options())
def user(self, user_id: str | None = None) -> UserClient:
"""Retrieve the sub-client for querying users.
Args:
user_id: ID of user to be queried. If None, queries the user belonging to the token supplied to the client.
"""
return UserClient(resource_id=user_id, **self._options())
def store(self) -> StoreCollectionClient:
"""Retrieve the sub-client for Apify store."""
return StoreCollectionClient(**self._options())
class ApifyClientAsync(_BaseApifyClient):
"""The asynchronous version of the Apify API client."""
http_client: HTTPClientAsync
def __init__(
self,
token: str | None = None,
*,
api_url: str | None = None,
api_public_url: str | None = None,
max_retries: int | None = 8,
min_delay_between_retries_millis: int | None = 500,
timeout_secs: int | None = DEFAULT_TIMEOUT,
headers: dict | None = None,
) -> None:
"""Initialize a new instance.
Args:
token: The Apify API token.
api_url: The URL of the Apify API server to which to connect. Defaults to https://api.apify.com. It can
be an internal URL that is not globally accessible, in such case `api_public_url` should be set as well.
api_public_url: The globally accessible URL of the Apify API server. It should be set only if the `api_url`
is an internal URL that is not globally accessible.
max_retries: How many times to retry a failed request at most.
min_delay_between_retries_millis: How long will the client wait between retrying requests
(increases exponentially from this value).
timeout_secs: The socket timeout of the HTTP requests sent to the Apify API.
headers: Set headers to client for all requests.
"""
super().__init__(
token,
api_url=api_url,
api_public_url=api_public_url,
max_retries=max_retries,
min_delay_between_retries_millis=min_delay_between_retries_millis,
timeout_secs=timeout_secs,
)
self.stats = Statistics()
if headers:
self._check_custom_headers(headers)
self.http_client = HTTPClientAsync(
token=token,
max_retries=self.max_retries,
min_delay_between_retries_millis=self.min_delay_between_retries_millis,
timeout_secs=self.timeout_secs,
stats=self.stats,
headers=headers,
)
def actor(self, actor_id: str) -> ActorClientAsync:
"""Retrieve the sub-client for manipulating a single Actor.
Args:
actor_id: ID of the Actor to be manipulated.
"""
return ActorClientAsync(resource_id=actor_id, **self._options())
def actors(self) -> ActorCollectionClientAsync:
"""Retrieve the sub-client for manipulating Actors."""
return ActorCollectionClientAsync(**self._options())
def build(self, build_id: str) -> BuildClientAsync:
"""Retrieve the sub-client for manipulating a single Actor build.
Args:
build_id: ID of the Actor build to be manipulated.
"""
return BuildClientAsync(resource_id=build_id, **self._options())
def builds(self) -> BuildCollectionClientAsync:
"""Retrieve the sub-client for querying multiple builds of a user."""
return BuildCollectionClientAsync(**self._options())
def run(self, run_id: str) -> RunClientAsync:
"""Retrieve the sub-client for manipulating a single Actor run.
Args:
run_id: ID of the Actor run to be manipulated.
"""
return RunClientAsync(resource_id=run_id, **self._options())
def runs(self) -> RunCollectionClientAsync:
"""Retrieve the sub-client for querying multiple Actor runs of a user."""
return RunCollectionClientAsync(**self._options())
def dataset(self, dataset_id: str) -> DatasetClientAsync:
"""Retrieve the sub-client for manipulating a single dataset.
Args:
dataset_id: ID of the dataset to be manipulated.
"""
return DatasetClientAsync(resource_id=dataset_id, **self._options())
def datasets(self) -> DatasetCollectionClientAsync:
"""Retrieve the sub-client for manipulating datasets."""
return DatasetCollectionClientAsync(**self._options())
def key_value_store(self, key_value_store_id: str) -> KeyValueStoreClientAsync:
"""Retrieve the sub-client for manipulating a single key-value store.
Args:
key_value_store_id: ID of the key-value store to be manipulated.
"""
return KeyValueStoreClientAsync(resource_id=key_value_store_id, **self._options())
def key_value_stores(self) -> KeyValueStoreCollectionClientAsync:
"""Retrieve the sub-client for manipulating key-value stores."""
return KeyValueStoreCollectionClientAsync(**self._options())
def request_queue(self, request_queue_id: str, *, client_key: str | None = None) -> RequestQueueClientAsync:
"""Retrieve the sub-client for manipulating a single request queue.
Args:
request_queue_id: ID of the request queue to be manipulated.
client_key: A unique identifier of the client accessing the request queue.
"""
return RequestQueueClientAsync(resource_id=request_queue_id, client_key=client_key, **self._options())
def request_queues(self) -> RequestQueueCollectionClientAsync:
"""Retrieve the sub-client for manipulating request queues."""
return RequestQueueCollectionClientAsync(**self._options())
def webhook(self, webhook_id: str) -> WebhookClientAsync:
"""Retrieve the sub-client for manipulating a single webhook.
Args:
webhook_id: ID of the webhook to be manipulated.
"""
return WebhookClientAsync(resource_id=webhook_id, **self._options())
def webhooks(self) -> WebhookCollectionClientAsync:
"""Retrieve the sub-client for querying multiple webhooks of a user."""
return WebhookCollectionClientAsync(**self._options())
def webhook_dispatch(self, webhook_dispatch_id: str) -> WebhookDispatchClientAsync:
"""Retrieve the sub-client for accessing a single webhook dispatch.
Args:
webhook_dispatch_id: ID of the webhook dispatch to access.
"""
return WebhookDispatchClientAsync(resource_id=webhook_dispatch_id, **self._options())
def webhook_dispatches(self) -> WebhookDispatchCollectionClientAsync:
"""Retrieve the sub-client for querying multiple webhook dispatches of a user."""
return WebhookDispatchCollectionClientAsync(**self._options())
def schedule(self, schedule_id: str) -> ScheduleClientAsync:
"""Retrieve the sub-client for manipulating a single schedule.
Args:
schedule_id: ID of the schedule to be manipulated.
"""
return ScheduleClientAsync(resource_id=schedule_id, **self._options())
def schedules(self) -> ScheduleCollectionClientAsync:
"""Retrieve the sub-client for manipulating schedules."""
return ScheduleCollectionClientAsync(**self._options())
def log(self, build_or_run_id: str) -> LogClientAsync:
"""Retrieve the sub-client for retrieving logs.
Args:
build_or_run_id: ID of the Actor build or run for which to access the log.
"""
return LogClientAsync(resource_id=build_or_run_id, **self._options())
def task(self, task_id: str) -> TaskClientAsync:
"""Retrieve the sub-client for manipulating a single task.
Args:
task_id: ID of the task to be manipulated.
"""
return TaskClientAsync(resource_id=task_id, **self._options())
def tasks(self) -> TaskCollectionClientAsync:
"""Retrieve the sub-client for manipulating tasks."""
return TaskCollectionClientAsync(**self._options())
def user(self, user_id: str | None = None) -> UserClientAsync:
"""Retrieve the sub-client for querying users.
Args:
user_id: ID of user to be queried. If None, queries the user belonging to the token supplied to the client.
"""
return UserClientAsync(resource_id=user_id, **self._options())
def store(self) -> StoreCollectionClientAsync:
"""Retrieve the sub-client for Apify store."""
return StoreCollectionClientAsync(**self._options())