-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathactor_collection.py
More file actions
312 lines (284 loc) · 13.7 KB
/
actor_collection.py
File metadata and controls
312 lines (284 loc) · 13.7 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
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Literal
from apify_client._docs import docs_group
from apify_client._models import (
Actor,
ActorResponse,
ActorStandby,
CreateActorRequest,
DefaultRunOptions,
ExampleRunInput,
ListOfActors,
ListOfActorsResponse,
)
from apify_client._resource_clients._resource_client import ResourceClient, ResourceClientAsync
from apify_client._utils import to_seconds
if TYPE_CHECKING:
from datetime import timedelta
from apify_client._types import Timeout
@docs_group('Resource clients')
class ActorCollectionClient(ResourceClient):
"""Sub-client for the Actor collection.
Provides methods to manage the Actor collection, e.g. list or create Actors. Obtain an instance via an appropriate
method on the `ApifyClient` class.
"""
def __init__(
self,
*,
resource_path: str = 'acts',
**kwargs: Any,
) -> None:
super().__init__(
resource_path=resource_path,
**kwargs,
)
def list(
self,
*,
my: bool | None = None,
limit: int | None = None,
offset: int | None = None,
desc: bool | None = None,
sort_by: Literal['createdAt', 'stats.lastRunStartedAt'] | None = 'createdAt',
timeout: Timeout = 'medium',
) -> ListOfActors:
"""List the Actors the user has created or used.
https://docs.apify.com/api/v2#/reference/actors/actor-collection/get-list-of-actors
Args:
my: If True, will return only Actors which the user has created themselves.
limit: How many Actors to list.
offset: What Actor to include as first when retrieving the list.
desc: Whether to sort the Actors in descending order based on their creation date.
sort_by: Field to sort the results by.
timeout: Timeout for the API HTTP request.
Returns:
The list of available Actors matching the specified filters.
"""
result = self._list(timeout=timeout, my=my, limit=limit, offset=offset, desc=desc, sortBy=sort_by)
return ListOfActorsResponse.model_validate(result).data
def create(
self,
*,
name: str,
title: str | None = None,
description: str | None = None,
seo_title: str | None = None,
seo_description: str | None = None,
versions: list[dict[str, Any]] | None = None, # ty: ignore[invalid-type-form]
restart_on_error: bool | None = None,
is_public: bool | None = None,
is_deprecated: bool | None = None,
categories: list[str] | None = None, # ty: ignore[invalid-type-form]
default_run_build: str | None = None,
default_run_max_items: int | None = None,
default_run_memory_mbytes: int | None = None,
default_run_timeout: timedelta | None = None,
example_run_input_body: Any = None,
example_run_input_content_type: str | None = None,
actor_standby_is_enabled: bool | None = None,
actor_standby_desired_requests_per_actor_run: int | None = None,
actor_standby_max_requests_per_actor_run: int | None = None,
actor_standby_idle_timeout: timedelta | None = None,
actor_standby_build: str | None = None,
actor_standby_memory_mbytes: int | None = None,
timeout: Timeout = 'medium',
) -> Actor:
"""Create a new Actor.
https://docs.apify.com/api/v2#/reference/actors/actor-collection/create-actor
Args:
name: The name of the Actor.
title: The title of the Actor (human-readable).
description: The description for the Actor.
seo_title: The title of the Actor optimized for search engines.
seo_description: The description of the Actor optimized for search engines.
versions: The list of Actor versions.
restart_on_error: If true, the Actor run process will be restarted whenever it exits with
a non-zero status code.
is_public: Whether the Actor is public.
is_deprecated: Whether the Actor is deprecated.
categories: The categories to which the Actor belongs to.
default_run_build: Tag or number of the build that you want to run by default.
default_run_max_items: Default limit of the number of results that will be returned by runs
of this Actor, if the Actor is charged per result.
default_run_memory_mbytes: Default amount of memory allocated for the runs of this Actor, in megabytes.
default_run_timeout: Default timeout for the runs of this Actor.
example_run_input_body: Input to be prefilled as default input to new users of this Actor.
example_run_input_content_type: The content type of the example run input.
actor_standby_is_enabled: Whether the Actor Standby is enabled.
actor_standby_desired_requests_per_actor_run: The desired number of concurrent HTTP requests for
a single Actor Standby run.
actor_standby_max_requests_per_actor_run: The maximum number of concurrent HTTP requests for
a single Actor Standby run.
actor_standby_idle_timeout: If the Actor run does not receive any requests for this time,
it will be shut down.
actor_standby_build: The build tag or number to run when the Actor is in Standby mode.
actor_standby_memory_mbytes: The memory in megabytes to use when the Actor is in Standby mode.
timeout: Timeout for the API HTTP request.
Returns:
The created Actor.
"""
actor_fields = CreateActorRequest(
name=name,
title=title,
description=description,
seo_title=seo_title,
seo_description=seo_description,
versions=versions,
is_public=is_public,
is_deprecated=is_deprecated,
categories=categories,
default_run_options=DefaultRunOptions(
build=default_run_build,
max_items=default_run_max_items,
memory_mbytes=default_run_memory_mbytes,
timeout_secs=to_seconds(default_run_timeout, as_int=True),
restart_on_error=restart_on_error,
),
actor_standby=ActorStandby(
is_enabled=actor_standby_is_enabled,
desired_requests_per_actor_run=actor_standby_desired_requests_per_actor_run,
max_requests_per_actor_run=actor_standby_max_requests_per_actor_run,
idle_timeout_secs=to_seconds(actor_standby_idle_timeout, as_int=True),
build=actor_standby_build,
memory_mbytes=actor_standby_memory_mbytes,
),
example_run_input=ExampleRunInput(
body=example_run_input_body,
content_type=example_run_input_content_type,
),
)
result = self._create(timeout=timeout, **actor_fields.model_dump(by_alias=True, exclude_none=True))
return ActorResponse.model_validate(result).data
@docs_group('Resource clients')
class ActorCollectionClientAsync(ResourceClientAsync):
"""Sub-client for the Actor collection.
Provides methods to manage the Actor collection, e.g. list or create Actors. Obtain an instance via an appropriate
method on the `ApifyClientAsync` class.
"""
def __init__(
self,
*,
resource_path: str = 'acts',
**kwargs: Any,
) -> None:
super().__init__(
resource_path=resource_path,
**kwargs,
)
async def list(
self,
*,
my: bool | None = None,
limit: int | None = None,
offset: int | None = None,
desc: bool | None = None,
sort_by: Literal['createdAt', 'stats.lastRunStartedAt'] | None = 'createdAt',
timeout: Timeout = 'medium',
) -> ListOfActors:
"""List the Actors the user has created or used.
https://docs.apify.com/api/v2#/reference/actors/actor-collection/get-list-of-actors
Args:
my: If True, will return only Actors which the user has created themselves.
limit: How many Actors to list.
offset: What Actor to include as first when retrieving the list.
desc: Whether to sort the Actors in descending order based on their creation date.
sort_by: Field to sort the results by.
timeout: Timeout for the API HTTP request.
Returns:
The list of available Actors matching the specified filters.
"""
result = await self._list(timeout=timeout, my=my, limit=limit, offset=offset, desc=desc, sortBy=sort_by)
return ListOfActorsResponse.model_validate(result).data
async def create(
self,
*,
name: str,
title: str | None = None,
description: str | None = None,
seo_title: str | None = None,
seo_description: str | None = None,
versions: list[dict[str, Any]] | None = None, # ty: ignore[invalid-type-form]
restart_on_error: bool | None = None,
is_public: bool | None = None,
is_deprecated: bool | None = None,
categories: list[str] | None = None, # ty: ignore[invalid-type-form]
default_run_build: str | None = None,
default_run_max_items: int | None = None,
default_run_memory_mbytes: int | None = None,
default_run_timeout: timedelta | None = None,
example_run_input_body: Any = None,
example_run_input_content_type: str | None = None,
actor_standby_is_enabled: bool | None = None,
actor_standby_desired_requests_per_actor_run: int | None = None,
actor_standby_max_requests_per_actor_run: int | None = None,
actor_standby_idle_timeout: timedelta | None = None,
actor_standby_build: str | None = None,
actor_standby_memory_mbytes: int | None = None,
timeout: Timeout = 'medium',
) -> Actor:
"""Create a new Actor.
https://docs.apify.com/api/v2#/reference/actors/actor-collection/create-actor
Args:
name: The name of the Actor.
title: The title of the Actor (human-readable).
description: The description for the Actor.
seo_title: The title of the Actor optimized for search engines.
seo_description: The description of the Actor optimized for search engines.
versions: The list of Actor versions.
restart_on_error: If true, the Actor run process will be restarted whenever it exits with
a non-zero status code.
is_public: Whether the Actor is public.
is_deprecated: Whether the Actor is deprecated.
categories: The categories to which the Actor belongs to.
default_run_build: Tag or number of the build that you want to run by default.
default_run_max_items: Default limit of the number of results that will be returned by runs
of this Actor, if the Actor is charged per result.
default_run_memory_mbytes: Default amount of memory allocated for the runs of this Actor, in megabytes.
default_run_timeout: Default timeout for the runs of this Actor.
example_run_input_body: Input to be prefilled as default input to new users of this Actor.
example_run_input_content_type: The content type of the example run input.
actor_standby_is_enabled: Whether the Actor Standby is enabled.
actor_standby_desired_requests_per_actor_run: The desired number of concurrent HTTP requests for
a single Actor Standby run.
actor_standby_max_requests_per_actor_run: The maximum number of concurrent HTTP requests for
a single Actor Standby run.
actor_standby_idle_timeout: If the Actor run does not receive any requests for this time,
it will be shut down.
actor_standby_build: The build tag or number to run when the Actor is in Standby mode.
actor_standby_memory_mbytes: The memory in megabytes to use when the Actor is in Standby mode.
timeout: Timeout for the API HTTP request.
Returns:
The created Actor.
"""
actor_fields = CreateActorRequest(
name=name,
title=title,
description=description,
seo_title=seo_title,
seo_description=seo_description,
versions=versions,
is_public=is_public,
is_deprecated=is_deprecated,
categories=categories,
default_run_options=DefaultRunOptions(
build=default_run_build,
max_items=default_run_max_items,
memory_mbytes=default_run_memory_mbytes,
timeout_secs=to_seconds(default_run_timeout, as_int=True),
restart_on_error=restart_on_error,
),
actor_standby=ActorStandby(
is_enabled=actor_standby_is_enabled,
desired_requests_per_actor_run=actor_standby_desired_requests_per_actor_run,
max_requests_per_actor_run=actor_standby_max_requests_per_actor_run,
idle_timeout_secs=to_seconds(actor_standby_idle_timeout, as_int=True),
build=actor_standby_build,
memory_mbytes=actor_standby_memory_mbytes,
),
example_run_input=ExampleRunInput(
body=example_run_input_body,
content_type=example_run_input_content_type,
),
)
result = await self._create(timeout=timeout, **actor_fields.model_dump(by_alias=True, exclude_none=True))
return ActorResponse.model_validate(result).data