Skip to content

Commit 1d7ea34

Browse files
authored
Improve Apify actions (#16384)
* updates * updates * updates * updates
1 parent 3f4c67c commit 1d7ea34

File tree

10 files changed

+187
-33
lines changed

10 files changed

+187
-33
lines changed

components/apify/actions/get-dataset-items/get-dataset-items.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "apify-get-dataset-items",
66
name: "Get Dataset Items",
77
description: "Returns data stored in a dataset. [See the documentation](https://docs.apify.com/api/v2/dataset-items-get)",
8-
version: "0.0.1",
8+
version: "0.0.2",
99
type: "action",
1010
props: {
1111
apify,

components/apify/actions/run-actor/run-actor.mjs

+143-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* eslint-disable no-unused-vars */
22
import apify from "../../apify.app.mjs";
3+
import { parseObject } from "../../common/utils.mjs";
4+
import { EVENT_TYPES } from "../../common/constants.mjs";
35

46
export default {
57
key: "apify-run-actor",
68
name: "Run Actor",
79
description: "Performs an execution of a selected actor in Apify. [See the documentation](https://docs.apify.com/api/v2#/reference/actors/run-collection/run-actor)",
8-
version: "0.0.2",
10+
version: "0.0.3",
911
type: "action",
1012
props: {
1113
apify,
@@ -14,6 +16,52 @@ export default {
1416
apify,
1517
"actorId",
1618
],
19+
},
20+
buildId: {
21+
propDefinition: [
22+
apify,
23+
"buildId",
24+
(c) => ({
25+
actorId: c.actorId,
26+
}),
27+
],
28+
reloadProps: true,
29+
},
30+
runAsynchronously: {
31+
type: "boolean",
32+
label: "Run Asynchronously",
33+
description: "Set to `true` to run the actor asynchronously",
34+
reloadProps: true,
35+
},
36+
timeout: {
37+
type: "string",
38+
label: "Timeout",
39+
description: "Optional timeout for the run, in seconds. By default, the run uses a timeout specified in the default run configuration for the Actor.",
40+
optional: true,
41+
},
42+
memory: {
43+
type: "string",
44+
label: "Memory",
45+
description: "Memory limit for the run, in megabytes. The amount of memory can be set to a power of 2 with a minimum of 128. By default, the run uses a memory limit specified in the default run configuration for the Actor.",
46+
optional: true,
47+
},
48+
maxItems: {
49+
type: "string",
50+
label: "Max Items",
51+
description: "The maximum number of items that the Actor run should return. This is useful for pay-per-result Actors, as it allows you to limit the number of results that will be charged to your subscription. You can access the maximum number of items in your Actor by using the ACTOR_MAX_PAID_DATASET_ITEMS environment variable.",
52+
optional: true,
53+
},
54+
maxTotalChargeUsd: {
55+
type: "string",
56+
label: "Max Total Charge USD",
57+
description: "Specifies the maximum cost of the Actor run. This parameter is useful for pay-per-event Actors, as it allows you to limit the amount charged to your subscription. You can access the maximum cost in your Actor by using the ACTOR_MAX_TOTAL_CHARGE_USD environment variable.",
58+
optional: true,
59+
},
60+
webhook: {
61+
type: "string",
62+
label: "Webhook",
63+
description: "Specifies optional webhook associated with the Actor run, which can be used to receive a notification e.g. when the Actor finished or failed.",
64+
optional: true,
1765
reloadProps: true,
1866
},
1967
},
@@ -28,16 +76,14 @@ export default {
2876
? type
2977
: "string[]";
3078
},
31-
async getSchema() {
32-
const { data: { items: builds } } = await this.apify.listBuilds(this.actorId);
33-
const buildId = builds.at(-1).id;
79+
async getSchema(buildId) {
3480
const { data: { inputSchema } } = await this.apify.getBuild(buildId);
3581
return JSON.parse(inputSchema);
3682
},
3783
async prepareData(data) {
3884
const newData = {};
3985

40-
const { properties } = await this.getSchema();
86+
const { properties } = await this.getSchema(this.buildId);
4187
for (const [
4288
key,
4389
value,
@@ -85,27 +131,64 @@ export default {
85131
},
86132
async additionalProps() {
87133
const props = {};
88-
if (this.actorId) {
89-
const {
90-
properties, required: requiredProps = [],
91-
} = await this.getSchema();
134+
if (this.buildId) {
135+
try {
136+
const {
137+
properties, required: requiredProps = [],
138+
} = await this.getSchema(this.buildId);
92139

93-
for (const [
94-
key,
95-
value,
96-
] of Object.entries(properties)) {
97-
if (value.editor === "hidden") continue;
140+
for (const [
141+
key,
142+
value,
143+
] of Object.entries(properties)) {
144+
if (value.editor === "hidden") continue;
98145

99-
props[key] = {
100-
type: this.getType(value.type),
101-
label: value.title,
102-
description: value.description,
103-
optional: !requiredProps.includes(key),
146+
props[key] = {
147+
type: this.getType(value.type),
148+
label: value.title,
149+
description: value.description,
150+
optional: !requiredProps.includes(key),
151+
};
152+
const options = this.prepareOptions(value);
153+
if (options) props[key].options = options;
154+
if (value.default) {
155+
props[key].description += ` Default: \`${JSON.stringify(value.default)}\``;
156+
if (props[key].type !== "object") { // default values don't work properly for object props
157+
props[key].default = value.default;
158+
}
159+
}
160+
}
161+
} catch {
162+
props.properties = {
163+
type: "object",
164+
label: "Properties",
165+
description: "Properties to set for this actor",
166+
};
167+
}
168+
if (this.runAsynchronously) {
169+
props.outputRecordKey = {
170+
type: "string",
171+
label: "Output Record Key",
172+
description: "Key of the record from run's default key-value store to be returned in the response. By default, it is OUTPUT.",
173+
optional: true,
174+
};
175+
} else {
176+
props.waitForFinish = {
177+
type: "string",
178+
label: "Wait For Finish",
179+
description: "The maximum number of seconds the server waits for the run to finish. By default, it is 0, the maximum value is 60. If the build finishes in time then the returned run object will have a terminal status (e.g. SUCCEEDED), otherwise it will have a transitional status (e.g. RUNNING).",
180+
optional: true,
104181
};
105-
const options = this.prepareOptions(value);
106-
if (options) props[key].options = options;
107182
}
108183
}
184+
if (this.webhook) {
185+
props.eventTypes = {
186+
type: "string[]",
187+
label: "Event Types",
188+
description: "The types of events to send to the webhook",
189+
options: EVENT_TYPES,
190+
};
191+
}
109192
return props;
110193
},
111194
async run({ $ }) {
@@ -117,14 +200,50 @@ export default {
117200
prepareData,
118201
apify,
119202
actorId,
203+
buildId,
204+
properties,
205+
runAsynchronously,
206+
outputRecordKey,
207+
timeout,
208+
memory,
209+
maxItems,
210+
maxTotalChargeUsd,
211+
waitForFinish,
212+
webhook,
213+
eventTypes,
120214
...data
121215
} = this;
122216

123-
const response = await apify.runActor({
217+
const fn = runAsynchronously
218+
? apify.runActorAsynchronously
219+
: apify.runActor;
220+
221+
const response = await fn({
124222
actorId,
125-
data: await prepareData(data),
223+
data: properties
224+
? parseObject(properties)
225+
: await prepareData(data),
226+
params: {
227+
outputRecordKey,
228+
timeout,
229+
memory,
230+
maxItems,
231+
maxTotalChargeUsd,
232+
waitForFinish,
233+
webhooks: webhook
234+
? btoa(JSON.stringify([
235+
{
236+
eventTypes,
237+
requestUrl: webhook,
238+
},
239+
]))
240+
: undefined,
241+
},
126242
});
127-
$.export("$summary", `Successfully started actor run with ID: ${response.data.id}`);
243+
const summary = this.runAsynchronously
244+
? `Successfully started actor run with ID: ${response.data.id}`
245+
: `Successfully ran actor with ID: ${this.actorId}`;
246+
$.export("$summary", `${summary}`);
128247
return response;
129248
},
130249
};

components/apify/actions/run-task-synchronously/run-task-synchronously.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "apify-run-task-synchronously",
55
name: "Run Task Synchronously",
66
description: "Run a specific task and return its dataset items. [See the documentation](https://docs.apify.com/api/v2/actor-task-run-sync-get-dataset-items-get)",
7-
version: "0.0.1",
7+
version: "0.0.2",
88
type: "action",
99
props: {
1010
apify,

components/apify/actions/scrape-single-url/scrape-single-url.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "apify-scrape-single-url",
66
name: "Scrape Single URL",
77
description: "Executes a scraper on a specific website and returns its content as text. This action is perfect for extracting content from a single page.",
8-
version: "0.0.2",
8+
version: "0.0.3",
99
type: "action",
1010
props: {
1111
apify,

components/apify/actions/set-key-value-store-record/set-key-value-store-record.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "apify-set-key-value-store-record",
66
name: "Set Key-Value Store Record",
77
description: "Create or update a record in the key-value store of Apify. [See the documentation](https://docs.apify.com/api/v2#/reference/key-value-stores/record-collection/put-record)",
8-
version: "0.0.2",
8+
version: "0.0.3",
99
type: "action",
1010
props: {
1111
apify,

components/apify/apify.app.mjs

+28-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default {
2929
actorId: {
3030
type: "string",
3131
label: "Actor ID",
32-
description: "The ID of the actor to run.",
32+
description: "Actor ID or a tilde-separated owner's username and Actor name",
3333
async options({ page }) {
3434
const { data: { items } } = await this.listActors({
3535
params: {
@@ -105,6 +105,23 @@ export default {
105105
})) || [];
106106
},
107107
},
108+
buildId: {
109+
type: "string",
110+
label: "Build",
111+
description: "Specifies the Actor build to run. It can be either a build tag or build number.",
112+
async options({
113+
page, actorId,
114+
}) {
115+
const { data: { items } } = await this.listBuilds({
116+
actorId,
117+
params: {
118+
offset: LIMIT * page,
119+
limit: LIMIT,
120+
},
121+
});
122+
return items?.map(({ id }) => id) || [];
123+
},
124+
},
108125
clean: {
109126
type: "boolean",
110127
label: "Clean",
@@ -171,6 +188,15 @@ export default {
171188
},
172189
runActor({
173190
actorId, ...opts
191+
}) {
192+
return this._makeRequest({
193+
method: "POST",
194+
path: `/acts/${actorId}/run-sync`,
195+
...opts,
196+
});
197+
},
198+
runActorAsynchronously({
199+
actorId, ...opts
174200
}) {
175201
return this._makeRequest({
176202
method: "POST",
@@ -207,7 +233,7 @@ export default {
207233
...opts,
208234
});
209235
},
210-
listBuilds(actorId) {
236+
listBuilds({ actorId }) {
211237
return this._makeRequest({
212238
path: `/acts/${actorId}/builds`,
213239
});

components/apify/common/constants.mjs

+9
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
export const ACTOR_ID = "aYG0l9s7dbB7j3gbS";
22
export const LIMIT = 100;
3+
4+
export const EVENT_TYPES = [
5+
"ACTOR.RUN.CREATED",
6+
"ACTOR.RUN.SUCCEEDED",
7+
"ACTOR.RUN.FAILED",
8+
"ACTOR.RUN.ABORTED",
9+
"ACTOR.RUN.TIMED_OUT",
10+
"ACTOR.RUN.RESURRECTED",
11+
];

components/apify/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/apify",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Pipedream Apify Components",
55
"main": "apify.app.mjs",
66
"keywords": [

components/apify/sources/new-finished-actor-run-instant/new-finished-actor-run-instant.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "apify-new-finished-actor-run-instant",
77
name: "New Finished Actor Run (Instant)",
88
description: "Emit new event when a selected actor is run and finishes.",
9-
version: "0.0.2",
9+
version: "0.0.3",
1010
type: "source",
1111
dedupe: "unique",
1212
props: {

components/apify/sources/new-finished-task-run-instant/new-finished-task-run-instant.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "apify-new-finished-task-run-instant",
77
name: "New Finished Task Run (Instant)",
88
description: "Emit new event when a selected task is run and finishes.",
9-
version: "0.0.2",
9+
version: "0.0.3",
1010
type: "source",
1111
dedupe: "unique",
1212
props: {

0 commit comments

Comments
 (0)