scrapely-client-python 3.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of scrapely-client-python might be problematic. Click here for more details.

Files changed (65) hide show
  1. scrapely_client/__init__.py +30 -0
  2. scrapely_client/_charging.py +186 -0
  3. scrapely_client/_client_registry.py +136 -0
  4. scrapely_client/_consts.py +96 -0
  5. scrapely_client/_docs.py +37 -0
  6. scrapely_client/_literals.py +539 -0
  7. scrapely_client/_logging.py +180 -0
  8. scrapely_client/_models.py +4172 -0
  9. scrapely_client/_pagination.py +220 -0
  10. scrapely_client/_resource_clients/__init__.py +84 -0
  11. scrapely_client/_resource_clients/_resource_client.py +567 -0
  12. scrapely_client/_resource_clients/actor.py +1042 -0
  13. scrapely_client/_resource_clients/actor_collection.py +377 -0
  14. scrapely_client/_resource_clients/actor_env_var.py +154 -0
  15. scrapely_client/_resource_clients/actor_env_var_collection.py +165 -0
  16. scrapely_client/_resource_clients/actor_version.py +254 -0
  17. scrapely_client/_resource_clients/actor_version_collection.py +232 -0
  18. scrapely_client/_resource_clients/build.py +244 -0
  19. scrapely_client/_resource_clients/build_collection.py +164 -0
  20. scrapely_client/_resource_clients/dataset.py +1151 -0
  21. scrapely_client/_resource_clients/dataset_collection.py +244 -0
  22. scrapely_client/_resource_clients/key_value_store.py +906 -0
  23. scrapely_client/_resource_clients/key_value_store_collection.py +243 -0
  24. scrapely_client/_resource_clients/log.py +225 -0
  25. scrapely_client/_resource_clients/request_queue.py +1167 -0
  26. scrapely_client/_resource_clients/request_queue_collection.py +240 -0
  27. scrapely_client/_resource_clients/run.py +875 -0
  28. scrapely_client/_resource_clients/run_collection.py +226 -0
  29. scrapely_client/_resource_clients/schedule.py +243 -0
  30. scrapely_client/_resource_clients/schedule_collection.py +260 -0
  31. scrapely_client/_resource_clients/store_collection.py +242 -0
  32. scrapely_client/_resource_clients/task.py +793 -0
  33. scrapely_client/_resource_clients/task_collection.py +322 -0
  34. scrapely_client/_resource_clients/user.py +262 -0
  35. scrapely_client/_resource_clients/webhook.py +292 -0
  36. scrapely_client/_resource_clients/webhook_collection.py +281 -0
  37. scrapely_client/_resource_clients/webhook_dispatch.py +84 -0
  38. scrapely_client/_resource_clients/webhook_dispatch_collection.py +158 -0
  39. scrapely_client/_scrapely_client.py +837 -0
  40. scrapely_client/_statistics.py +29 -0
  41. scrapely_client/_status_message_watcher.py +197 -0
  42. scrapely_client/_streamed_log.py +250 -0
  43. scrapely_client/_typeddicts.py +408 -0
  44. scrapely_client/_utils/__init__.py +0 -0
  45. scrapely_client/_utils/crypto.py +87 -0
  46. scrapely_client/_utils/encoding.py +133 -0
  47. scrapely_client/_utils/errors.py +33 -0
  48. scrapely_client/_utils/http.py +137 -0
  49. scrapely_client/_utils/time.py +32 -0
  50. scrapely_client/_utils/try_import.py +61 -0
  51. scrapely_client/errors.py +190 -0
  52. scrapely_client/http_clients/__init__.py +10 -0
  53. scrapely_client/http_clients/_base.py +430 -0
  54. scrapely_client/http_clients/_impit.py +598 -0
  55. scrapely_client/http_compressors/__init__.py +18 -0
  56. scrapely_client/http_compressors/_base.py +25 -0
  57. scrapely_client/http_compressors/_brotli.py +38 -0
  58. scrapely_client/http_compressors/_gzip.py +38 -0
  59. scrapely_client/http_compressors/_resolve.py +38 -0
  60. scrapely_client/py.typed +0 -0
  61. scrapely_client/types.py +52 -0
  62. scrapely_client_python-3.1.3.dist-info/METADATA +269 -0
  63. scrapely_client_python-3.1.3.dist-info/RECORD +65 -0
  64. scrapely_client_python-3.1.3.dist-info/WHEEL +4 -0
  65. scrapely_client_python-3.1.3.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,1042 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Any, Literal
4
+
5
+ from pydantic import TypeAdapter
6
+
7
+ from scrapely_client._docs import docs_group
8
+ from scrapely_client._models import (
9
+ Actor,
10
+ ActorResponse,
11
+ ActorStandby,
12
+ Build,
13
+ BuildResponse,
14
+ CreateOrUpdateVersionRequest,
15
+ DefaultRunOptions,
16
+ ExampleRunInput,
17
+ FlatPricePerMonthActorPricingInfo,
18
+ FreeActorPricingInfo,
19
+ PayPerEventActorPricingInfo,
20
+ PricePerDatasetItemActorPricingInfo,
21
+ Run,
22
+ RunResponse,
23
+ UpdateActorRequest,
24
+ )
25
+ from scrapely_client._resource_clients._resource_client import ResourceClient, ResourceClientAsync
26
+ from scrapely_client._utils.encoding import encode_key_value_store_record_value, encode_webhooks_to_base64
27
+ from scrapely_client._utils.http import response_to_dict
28
+ from scrapely_client._utils.time import to_seconds
29
+
30
+ if TYPE_CHECKING:
31
+ from datetime import timedelta
32
+ from decimal import Decimal
33
+ from logging import Logger
34
+
35
+ from scrapely_client._literals import ActorJobStatus, ActorPermissionLevel, RunOrigin
36
+ from scrapely_client._resource_clients import (
37
+ ActorVersionClient,
38
+ ActorVersionClientAsync,
39
+ ActorVersionCollectionClient,
40
+ ActorVersionCollectionClientAsync,
41
+ BuildClient,
42
+ BuildClientAsync,
43
+ BuildCollectionClient,
44
+ BuildCollectionClientAsync,
45
+ RunClient,
46
+ RunClientAsync,
47
+ RunCollectionClient,
48
+ RunCollectionClientAsync,
49
+ WebhookCollectionClient,
50
+ WebhookCollectionClientAsync,
51
+ )
52
+ from scrapely_client.types import Timeout, WebhooksList
53
+
54
+ _PricingInfo = (
55
+ PayPerEventActorPricingInfo
56
+ | PricePerDatasetItemActorPricingInfo
57
+ | FlatPricePerMonthActorPricingInfo
58
+ | FreeActorPricingInfo
59
+ )
60
+ _pricing_info_list_adapter = TypeAdapter(list[_PricingInfo])
61
+
62
+
63
+ @docs_group('Resource clients')
64
+ class ActorClient(ResourceClient):
65
+ """Sub-client for managing a specific Actor.
66
+
67
+ Provides methods to manage a specific Actor, e.g. update it, delete it, build it, or start runs. Obtain an instance
68
+ via an appropriate method on the `ApifyClient` class.
69
+ """
70
+
71
+ def __init__(
72
+ self,
73
+ *,
74
+ resource_id: str,
75
+ resource_path: str = 'actors',
76
+ **kwargs: Any,
77
+ ) -> None:
78
+ super().__init__(
79
+ resource_id=resource_id,
80
+ resource_path=resource_path,
81
+ **kwargs,
82
+ )
83
+
84
+ def get(self, *, timeout: Timeout = 'short') -> Actor | None:
85
+ """Retrieve the Actor.
86
+
87
+
88
+ Args:
89
+ timeout: Timeout for the API HTTP request.
90
+
91
+ Returns:
92
+ The retrieved Actor.
93
+ """
94
+ result = self._get(timeout=timeout)
95
+ if result is None:
96
+ return None
97
+ return ActorResponse.model_validate(result).data
98
+
99
+ def update(
100
+ self,
101
+ *,
102
+ name: str | None = None,
103
+ title: str | None = None,
104
+ description: str | None = None,
105
+ seo_title: str | None = None,
106
+ seo_description: str | None = None,
107
+ versions: list[dict[str, Any]] | None = None,
108
+ restart_on_error: bool | None = None,
109
+ is_public: bool | None = None,
110
+ is_deprecated: bool | None = None,
111
+ categories: list[str] | None = None,
112
+ default_run_build: str | None = None,
113
+ default_run_max_items: int | None = None,
114
+ default_run_memory_mbytes: int | None = None,
115
+ default_run_timeout: timedelta | None = None,
116
+ example_run_input_body: Any = None,
117
+ example_run_input_content_type: str | None = None,
118
+ actor_standby_is_enabled: bool | None = None,
119
+ actor_standby_desired_requests_per_actor_run: int | None = None,
120
+ actor_standby_max_requests_per_actor_run: int | None = None,
121
+ actor_standby_idle_timeout: timedelta | None = None,
122
+ actor_standby_build: str | None = None,
123
+ actor_standby_memory_mbytes: int | None = None,
124
+ pricing_infos: list[dict[str, Any]] | None = None,
125
+ actor_permission_level: ActorPermissionLevel | None = None,
126
+ tagged_builds: dict[str, dict[str, str] | None] | None = None,
127
+ timeout: Timeout = 'short',
128
+ ) -> Actor:
129
+ """Update the Actor with the specified fields.
130
+
131
+
132
+ Args:
133
+ name: The name of the Actor.
134
+ title: The title of the Actor (human-readable).
135
+ description: The description for the Actor.
136
+ seo_title: The title of the Actor optimized for search engines.
137
+ seo_description: The description of the Actor optimized for search engines.
138
+ versions: The list of Actor versions.
139
+ restart_on_error: If true, the Actor run process will be restarted whenever it exits with
140
+ a non-zero status code.
141
+ is_public: Whether the Actor is public.
142
+ is_deprecated: Whether the Actor is deprecated.
143
+ categories: The categories to which the Actor belongs to.
144
+ default_run_build: Tag or number of the build that you want to run by default.
145
+ default_run_max_items: Default limit of the number of results that will be returned
146
+ by runs of this Actor, if the Actor is charged per result.
147
+ default_run_memory_mbytes: Default amount of memory allocated for the runs of this Actor, in megabytes.
148
+ default_run_timeout: Default timeout for the runs of this Actor.
149
+ example_run_input_body: Input to be prefilled as default input to new users of this Actor.
150
+ example_run_input_content_type: The content type of the example run input.
151
+ actor_standby_is_enabled: Whether the Actor Standby is enabled.
152
+ actor_standby_desired_requests_per_actor_run: The desired number of concurrent HTTP requests for
153
+ a single Actor Standby run.
154
+ actor_standby_max_requests_per_actor_run: The maximum number of concurrent HTTP requests for
155
+ a single Actor Standby run.
156
+ actor_standby_idle_timeout: If the Actor run does not receive any requests for this time,
157
+ it will be shut down.
158
+ actor_standby_build: The build tag or number to run when the Actor is in Standby mode.
159
+ actor_standby_memory_mbytes: The memory in megabytes to use when the Actor is in Standby mode.
160
+ pricing_infos: A list of objects that describes the pricing of the Actor.
161
+ actor_permission_level: The permission level of the Actor on Scrapely platform.
162
+ tagged_builds: A dictionary mapping build tag names to their settings. Use it to create, update,
163
+ or remove build tags. To assign a tag, provide a dict with 'buildId' key. To remove a tag,
164
+ set its value to None. Example: {'latest': {'buildId': 'abc'}, 'beta': None}.
165
+ timeout: Timeout for the API HTTP request.
166
+
167
+ Returns:
168
+ The updated Actor.
169
+ """
170
+ actor_fields = UpdateActorRequest(
171
+ name=name,
172
+ title=title,
173
+ description=description,
174
+ seo_title=seo_title,
175
+ seo_description=seo_description,
176
+ versions=[CreateOrUpdateVersionRequest.model_validate(v) for v in versions] if versions else None,
177
+ is_public=is_public,
178
+ is_deprecated=is_deprecated,
179
+ categories=categories,
180
+ pricing_infos=_pricing_info_list_adapter.validate_python(pricing_infos) if pricing_infos else None,
181
+ actor_permission_level=actor_permission_level,
182
+ default_run_options=DefaultRunOptions(
183
+ build=default_run_build,
184
+ max_items=default_run_max_items,
185
+ memory_mbytes=default_run_memory_mbytes,
186
+ timeout_secs=to_seconds(default_run_timeout, as_int=True),
187
+ restart_on_error=restart_on_error,
188
+ ),
189
+ actor_standby=ActorStandby(
190
+ is_enabled=actor_standby_is_enabled,
191
+ desired_requests_per_actor_run=actor_standby_desired_requests_per_actor_run,
192
+ max_requests_per_actor_run=actor_standby_max_requests_per_actor_run,
193
+ idle_timeout_secs=to_seconds(actor_standby_idle_timeout, as_int=True),
194
+ build=actor_standby_build,
195
+ memory_mbytes=actor_standby_memory_mbytes,
196
+ ),
197
+ example_run_input=ExampleRunInput(
198
+ body=example_run_input_body,
199
+ content_type=example_run_input_content_type,
200
+ ),
201
+ tagged_builds=tagged_builds,
202
+ )
203
+ result = self._update(timeout=timeout, **actor_fields.model_dump(by_alias=True, exclude_none=True))
204
+ return ActorResponse.model_validate(result).data
205
+
206
+ def delete(self, *, timeout: Timeout = 'short') -> None:
207
+ """Delete the Actor.
208
+
209
+
210
+ Args:
211
+ timeout: Timeout for the API HTTP request.
212
+ """
213
+ self._delete(timeout=timeout)
214
+
215
+ def start(
216
+ self,
217
+ *,
218
+ run_input: Any = None,
219
+ content_type: str | None = None,
220
+ build: str | None = None,
221
+ max_items: int | None = None,
222
+ max_total_charge_usd: Decimal | None = None,
223
+ restart_on_error: bool | None = None,
224
+ memory_mbytes: int | None = None,
225
+ run_timeout: timedelta | None = None,
226
+ force_permission_level: ActorPermissionLevel | None = None,
227
+ wait_for_finish: int | None = None,
228
+ webhooks: WebhooksList | None = None,
229
+ timeout: Timeout = 'medium',
230
+ ) -> Run:
231
+ """Start the Actor and immediately return the Run object.
232
+
233
+
234
+ Args:
235
+ run_input: The input to pass to the Actor run.
236
+ content_type: The content type of the input.
237
+ build: Specifies the Actor build to run. It can be either a build tag or build number. By default,
238
+ the run uses the build specified in the default run configuration for the Actor (typically latest).
239
+ max_items: Maximum number of results that will be returned by this run. If the Actor is charged
240
+ per result, you will not be charged for more results than the given limit.
241
+ max_total_charge_usd: A limit on the total charged amount for pay-per-event Actors.
242
+ restart_on_error: If true, the Actor run process will be restarted whenever it exits with
243
+ a non-zero status code.
244
+ memory_mbytes: Memory limit for the run, in megabytes. By default, the run uses a memory limit
245
+ specified in the default run configuration for the Actor.
246
+ run_timeout: Optional timeout for the run. By default, the run uses timeout specified
247
+ in the default run configuration for the Actor.
248
+ force_permission_level: Override the Actor's permissions for this run. If not set, the Actor will run
249
+ with permissions configured in the Actor settings.
250
+ wait_for_finish: The maximum number of seconds the server waits for the run to finish. By default,
251
+ it is 0, the maximum value is 60.
252
+ webhooks: Optional ad-hoc webhooks associated with
253
+ the Actor run which can be used to receive a notification, e.g. when the Actor finished or failed.
254
+ If you already have a webhook set up for the Actor or task, you do not have to add it again here.
255
+ Each webhook is represented by a dictionary containing these items:
256
+ * `event_types`: List of `WebhookEventType` values which trigger the webhook.
257
+ * `request_url`: URL to which to send the webhook HTTP request.
258
+ * `payload_template`: Optional template for the request payload.
259
+ timeout: Timeout for the API HTTP request.
260
+
261
+ Returns:
262
+ The run object.
263
+ """
264
+ run_input, content_type = encode_key_value_store_record_value(run_input, content_type=content_type)
265
+
266
+ request_params = self._build_params(
267
+ build=build,
268
+ maxItems=max_items,
269
+ maxTotalChargeUsd=max_total_charge_usd,
270
+ restartOnError=restart_on_error,
271
+ memory=memory_mbytes,
272
+ timeout=to_seconds(run_timeout, as_int=True),
273
+ waitForFinish=wait_for_finish,
274
+ forcePermissionLevel=force_permission_level,
275
+ webhooks=encode_webhooks_to_base64(webhooks),
276
+ )
277
+
278
+ response = self._http_client.call(
279
+ url=self._build_url('runs'),
280
+ method='POST',
281
+ headers={'content-type': content_type},
282
+ data=run_input,
283
+ params=request_params,
284
+ timeout=timeout,
285
+ )
286
+
287
+ result = response_to_dict(response)
288
+ return RunResponse.model_validate(result).data
289
+
290
+ def call(
291
+ self,
292
+ *,
293
+ run_input: Any = None,
294
+ content_type: str | None = None,
295
+ build: str | None = None,
296
+ max_items: int | None = None,
297
+ max_total_charge_usd: Decimal | None = None,
298
+ restart_on_error: bool | None = None,
299
+ memory_mbytes: int | None = None,
300
+ run_timeout: timedelta | None = None,
301
+ webhooks: WebhooksList | None = None,
302
+ force_permission_level: ActorPermissionLevel | None = None,
303
+ wait_duration: timedelta | None = None,
304
+ logger: Logger | Literal['default'] | None = 'default',
305
+ timeout: Timeout = 'no_timeout',
306
+ ) -> Run | None:
307
+ """Start the Actor and wait for it to finish before returning the Run object.
308
+
309
+ It waits indefinitely, unless the wait_duration argument is provided.
310
+
311
+
312
+ Args:
313
+ run_input: The input to pass to the Actor run.
314
+ content_type: The content type of the input.
315
+ build: Specifies the Actor build to run. It can be either a build tag or build number. By default,
316
+ the run uses the build specified in the default run configuration for the Actor (typically latest).
317
+ max_items: Maximum number of results that will be returned by this run. If the Actor is charged
318
+ per result, you will not be charged for more results than the given limit.
319
+ max_total_charge_usd: A limit on the total charged amount for pay-per-event Actors.
320
+ restart_on_error: If true, the Actor run process will be restarted whenever it exits with
321
+ a non-zero status code.
322
+ memory_mbytes: Memory limit for the run, in megabytes. By default, the run uses a memory limit
323
+ specified in the default run configuration for the Actor.
324
+ run_timeout: Optional timeout for the run. By default, the run uses timeout specified
325
+ in the default run configuration for the Actor.
326
+ force_permission_level: Override the Actor's permissions for this run. If not set, the Actor will run
327
+ with permissions configured in the Actor settings.
328
+ webhooks: Optional webhooks associated with the Actor run, which can
329
+ be used to receive a notification, e.g. when the Actor finished or failed. If you already have
330
+ a webhook set up for the Actor, you do not have to add it again here.
331
+ wait_duration: The maximum time the server waits for the run to finish. If not provided,
332
+ waits indefinitely.
333
+ logger: Logger used to redirect logs from the Actor run. Using "default" literal means that a predefined
334
+ default logger will be used. Setting `None` will disable any log propagation. Passing custom logger
335
+ will redirect logs to the provided logger. The logger is also used to capture status and status message
336
+ of the other Actor run.
337
+ timeout: Timeout for the API HTTP request.
338
+
339
+ Returns:
340
+ The run object.
341
+ """
342
+ started_run = self.start(
343
+ run_input=run_input,
344
+ content_type=content_type,
345
+ build=build,
346
+ max_items=max_items,
347
+ max_total_charge_usd=max_total_charge_usd,
348
+ restart_on_error=restart_on_error,
349
+ memory_mbytes=memory_mbytes,
350
+ run_timeout=run_timeout,
351
+ webhooks=webhooks,
352
+ force_permission_level=force_permission_level,
353
+ timeout=timeout,
354
+ )
355
+ run_client = self._client_registry.run_client(
356
+ resource_id=started_run.id,
357
+ base_url=self._base_url,
358
+ public_base_url=self._public_base_url,
359
+ http_client=self._http_client,
360
+ client_registry=self._client_registry,
361
+ )
362
+
363
+ if not logger:
364
+ return run_client.wait_for_finish(wait_duration=wait_duration)
365
+
366
+ if logger == 'default':
367
+ logger = None
368
+
369
+ with run_client.get_status_message_watcher(to_logger=logger), run_client.get_streamed_log(to_logger=logger):
370
+ return run_client.wait_for_finish(wait_duration=wait_duration)
371
+
372
+ def build(
373
+ self,
374
+ *,
375
+ version_number: str,
376
+ beta_packages: bool | None = None,
377
+ tag: str | None = None,
378
+ use_cache: bool | None = None,
379
+ wait_for_finish: int | None = None,
380
+ timeout: Timeout = 'medium',
381
+ ) -> Build:
382
+ """Build the Actor.
383
+
384
+
385
+ Args:
386
+ version_number: Actor version number to be built.
387
+ beta_packages: If True, then the Actor is built with beta versions of the platform SDK NPM packages. By default,
388
+ the build uses latest stable packages.
389
+ tag: Tag to be applied to the build on success. By default, the tag is taken from the Actor version's
390
+ build tag property.
391
+ use_cache: If true, the Actor's Docker container will be rebuilt using layer cache
392
+ (https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#leverage-build-cache).
393
+ This is to enable quick rebuild during development. By default, the cache is not used.
394
+ wait_for_finish: The maximum number of seconds the server waits for the build to finish before returning.
395
+ By default it is 0, the maximum value is 60.
396
+ timeout: Timeout for the API HTTP request.
397
+
398
+ Returns:
399
+ The build object.
400
+ """
401
+ request_params = self._build_params(
402
+ version=version_number,
403
+ betaPackages=beta_packages,
404
+ tag=tag,
405
+ useCache=use_cache,
406
+ waitForFinish=wait_for_finish,
407
+ )
408
+
409
+ response = self._http_client.call(
410
+ url=self._build_url('builds'),
411
+ method='POST',
412
+ params=request_params,
413
+ timeout=timeout,
414
+ )
415
+
416
+ result = response_to_dict(response)
417
+ return BuildResponse.model_validate(result).data
418
+
419
+ def builds(self) -> BuildCollectionClient:
420
+ """Retrieve a client for the builds of this Actor."""
421
+ return self._client_registry.build_collection_client(
422
+ resource_path='builds',
423
+ **self._base_client_kwargs,
424
+ )
425
+
426
+ def runs(self) -> RunCollectionClient:
427
+ """Retrieve a client for the runs of this Actor."""
428
+ return self._client_registry.run_collection_client(
429
+ resource_path='runs',
430
+ **self._base_client_kwargs,
431
+ )
432
+
433
+ def default_build(
434
+ self,
435
+ *,
436
+ wait_for_finish: int | None = None,
437
+ timeout: Timeout = 'short',
438
+ ) -> BuildClient:
439
+ """Retrieve Actor's default build.
440
+
441
+
442
+ Args:
443
+ wait_for_finish: The maximum number of seconds the server waits for the build to finish before returning.
444
+ By default it is 0, the maximum value is 60.
445
+ timeout: Timeout for the API HTTP request.
446
+
447
+ Returns:
448
+ The resource client for the default build of this Actor.
449
+ """
450
+ request_params = self._build_params(
451
+ waitForFinish=wait_for_finish,
452
+ )
453
+
454
+ response = self._http_client.call(
455
+ url=self._build_url('builds/default'),
456
+ method='GET',
457
+ params=request_params,
458
+ timeout=timeout,
459
+ )
460
+ result = response_to_dict(response)
461
+
462
+ return self._client_registry.build_client(
463
+ resource_id=result['data']['id'],
464
+ base_url=self._base_url,
465
+ public_base_url=self._public_base_url,
466
+ http_client=self._http_client,
467
+ client_registry=self._client_registry,
468
+ )
469
+
470
+ def last_run(
471
+ self,
472
+ *,
473
+ status: ActorJobStatus | None = None,
474
+ origin: RunOrigin | None = None,
475
+ ) -> RunClient:
476
+ """Retrieve the client for the last run of this Actor.
477
+
478
+ Last run is retrieved based on the start time of the runs.
479
+
480
+ Args:
481
+ status: Consider only runs with this status.
482
+ origin: Consider only runs started with this origin.
483
+
484
+ Returns:
485
+ The resource client for the last run of this Actor.
486
+ """
487
+ return self._client_registry.run_client(
488
+ resource_id='last',
489
+ resource_path='runs',
490
+ params=self._build_params(
491
+ status=status,
492
+ origin=origin,
493
+ ),
494
+ **self._base_client_kwargs,
495
+ )
496
+
497
+ def versions(self) -> ActorVersionCollectionClient:
498
+ """Retrieve a client for the versions of this Actor."""
499
+ return self._client_registry.actor_version_collection_client(**self._base_client_kwargs)
500
+
501
+ def version(self, version_number: str) -> ActorVersionClient:
502
+ """Retrieve the client for the specified version of this Actor.
503
+
504
+ Args:
505
+ version_number: The version number for which to retrieve the resource client.
506
+
507
+ Returns:
508
+ The resource client for the specified Actor version.
509
+ """
510
+ return self._client_registry.actor_version_client(
511
+ resource_id=version_number,
512
+ **self._base_client_kwargs,
513
+ )
514
+
515
+ def webhooks(self) -> WebhookCollectionClient:
516
+ """Retrieve a client for webhooks associated with this Actor."""
517
+ return self._client_registry.webhook_collection_client(**self._base_client_kwargs)
518
+
519
+ def validate_input(
520
+ self,
521
+ run_input: Any = None,
522
+ *,
523
+ build_tag: str | None = None,
524
+ content_type: str | None = None,
525
+ timeout: Timeout = 'short',
526
+ ) -> bool:
527
+ """Validate an input for the Actor that defines an input schema.
528
+
529
+ Args:
530
+ run_input: The input to validate.
531
+ build_tag: The Actor's build tag.
532
+ content_type: The content type of the input.
533
+ timeout: Timeout for the API HTTP request.
534
+
535
+ Returns:
536
+ True if the input is valid, else raise an exception with validation error details.
537
+ """
538
+ run_input, content_type = encode_key_value_store_record_value(run_input, content_type=content_type)
539
+
540
+ self._http_client.call(
541
+ url=self._build_url('validate-input'),
542
+ method='POST',
543
+ headers={'content-type': content_type},
544
+ data=run_input,
545
+ params=self._build_params(build=build_tag),
546
+ timeout=timeout,
547
+ )
548
+
549
+ return True
550
+
551
+
552
+ @docs_group('Resource clients')
553
+ class ActorClientAsync(ResourceClientAsync):
554
+ """Sub-client for managing a specific Actor.
555
+
556
+ Provides methods to manage a specific Actor, e.g. update it, delete it, build it, or start runs. Obtain an instance
557
+ via an appropriate method on the `ApifyClientAsync` class.
558
+ """
559
+
560
+ def __init__(
561
+ self,
562
+ *,
563
+ resource_id: str,
564
+ resource_path: str = 'actors',
565
+ **kwargs: Any,
566
+ ) -> None:
567
+ super().__init__(
568
+ resource_id=resource_id,
569
+ resource_path=resource_path,
570
+ **kwargs,
571
+ )
572
+
573
+ async def get(self, *, timeout: Timeout = 'short') -> Actor | None:
574
+ """Retrieve the Actor.
575
+
576
+
577
+ Args:
578
+ timeout: Timeout for the API HTTP request.
579
+
580
+ Returns:
581
+ The retrieved Actor.
582
+ """
583
+ result = await self._get(timeout=timeout)
584
+ if result is None:
585
+ return None
586
+ return ActorResponse.model_validate(result).data
587
+
588
+ async def update(
589
+ self,
590
+ *,
591
+ name: str | None = None,
592
+ title: str | None = None,
593
+ description: str | None = None,
594
+ seo_title: str | None = None,
595
+ seo_description: str | None = None,
596
+ versions: list[dict[str, Any]] | None = None,
597
+ restart_on_error: bool | None = None,
598
+ is_public: bool | None = None,
599
+ is_deprecated: bool | None = None,
600
+ categories: list[str] | None = None,
601
+ default_run_build: str | None = None,
602
+ default_run_max_items: int | None = None,
603
+ default_run_memory_mbytes: int | None = None,
604
+ default_run_timeout: timedelta | None = None,
605
+ example_run_input_body: Any = None,
606
+ example_run_input_content_type: str | None = None,
607
+ actor_standby_is_enabled: bool | None = None,
608
+ actor_standby_desired_requests_per_actor_run: int | None = None,
609
+ actor_standby_max_requests_per_actor_run: int | None = None,
610
+ actor_standby_idle_timeout: timedelta | None = None,
611
+ actor_standby_build: str | None = None,
612
+ actor_standby_memory_mbytes: int | None = None,
613
+ pricing_infos: list[dict[str, Any]] | None = None,
614
+ actor_permission_level: ActorPermissionLevel | None = None,
615
+ tagged_builds: dict[str, dict[str, str] | None] | None = None,
616
+ timeout: Timeout = 'short',
617
+ ) -> Actor:
618
+ """Update the Actor with the specified fields.
619
+
620
+
621
+ Args:
622
+ name: The name of the Actor.
623
+ title: The title of the Actor (human-readable).
624
+ description: The description for the Actor.
625
+ seo_title: The title of the Actor optimized for search engines.
626
+ seo_description: The description of the Actor optimized for search engines.
627
+ versions: The list of Actor versions.
628
+ restart_on_error: If true, the Actor run process will be restarted whenever it exits with
629
+ a non-zero status code.
630
+ is_public: Whether the Actor is public.
631
+ is_deprecated: Whether the Actor is deprecated.
632
+ categories: The categories to which the Actor belongs to.
633
+ default_run_build: Tag or number of the build that you want to run by default.
634
+ default_run_max_items: Default limit of the number of results that will be returned
635
+ by runs of this Actor, if the Actor is charged per result.
636
+ default_run_memory_mbytes: Default amount of memory allocated for the runs of this Actor, in megabytes.
637
+ default_run_timeout: Default timeout for the runs of this Actor.
638
+ example_run_input_body: Input to be prefilled as default input to new users of this Actor.
639
+ example_run_input_content_type: The content type of the example run input.
640
+ actor_standby_is_enabled: Whether the Actor Standby is enabled.
641
+ actor_standby_desired_requests_per_actor_run: The desired number of concurrent HTTP requests for
642
+ a single Actor Standby run.
643
+ actor_standby_max_requests_per_actor_run: The maximum number of concurrent HTTP requests for
644
+ a single Actor Standby run.
645
+ actor_standby_idle_timeout: If the Actor run does not receive any requests for this time,
646
+ it will be shut down.
647
+ actor_standby_build: The build tag or number to run when the Actor is in Standby mode.
648
+ actor_standby_memory_mbytes: The memory in megabytes to use when the Actor is in Standby mode.
649
+ pricing_infos: A list of objects that describes the pricing of the Actor.
650
+ actor_permission_level: The permission level of the Actor on Scrapely platform.
651
+ tagged_builds: A dictionary mapping build tag names to their settings. Use it to create, update,
652
+ or remove build tags. To assign a tag, provide a dict with 'buildId' key. To remove a tag,
653
+ set its value to None. Example: {'latest': {'buildId': 'abc'}, 'beta': None}.
654
+ timeout: Timeout for the API HTTP request.
655
+
656
+ Returns:
657
+ The updated Actor.
658
+ """
659
+ actor_fields = UpdateActorRequest(
660
+ name=name,
661
+ title=title,
662
+ description=description,
663
+ seo_title=seo_title,
664
+ seo_description=seo_description,
665
+ versions=[CreateOrUpdateVersionRequest.model_validate(v) for v in versions] if versions else None,
666
+ is_public=is_public,
667
+ is_deprecated=is_deprecated,
668
+ categories=categories,
669
+ pricing_infos=_pricing_info_list_adapter.validate_python(pricing_infos) if pricing_infos else None,
670
+ actor_permission_level=actor_permission_level,
671
+ default_run_options=DefaultRunOptions(
672
+ build=default_run_build,
673
+ max_items=default_run_max_items,
674
+ memory_mbytes=default_run_memory_mbytes,
675
+ timeout_secs=to_seconds(default_run_timeout, as_int=True),
676
+ restart_on_error=restart_on_error,
677
+ ),
678
+ actor_standby=ActorStandby(
679
+ is_enabled=actor_standby_is_enabled,
680
+ desired_requests_per_actor_run=actor_standby_desired_requests_per_actor_run,
681
+ max_requests_per_actor_run=actor_standby_max_requests_per_actor_run,
682
+ idle_timeout_secs=to_seconds(actor_standby_idle_timeout, as_int=True),
683
+ build=actor_standby_build,
684
+ memory_mbytes=actor_standby_memory_mbytes,
685
+ ),
686
+ example_run_input=ExampleRunInput(
687
+ body=example_run_input_body,
688
+ content_type=example_run_input_content_type,
689
+ ),
690
+ tagged_builds=tagged_builds,
691
+ )
692
+ result = await self._update(timeout=timeout, **actor_fields.model_dump(by_alias=True, exclude_none=True))
693
+ return ActorResponse.model_validate(result).data
694
+
695
+ async def delete(self, *, timeout: Timeout = 'short') -> None:
696
+ """Delete the Actor.
697
+
698
+
699
+ Args:
700
+ timeout: Timeout for the API HTTP request.
701
+ """
702
+ await self._delete(timeout=timeout)
703
+
704
+ async def start(
705
+ self,
706
+ *,
707
+ run_input: Any = None,
708
+ content_type: str | None = None,
709
+ build: str | None = None,
710
+ max_items: int | None = None,
711
+ max_total_charge_usd: Decimal | None = None,
712
+ restart_on_error: bool | None = None,
713
+ memory_mbytes: int | None = None,
714
+ run_timeout: timedelta | None = None,
715
+ force_permission_level: ActorPermissionLevel | None = None,
716
+ wait_for_finish: int | None = None,
717
+ webhooks: WebhooksList | None = None,
718
+ timeout: Timeout = 'medium',
719
+ ) -> Run:
720
+ """Start the Actor and immediately return the Run object.
721
+
722
+
723
+ Args:
724
+ run_input: The input to pass to the Actor run.
725
+ content_type: The content type of the input.
726
+ build: Specifies the Actor build to run. It can be either a build tag or build number. By default,
727
+ the run uses the build specified in the default run configuration for the Actor (typically latest).
728
+ max_items: Maximum number of results that will be returned by this run. If the Actor is charged
729
+ per result, you will not be charged for more results than the given limit.
730
+ max_total_charge_usd: A limit on the total charged amount for pay-per-event Actors.
731
+ restart_on_error: If true, the Actor run process will be restarted whenever it exits with
732
+ a non-zero status code.
733
+ memory_mbytes: Memory limit for the run, in megabytes. By default, the run uses a memory limit
734
+ specified in the default run configuration for the Actor.
735
+ run_timeout: Optional timeout for the run. By default, the run uses timeout specified
736
+ in the default run configuration for the Actor.
737
+ force_permission_level: Override the Actor's permissions for this run. If not set, the Actor will run
738
+ with permissions configured in the Actor settings.
739
+ wait_for_finish: The maximum number of seconds the server waits for the run to finish. By default,
740
+ it is 0, the maximum value is 60.
741
+ webhooks: Optional ad-hoc webhooks associated with
742
+ the Actor run which can be used to receive a notification, e.g. when the Actor finished or failed.
743
+ If you already have a webhook set up for the Actor or task, you do not have to add it again here.
744
+ Each webhook is represented by a dictionary containing these items:
745
+ * `event_types`: List of `WebhookEventType` values which trigger the webhook.
746
+ * `request_url`: URL to which to send the webhook HTTP request.
747
+ * `payload_template`: Optional template for the request payload.
748
+ timeout: Timeout for the API HTTP request.
749
+
750
+ Returns:
751
+ The run object.
752
+ """
753
+ run_input, content_type = encode_key_value_store_record_value(run_input, content_type=content_type)
754
+
755
+ request_params = self._build_params(
756
+ build=build,
757
+ maxItems=max_items,
758
+ maxTotalChargeUsd=max_total_charge_usd,
759
+ restartOnError=restart_on_error,
760
+ memory=memory_mbytes,
761
+ timeout=to_seconds(run_timeout, as_int=True),
762
+ waitForFinish=wait_for_finish,
763
+ forcePermissionLevel=force_permission_level,
764
+ webhooks=encode_webhooks_to_base64(webhooks),
765
+ )
766
+
767
+ response = await self._http_client.call(
768
+ url=self._build_url('runs'),
769
+ method='POST',
770
+ headers={'content-type': content_type},
771
+ data=run_input,
772
+ params=request_params,
773
+ timeout=timeout,
774
+ )
775
+
776
+ result = response_to_dict(response)
777
+ return RunResponse.model_validate(result).data
778
+
779
+ async def call(
780
+ self,
781
+ *,
782
+ run_input: Any = None,
783
+ content_type: str | None = None,
784
+ build: str | None = None,
785
+ max_items: int | None = None,
786
+ max_total_charge_usd: Decimal | None = None,
787
+ restart_on_error: bool | None = None,
788
+ memory_mbytes: int | None = None,
789
+ run_timeout: timedelta | None = None,
790
+ webhooks: WebhooksList | None = None,
791
+ force_permission_level: ActorPermissionLevel | None = None,
792
+ wait_duration: timedelta | None = None,
793
+ logger: Logger | Literal['default'] | None = 'default',
794
+ timeout: Timeout = 'no_timeout',
795
+ ) -> Run | None:
796
+ """Start the Actor and wait for it to finish before returning the Run object.
797
+
798
+ It waits indefinitely, unless the wait_duration argument is provided.
799
+
800
+
801
+ Args:
802
+ run_input: The input to pass to the Actor run.
803
+ content_type: The content type of the input.
804
+ build: Specifies the Actor build to run. It can be either a build tag or build number. By default,
805
+ the run uses the build specified in the default run configuration for the Actor (typically latest).
806
+ max_items: Maximum number of results that will be returned by this run. If the Actor is charged
807
+ per result, you will not be charged for more results than the given limit.
808
+ max_total_charge_usd: A limit on the total charged amount for pay-per-event Actors.
809
+ restart_on_error: If true, the Actor run process will be restarted whenever it exits with
810
+ a non-zero status code.
811
+ memory_mbytes: Memory limit for the run, in megabytes. By default, the run uses a memory limit
812
+ specified in the default run configuration for the Actor.
813
+ run_timeout: Optional timeout for the run. By default, the run uses timeout specified
814
+ in the default run configuration for the Actor.
815
+ force_permission_level: Override the Actor's permissions for this run. If not set, the Actor will run
816
+ with permissions configured in the Actor settings.
817
+ webhooks: Optional webhooks associated with the Actor run, which can
818
+ be used to receive a notification, e.g. when the Actor finished or failed. If you already have
819
+ a webhook set up for the Actor, you do not have to add it again here.
820
+ wait_duration: The maximum time the server waits for the run to finish. If not provided,
821
+ waits indefinitely.
822
+ logger: Logger used to redirect logs from the Actor run. Using "default" literal means that a predefined
823
+ default logger will be used. Setting `None` will disable any log propagation. Passing custom logger
824
+ will redirect logs to the provided logger. The logger is also used to capture status and status message
825
+ of the other Actor run.
826
+ timeout: Timeout for the API HTTP request.
827
+
828
+ Returns:
829
+ The run object.
830
+ """
831
+ started_run = await self.start(
832
+ run_input=run_input,
833
+ content_type=content_type,
834
+ build=build,
835
+ max_items=max_items,
836
+ max_total_charge_usd=max_total_charge_usd,
837
+ restart_on_error=restart_on_error,
838
+ memory_mbytes=memory_mbytes,
839
+ run_timeout=run_timeout,
840
+ webhooks=webhooks,
841
+ force_permission_level=force_permission_level,
842
+ timeout=timeout,
843
+ )
844
+
845
+ run_client = self._client_registry.run_client(
846
+ resource_id=started_run.id,
847
+ base_url=self._base_url,
848
+ public_base_url=self._public_base_url,
849
+ http_client=self._http_client,
850
+ client_registry=self._client_registry,
851
+ )
852
+
853
+ if not logger:
854
+ return await run_client.wait_for_finish(wait_duration=wait_duration)
855
+
856
+ if logger == 'default':
857
+ logger = None
858
+
859
+ status_redirector = await run_client.get_status_message_watcher(to_logger=logger)
860
+ streamed_log = await run_client.get_streamed_log(to_logger=logger)
861
+
862
+ async with status_redirector, streamed_log:
863
+ return await run_client.wait_for_finish(wait_duration=wait_duration)
864
+
865
+ async def build(
866
+ self,
867
+ *,
868
+ version_number: str,
869
+ beta_packages: bool | None = None,
870
+ tag: str | None = None,
871
+ use_cache: bool | None = None,
872
+ wait_for_finish: int | None = None,
873
+ timeout: Timeout = 'medium',
874
+ ) -> Build:
875
+ """Build the Actor.
876
+
877
+
878
+ Args:
879
+ version_number: Actor version number to be built.
880
+ beta_packages: If True, then the Actor is built with beta versions of the platform SDK NPM packages. By default,
881
+ the build uses latest stable packages.
882
+ tag: Tag to be applied to the build on success. By default, the tag is taken from the Actor version's
883
+ build tag property.
884
+ use_cache: If true, the Actor's Docker container will be rebuilt using layer cache
885
+ (https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#leverage-build-cache).
886
+ This is to enable quick rebuild during development. By default, the cache is not used.
887
+ wait_for_finish: The maximum number of seconds the server waits for the build to finish before returning.
888
+ By default it is 0, the maximum value is 60.
889
+ timeout: Timeout for the API HTTP request.
890
+
891
+ Returns:
892
+ The build object.
893
+ """
894
+ request_params = self._build_params(
895
+ version=version_number,
896
+ betaPackages=beta_packages,
897
+ tag=tag,
898
+ useCache=use_cache,
899
+ waitForFinish=wait_for_finish,
900
+ )
901
+
902
+ response = await self._http_client.call(
903
+ url=self._build_url('builds'),
904
+ method='POST',
905
+ params=request_params,
906
+ timeout=timeout,
907
+ )
908
+
909
+ result = response_to_dict(response)
910
+ return BuildResponse.model_validate(result).data
911
+
912
+ def builds(self) -> BuildCollectionClientAsync:
913
+ """Retrieve a client for the builds of this Actor."""
914
+ return self._client_registry.build_collection_client(
915
+ resource_path='builds',
916
+ **self._base_client_kwargs,
917
+ )
918
+
919
+ def runs(self) -> RunCollectionClientAsync:
920
+ """Retrieve a client for the runs of this Actor."""
921
+ return self._client_registry.run_collection_client(
922
+ resource_path='runs',
923
+ **self._base_client_kwargs,
924
+ )
925
+
926
+ async def default_build(
927
+ self,
928
+ *,
929
+ wait_for_finish: int | None = None,
930
+ timeout: Timeout = 'short',
931
+ ) -> BuildClientAsync:
932
+ """Retrieve Actor's default build.
933
+
934
+
935
+ Args:
936
+ wait_for_finish: The maximum number of seconds the server waits for the build to finish before returning.
937
+ By default it is 0, the maximum value is 60.
938
+ timeout: Timeout for the API HTTP request.
939
+
940
+ Returns:
941
+ The resource client for the default build of this Actor.
942
+ """
943
+ request_params = self._build_params(
944
+ waitForFinish=wait_for_finish,
945
+ )
946
+
947
+ response = await self._http_client.call(
948
+ url=self._build_url('builds/default'),
949
+ method='GET',
950
+ params=request_params,
951
+ timeout=timeout,
952
+ )
953
+ result = response_to_dict(response)
954
+
955
+ return self._client_registry.build_client(
956
+ resource_id=result['data']['id'],
957
+ base_url=self._base_url,
958
+ public_base_url=self._public_base_url,
959
+ http_client=self._http_client,
960
+ client_registry=self._client_registry,
961
+ )
962
+
963
+ def last_run(
964
+ self,
965
+ *,
966
+ status: ActorJobStatus | None = None,
967
+ origin: RunOrigin | None = None,
968
+ ) -> RunClientAsync:
969
+ """Retrieve the client for the last run of this Actor.
970
+
971
+ Last run is retrieved based on the start time of the runs.
972
+
973
+ Args:
974
+ status: Consider only runs with this status.
975
+ origin: Consider only runs started with this origin.
976
+
977
+ Returns:
978
+ The resource client for the last run of this Actor.
979
+ """
980
+ return self._client_registry.run_client(
981
+ resource_id='last',
982
+ resource_path='runs',
983
+ params=self._build_params(
984
+ status=status,
985
+ origin=origin,
986
+ ),
987
+ **self._base_client_kwargs,
988
+ )
989
+
990
+ def versions(self) -> ActorVersionCollectionClientAsync:
991
+ """Retrieve a client for the versions of this Actor."""
992
+ return self._client_registry.actor_version_collection_client(**self._base_client_kwargs)
993
+
994
+ def version(self, version_number: str) -> ActorVersionClientAsync:
995
+ """Retrieve the client for the specified version of this Actor.
996
+
997
+ Args:
998
+ version_number: The version number for which to retrieve the resource client.
999
+
1000
+ Returns:
1001
+ The resource client for the specified Actor version.
1002
+ """
1003
+ return self._client_registry.actor_version_client(
1004
+ resource_id=version_number,
1005
+ **self._base_client_kwargs,
1006
+ )
1007
+
1008
+ def webhooks(self) -> WebhookCollectionClientAsync:
1009
+ """Retrieve a client for webhooks associated with this Actor."""
1010
+ return self._client_registry.webhook_collection_client(**self._base_client_kwargs)
1011
+
1012
+ async def validate_input(
1013
+ self,
1014
+ run_input: Any = None,
1015
+ *,
1016
+ build_tag: str | None = None,
1017
+ content_type: str | None = None,
1018
+ timeout: Timeout = 'short',
1019
+ ) -> bool:
1020
+ """Validate an input for the Actor that defines an input schema.
1021
+
1022
+ Args:
1023
+ run_input: The input to validate.
1024
+ build_tag: The Actor's build tag.
1025
+ content_type: The content type of the input.
1026
+ timeout: Timeout for the API HTTP request.
1027
+
1028
+ Returns:
1029
+ True if the input is valid, else raise an exception with validation error details.
1030
+ """
1031
+ run_input, content_type = encode_key_value_store_record_value(run_input, content_type=content_type)
1032
+
1033
+ await self._http_client.call(
1034
+ url=self._build_url('validate-input'),
1035
+ method='POST',
1036
+ headers={'content-type': content_type},
1037
+ data=run_input,
1038
+ params=self._build_params(build=build_tag),
1039
+ timeout=timeout,
1040
+ )
1041
+
1042
+ return True