Skip to content

Commit 58adceb

Browse files
committed
[Components] jigsawstack #13924
Actions - Verify Email - Object Detection - Sentiment Analysis
1 parent 9ab0c77 commit 58adceb

File tree

6 files changed

+139
-96
lines changed

6 files changed

+139
-96
lines changed
Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,73 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import fs from "fs";
3+
import mime from "mime";
4+
import {
5+
checkTmp,
6+
throwError,
7+
} from "../../common/utils.mjs";
18
import jigsawstack from "../../jigsawstack.app.mjs";
2-
import { axios } from "@pipedream/platform";
39

410
export default {
511
key: "jigsawstack-object-detection",
612
name: "Object Detection",
713
description: "Recognize objects within a provided image and retrieve it with great accuracy. [See the documentation](https://docs.jigsawstack.com/api-reference/ai/object-detection)",
8-
version: "0.0.{{ts}}",
14+
version: "0.0.1",
915
type: "action",
1016
props: {
1117
jigsawstack,
12-
imageUrl: {
13-
propDefinition: [
14-
jigsawstack,
15-
"imageUrl",
16-
],
18+
url: {
19+
type: "string",
20+
label: "Image URL",
21+
description: "The URL of the image to process.",
22+
optional: true,
23+
},
24+
fileStoreKey: {
25+
type: "string",
26+
label: "File Store Key",
27+
description: "The key used to store the image on Jigsawstack file [Storage](https://docs.jigsawstack.com/api-reference/store/file/add).",
28+
optional: true,
29+
},
30+
imageFile: {
31+
type: "string",
32+
label: "Image File",
33+
description: "The path to a file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp).",
34+
optional: true,
1735
},
1836
},
1937
async run({ $ }) {
20-
const response = await this.jigsawstack.detectObjectsInImage({
21-
imageUrl: this.imageUrl,
22-
});
23-
$.export("$summary", "Successfully detected objects in the image");
24-
return response;
38+
const {
39+
jigsawstack,
40+
...data
41+
} = this;
42+
43+
if (Object.keys(data).length > 1) {
44+
throw new ConfigurationError("You must provide only one option, either the **Image URL**, the **Image File**, or the **File Storage Key**.");
45+
}
46+
47+
if (data.fileStoreKey) data.file_store_key = data.fileStoreKey;
48+
49+
if (data.imageFile) {
50+
const filePath = checkTmp(data.imageFile);
51+
const file = fs.readFileSync(filePath);
52+
const { key } = await jigsawstack.uploadFile({
53+
headers: {
54+
"Content-Type": mime.getType(filePath),
55+
},
56+
data: file,
57+
});
58+
data.file_store_key = key;
59+
}
60+
61+
try {
62+
const response = await jigsawstack.detectObjectsInImage({
63+
$,
64+
data,
65+
});
66+
$.export("$summary", "Successfully detected objects in the image");
67+
return response;
68+
69+
} catch (e) {
70+
return throwError(e);
71+
}
2572
},
2673
};
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1+
import { throwError } from "../../common/utils.mjs";
12
import jigsawstack from "../../jigsawstack.app.mjs";
2-
import { axios } from "@pipedream/platform";
33

44
export default {
55
key: "jigsawstack-sentiment-analysis",
66
name: "Sentiment Analysis",
77
description: "Assess sentiment of a provided text. Vibes can be positive, negative, or neutral. [See the documentation](https://docs.jigsawstack.com/api-reference/ai/sentiment)",
8-
version: "0.0.{{ts}}",
8+
version: "0.0.1",
99
type: "action",
1010
props: {
1111
jigsawstack,
1212
text: {
13-
propDefinition: [
14-
jigsawstack,
15-
"text",
16-
],
13+
type: "string",
14+
label: "Text",
15+
description: "The text to analyze for sentiment.",
1716
},
1817
},
1918
async run({ $ }) {
20-
const response = await this.jigsawstack.analyzeSentiment({
21-
text: this.text,
22-
});
19+
try {
20+
const response = await this.jigsawstack.analyzeSentiment({
21+
$,
22+
data: {
23+
text: this.text,
24+
},
25+
});
2326

24-
$.export("$summary", `Successfully analyzed sentiment with emotion: ${response.sentiment.emotion} and sentiment: ${response.sentiment.sentiment}`);
25-
return response;
27+
$.export("$summary", `Successfully analyzed sentiment with emotion: ${response.sentiment.emotion} and sentiment: ${response.sentiment.sentiment}`);
28+
return response;
29+
} catch (e) {
30+
return throwError(e);
31+
}
2632
},
2733
};
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1+
import { throwError } from "../../common/utils.mjs";
12
import jigsawstack from "../../jigsawstack.app.mjs";
2-
import { axios } from "@pipedream/platform";
33

44
export default {
55
key: "jigsawstack-verify-email",
66
name: "Verify Email",
77
description: "Validate any email address and determine deliverability as well as disposable status. [See the documentation](https://docs.jigsawstack.com/api-reference/validate/email)",
8-
version: "0.0.{{ts}}",
8+
version: "0.0.1",
99
type: "action",
1010
props: {
1111
jigsawstack,
1212
email: {
13-
propDefinition: [
14-
jigsawstack,
15-
"email",
16-
],
13+
type: "string",
14+
label: "Email Address",
15+
description: "The email address to validate.",
1716
},
1817
},
1918
async run({ $ }) {
20-
const response = await this.jigsawstack.validateEmail({
21-
email: this.email,
22-
});
19+
try {
20+
const response = await this.jigsawstack.validateEmail({
21+
$,
22+
params: {
23+
email: this.email,
24+
},
25+
});
2326

24-
$.export("$summary", `Successfully validated email: ${this.email}`);
25-
return response;
27+
$.export("$summary", `Successfully validated email: ${this.email}`);
28+
return response;
29+
} catch (e) {
30+
return throwError(e);
31+
}
2632
},
2733
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
3+
export const throwError = ({ message }) => {
4+
const errorMessage = JSON.parse(message).message;
5+
throw new ConfigurationError(errorMessage);
6+
};
7+
8+
export const checkTmp = (filename) => {
9+
if (!filename.startsWith("/tmp")) {
10+
return `/tmp/${filename}`;
11+
}
12+
return filename;
13+
};

components/jigsawstack/jigsawstack.app.mjs

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,52 @@ import { axios } from "@pipedream/platform";
33
export default {
44
type: "app",
55
app: "jigsawstack",
6-
propDefinitions: {
7-
email: {
8-
type: "string",
9-
label: "Email Address",
10-
description: "The email address to validate.",
11-
},
12-
imageUrl: {
13-
type: "string",
14-
label: "Image URL",
15-
description: "The URL of the image to process.",
16-
},
17-
text: {
18-
type: "string",
19-
label: "Text",
20-
description: "The text to analyze for sentiment.",
21-
},
22-
},
236
methods: {
247
_baseUrl() {
25-
return "https://api.jigsawstack.com";
8+
return "https://api.jigsawstack.com/v1";
269
},
27-
async _makeRequest(opts = {}) {
28-
const {
29-
$ = this,
30-
method = "GET",
31-
path = "/",
32-
headers,
33-
...otherOpts
34-
} = opts;
10+
_headers(headers = {}) {
11+
return {
12+
"x-api-key": this.$auth.api_key,
13+
...headers,
14+
};
15+
},
16+
_makeRequest({
17+
$ = this, path, headers, ...opts
18+
}) {
3519
return axios($, {
36-
...otherOpts,
37-
method,
3820
url: this._baseUrl() + path,
39-
headers: {
40-
...headers,
41-
"x-api-key": this.$auth.api_key,
42-
},
21+
headers: this._headers(headers),
22+
...opts,
4323
});
4424
},
45-
async validateEmail(opts = {}) {
46-
const {
47-
email, ...otherOpts
48-
} = opts;
25+
validateEmail(opts = {}) {
4926
return this._makeRequest({
5027
method: "GET",
51-
path: "/v1/validate/email",
52-
params: {
53-
email,
54-
},
55-
...otherOpts,
28+
path: "/validate/email",
29+
...opts,
5630
});
5731
},
58-
async detectObjectsInImage(opts = {}) {
59-
const {
60-
imageUrl, ...otherOpts
61-
} = opts;
32+
detectObjectsInImage(opts = {}) {
6233
return this._makeRequest({
6334
method: "POST",
64-
path: "/v1/ai/object_detection",
65-
data: {
66-
url: imageUrl,
67-
},
68-
...otherOpts,
35+
path: "/ai/object_detection",
36+
...opts,
6937
});
7038
},
71-
async analyzeSentiment(opts = {}) {
72-
const {
73-
text, ...otherOpts
74-
} = opts;
39+
analyzeSentiment(opts = {}) {
7540
return this._makeRequest({
7641
method: "POST",
77-
path: "/v1/ai/sentiment",
78-
data: {
79-
text,
80-
},
81-
...otherOpts,
42+
path: "/ai/sentiment",
43+
...opts,
8244
});
8345
},
84-
authKeys() {
85-
console.log(Object.keys(this.$auth));
46+
uploadFile(opts = {}) {
47+
return this._makeRequest({
48+
method: "POST",
49+
path: "/store/file",
50+
...opts,
51+
});
8652
},
8753
},
8854
};

components/jigsawstack/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/jigsawstack",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream JigsawStack Components",
55
"main": "jigsawstack.app.mjs",
66
"keywords": [
@@ -11,5 +11,10 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.1",
17+
"form-data": "^4.0.0",
18+
"mime": "^4.0.4"
1419
}
1520
}

0 commit comments

Comments
 (0)