Skip to content

Commit 3e3e0e9

Browse files
authored
[Components] CsvBox new components (#18942)
1 parent 4ce86a4 commit 3e3e0e9

File tree

6 files changed

+329
-157
lines changed

6 files changed

+329
-157
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import FormData from "form-data";
2+
import { getFileStream } from "@pipedream/platform";
3+
import app from "../../csvbox.app.mjs";
4+
5+
export default {
6+
key: "csvbox-submit-spreadsheet",
7+
name: "Submit Spreadsheet",
8+
description: "Submit a spreadsheet file via public URL or local file path to CSVBox for processing. [See documentation](https://help.csvbox.io/advanced-installation/rest-file-api)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
app,
13+
file: {
14+
type: "string",
15+
label: "File Path Or URL",
16+
description: "Provide either a file URL or a path to a file in the `/tmp` directory (for example, `/tmp/spreadsheet.csv`).",
17+
},
18+
sheetLicenseKey: {
19+
propDefinition: [
20+
app,
21+
"sheetLicenseKey",
22+
],
23+
},
24+
userId: {
25+
propDefinition: [
26+
app,
27+
"userId",
28+
],
29+
},
30+
hasHeaders: {
31+
propDefinition: [
32+
app,
33+
"hasHeaders",
34+
],
35+
},
36+
syncDir: {
37+
type: "dir",
38+
accessMode: "read",
39+
sync: true,
40+
optional: true,
41+
},
42+
},
43+
annotations: {
44+
readOnlyHint: false,
45+
destructiveHint: false,
46+
openWorldHint: true,
47+
},
48+
methods: {
49+
booleanToNumber(value) {
50+
return value === true || value === "true" || value === "1" || value === 1
51+
? 1
52+
: 0;
53+
},
54+
},
55+
async run({ $ }) {
56+
const {
57+
app,
58+
booleanToNumber,
59+
file,
60+
sheetLicenseKey,
61+
userId,
62+
hasHeaders,
63+
} = this;
64+
let data;
65+
66+
const isUrl = file?.startsWith("http://") || file?.startsWith("https://");
67+
68+
const otherFields = {
69+
...(userId
70+
? {
71+
user: {
72+
user_id: userId,
73+
},
74+
}
75+
: {}
76+
),
77+
...(hasHeaders
78+
? {
79+
options: {
80+
has_header: booleanToNumber(hasHeaders),
81+
},
82+
}
83+
: {}
84+
),
85+
};
86+
87+
if (isUrl) {
88+
data = {
89+
import: {
90+
public_file_url: file,
91+
sheet_license_key: sheetLicenseKey,
92+
...otherFields,
93+
},
94+
};
95+
96+
} else {
97+
data = new FormData();
98+
data.append("file", await getFileStream(file));
99+
data.append("import", JSON.stringify({
100+
sheet_license_key: sheetLicenseKey,
101+
...otherFields,
102+
}));
103+
}
104+
105+
const response = await app.submitFile({
106+
$,
107+
headers: !isUrl
108+
? {
109+
"Content-Type": "multipart/form-data",
110+
}
111+
: undefined,
112+
data,
113+
});
114+
115+
$.export("$summary", "Successfully submitted spreadsheet");
116+
117+
return response;
118+
},
119+
};

components/csvbox/csvbox.app.mjs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,55 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "csvbox",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
sheetLicenseKey: {
8+
type: "string",
9+
label: "Sheet License Key",
10+
description: "The unique identifier for your CSVBox sheet. You can find it in **Sheets - Edit - Code Snippet - Sheet License Key**.",
11+
},
12+
userId: {
13+
type: "string",
14+
label: "User ID",
15+
description: "The unique identifier for the user. You can find it in the **Dashboard - Edit - Code Snippet**.",
16+
optional: true,
17+
},
18+
hasHeaders: {
19+
type: "boolean",
20+
label: "Has Headers",
21+
description: "Whether the spreadsheet has headers.",
22+
optional: true,
23+
},
24+
},
525
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
26+
getUrl(path) {
27+
return `https://api.csvbox.io/1.1${path}`;
28+
},
29+
getHeaders(headers) {
30+
return {
31+
"Content-Type": "application/json",
32+
"x-csvbox-api-key": `${this.$auth.api_key}`,
33+
"x-csvbox-secret-api-key": `${this.$auth.secret_api_key}`,
34+
...headers,
35+
};
36+
},
37+
_makeRequest({
38+
$ = this, path, headers, ...args
39+
} = {}) {
40+
return axios($, {
41+
debug: true,
42+
url: this.getUrl(path),
43+
headers: this.getHeaders(headers),
44+
...args,
45+
});
46+
},
47+
submitFile(args = {}) {
48+
return this._makeRequest({
49+
method: "POST",
50+
path: "/file",
51+
...args,
52+
});
953
},
1054
},
11-
};
55+
};

components/csvbox/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/csvbox",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream CSVbox Components",
55
"main": "csvbox.app.mjs",
66
"keywords": [
@@ -11,5 +11,9 @@
1111
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.0",
17+
"form-data": "^4.0.4"
1418
}
15-
}
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import app from "../../csvbox.app.mjs";
3+
import sampleEvent from "./test-event.mjs";
4+
5+
export default {
6+
key: "csvbox-new-import",
7+
name: "New Import",
8+
description: "Emit new event when CSVBox receives and processes a new import. [See documentation](https://help.csvbox.io/destinations#api-webhook)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
app,
14+
http: "$.interface.http",
15+
},
16+
async run({
17+
headers, body,
18+
}) {
19+
20+
if (!headers["content-type"] || headers["content-type"] !== "application/json") {
21+
throw new ConfigurationError("Invalid content type. Please check your CSVBox webhook configuration so that the content type is set to `JSON`.");
22+
}
23+
24+
const ts = Date.now();
25+
26+
this.$emit(body, {
27+
id: ts,
28+
summary: "New import has been processed",
29+
ts,
30+
});
31+
},
32+
sampleEvent,
33+
};
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
export default [
2+
{
3+
"import_id": 1841488,
4+
"sheet_id": 19247,
5+
"sheet_name": "Test 100",
6+
"row_number": 1,
7+
"total_rows": 5,
8+
"env_name": "default",
9+
"original_filename": "basicScience.csv",
10+
"custom_fields": [],
11+
"row_data": {
12+
"ID": "2",
13+
"Question": "What type of paper detects acids and alkali in liquid?",
14+
"Answer": "Litmus Paper",
15+
"Option B": "Filter paper",
16+
"Option C": "Hydrion Paper",
17+
"Option D": "Proton Paper"
18+
},
19+
"import_description": ""
20+
},
21+
{
22+
"import_id": 1841488,
23+
"sheet_id": 19247,
24+
"sheet_name": "Test 100",
25+
"row_number": 2,
26+
"total_rows": 5,
27+
"env_name": "default",
28+
"original_filename": "basicScience.csv",
29+
"custom_fields": [],
30+
"row_data": {
31+
"ID": "3",
32+
"Question": "What is the only metal to be liquid at room temperature? ",
33+
"Answer": "Mercury",
34+
"Option B": "Nickel",
35+
"Option C": "Chromium",
36+
"Option D": "Zirconium"
37+
},
38+
"import_description": ""
39+
},
40+
{
41+
"import_id": 1841488,
42+
"sheet_id": 19247,
43+
"sheet_name": "Test 100",
44+
"row_number": 3,
45+
"total_rows": 5,
46+
"env_name": "default",
47+
"original_filename": "basicScience.csv",
48+
"custom_fields": [],
49+
"row_data": {
50+
"ID": "4",
51+
"Question": "What is the chemical name of aqua fortis?",
52+
"Answer": "Nitric Acid",
53+
"Option B": "Hydrochloric Acid",
54+
"Option C": "Benzoic Acid",
55+
"Option D": "Acetic Acid"
56+
},
57+
"import_description": ""
58+
},
59+
{
60+
"import_id": 1841488,
61+
"sheet_id": 19247,
62+
"sheet_name": "Test 100",
63+
"row_number": 4,
64+
"total_rows": 5,
65+
"env_name": "default",
66+
"original_filename": "basicScience.csv",
67+
"custom_fields": [],
68+
"row_data": {
69+
"ID": "5",
70+
"Question": "What is the fourth state of matter after solid and liquid and gas?",
71+
"Answer": "Plasma",
72+
"Option B": "Ground State",
73+
"Option C": "Metal ",
74+
"Option D": "Flora"
75+
},
76+
"import_description": ""
77+
},
78+
{
79+
"import_id": 1841488,
80+
"sheet_id": 19247,
81+
"sheet_name": "Test 100",
82+
"row_number": 5,
83+
"total_rows": 5,
84+
"env_name": "default",
85+
"original_filename": "basicScience.csv",
86+
"custom_fields": [],
87+
"row_data": {
88+
"ID": "6",
89+
"Question": "What is the chemical symbol for Plutonium? ",
90+
"Answer": "Pu",
91+
"Option B": "Pa",
92+
"Option C": "Po",
93+
"Option D": "P"
94+
},
95+
"import_description": ""
96+
}
97+
];

0 commit comments

Comments
 (0)