Skip to content

Commit bd50c3f

Browse files
authored
Merge branch 'master' into issue-13804
2 parents a4cd434 + 3dc0120 commit bd50c3f

File tree

122 files changed

+3691
-1513
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+3691
-1513
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import agrello from "../../agrello.app.mjs";
2+
3+
export default {
4+
key: "agrello-get-document",
5+
name: "Get Document",
6+
description: "Get a document in Agrello. [See the documentation](https://api.agrello.io/public/webjars/swagger-ui/index.html)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
agrello,
11+
folderId: {
12+
propDefinition: [
13+
agrello,
14+
"folderId",
15+
],
16+
},
17+
documentId: {
18+
propDefinition: [
19+
agrello,
20+
"documentId",
21+
({ folderId }) => ({
22+
folderId,
23+
}),
24+
],
25+
},
26+
},
27+
async run({ $ }) {
28+
const response = await this.agrello.getDocument({
29+
$,
30+
documentId: this.documentId,
31+
});
32+
$.export("$summary", `Successfully retrieved document with ID ${this.documentId}`);
33+
return response;
34+
},
35+
};

components/agrello/agrello.app.mjs

Lines changed: 138 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,144 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "agrello",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
folderId: {
8+
type: "string",
9+
label: "Folder ID",
10+
description: "The ID of the folder",
11+
async options({ page }) {
12+
return await this.listFolders({
13+
params: {
14+
page,
15+
},
16+
});
17+
},
18+
},
19+
documentId: {
20+
type: "string",
21+
label: "Document ID",
22+
description: "The ID of the document",
23+
async options({
24+
folderId, page,
25+
}) {
26+
const { content } = await this.listDocuments({
27+
folderId,
28+
params: {
29+
page,
30+
},
31+
});
32+
return content.map(({
33+
id: value, name: label,
34+
}) => ({
35+
label,
36+
value,
37+
}));
38+
},
39+
},
40+
},
541
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
42+
_baseUrl() {
43+
return "https://api.agrello.io/public/v3";
44+
},
45+
_headers(headers = {}) {
46+
return {
47+
...headers,
48+
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
49+
};
50+
},
51+
_makeRequest({
52+
$ = this, path, headers, ...opts
53+
}) {
54+
return axios($, {
55+
url: this._baseUrl() + path,
56+
headers: this._headers(headers),
57+
...opts,
58+
});
59+
},
60+
async listFolders() {
61+
const { content } = await this._makeRequest({
62+
path: "/folders",
63+
});
64+
65+
const folders = [];
66+
for (const parent of content) {
67+
folders.push({
68+
label: `${parent.name}`,
69+
value: parent.id,
70+
});
71+
folders.push(...await this.getSubFolders(parent.name, parent.id));
72+
}
73+
74+
return folders;
75+
76+
},
77+
async getSubFolders(parentName, parentId) {
78+
const folders = [];
79+
const { subspaces } = await this._makeRequest({
80+
path: `/folders/${parentId}/folders`,
81+
});
82+
for (const folder of subspaces) {
83+
const label = `${parentName} - ${folder.name}`;
84+
folders.push({
85+
label,
86+
value: folder.id,
87+
});
88+
folders.push(...await this.getSubFolders(label, folder.id));
89+
}
90+
return folders;
91+
},
92+
listDocuments({
93+
folderId, ...opts
94+
}) {
95+
return this._makeRequest({
96+
path: `/folders/${folderId}/containers`,
97+
...opts,
98+
});
99+
},
100+
getDocument({ documentId }) {
101+
return this._makeRequest({
102+
path: `/containers/${documentId}`,
103+
});
104+
},
105+
createWebhook(opts = {}) {
106+
return this._makeRequest({
107+
method: "POST",
108+
path: "/webhooks",
109+
...opts,
110+
});
111+
},
112+
deleteWebhook(hookId) {
113+
return this._makeRequest({
114+
method: "DELETE",
115+
path: `/webhooks/${hookId}`,
116+
});
117+
},
118+
async *paginate({
119+
fn, params = {}, maxResults = null, ...opts
120+
}) {
121+
let hasMore = false;
122+
let count = 0;
123+
let page = 0;
124+
125+
do {
126+
params.page = page++;
127+
const { content } = await fn({
128+
params,
129+
...opts,
130+
});
131+
for (const d of content) {
132+
yield d;
133+
134+
if (maxResults && ++count === maxResults) {
135+
return count;
136+
}
137+
}
138+
139+
hasMore = content.length;
140+
141+
} while (hasMore);
9142
},
10143
},
11-
};
144+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const checkTmp = (filename) => {
2+
if (!filename.startsWith("/tmp")) {
3+
return `/tmp/${filename}`;
4+
}
5+
return filename;
6+
};
7+
8+
export const parseObject = (obj) => {
9+
if (!obj) return undefined;
10+
11+
if (Array.isArray(obj)) {
12+
return obj.map((item) => {
13+
if (typeof item === "string") {
14+
try {
15+
return JSON.parse(item);
16+
} catch (e) {
17+
return item;
18+
}
19+
}
20+
return item;
21+
});
22+
}
23+
if (typeof obj === "string") {
24+
try {
25+
return JSON.parse(obj);
26+
} catch (e) {
27+
return obj;
28+
}
29+
}
30+
return obj;
31+
};

components/agrello/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/agrello",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Agrello Components",
55
"main": "agrello.app.mjs",
66
"keywords": [
@@ -11,5 +11,9 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.1"
1417
}
15-
}
18+
}
19+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import agrello from "../../agrello.app.mjs";
2+
3+
export default {
4+
props: {
5+
agrello,
6+
http: {
7+
type: "$.interface.http",
8+
customResponse: false,
9+
},
10+
db: "$.service.db",
11+
},
12+
methods: {
13+
_setHookId(hookId) {
14+
this.db.set("webhookId", hookId);
15+
},
16+
_getHookId() {
17+
return this.db.get("webhookId");
18+
},
19+
},
20+
hooks: {
21+
async activate() {
22+
const data = await this.agrello.createWebhook({
23+
data: {
24+
event: this.getEvent(),
25+
url: this.http.endpoint,
26+
},
27+
});
28+
29+
this._setHookId(data.id);
30+
},
31+
async deactivate() {
32+
const webhookId = this._getHookId();
33+
await this.agrello.deleteWebhook(webhookId);
34+
},
35+
},
36+
async run({ body: { event } }) {
37+
const ts = Date.parse(new Date());
38+
this.$emit(event, {
39+
id: `${event.containerId}-${ts}`,
40+
summary: this.getSummary(event),
41+
ts: ts,
42+
});
43+
},
44+
};
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import agrello from "../../agrello.app.mjs";
3+
import sampleEmit from "./test-event.mjs";
4+
5+
export default {
6+
key: "agrello-new-document",
7+
name: "New Document Added to Folder",
8+
description: "Emit new event when a user adds a document to a specific folder. [See the documentation](https://api.agrello.io/public/webjars/swagger-ui/index.html)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
props: {
13+
agrello,
14+
db: "$.service.db",
15+
timer: {
16+
type: "$.interface.timer",
17+
default: {
18+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
19+
},
20+
},
21+
folderId: {
22+
propDefinition: [
23+
agrello,
24+
"folderId",
25+
],
26+
},
27+
},
28+
methods: {
29+
_getLastDate() {
30+
return this.db.get("lastDate") || 0;
31+
},
32+
_setLastDate(lastDate) {
33+
this.db.set("lastDate", lastDate);
34+
},
35+
async emitEvent(maxResults = false) {
36+
const lastDate = this._getLastDate();
37+
38+
const response = this.agrello.paginate({
39+
fn: this.agrello.listDocuments,
40+
folderId: this.folderId,
41+
params: {
42+
sort: "createdAt,DESC",
43+
},
44+
maxResults,
45+
});
46+
47+
let responseArray = [];
48+
for await (const item of response) {
49+
if (Date.parse(item.createdAt) <= lastDate) break;
50+
responseArray.push(item);
51+
}
52+
53+
if (responseArray.length) {
54+
if (maxResults && (responseArray.length > maxResults)) {
55+
responseArray.length = maxResults;
56+
}
57+
this._setLastDate(Date.parse(responseArray[0].createdAt));
58+
}
59+
60+
for (const item of responseArray.reverse()) {
61+
this.$emit(item, {
62+
id: item.id,
63+
summary: `New Document: ${item.name}`,
64+
ts: Date.parse(item.createdAt),
65+
});
66+
}
67+
},
68+
},
69+
hooks: {
70+
async deploy() {
71+
await this.emitEvent(25);
72+
},
73+
},
74+
async run() {
75+
await this.emitEvent();
76+
},
77+
sampleEmit,
78+
};

0 commit comments

Comments
 (0)