Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions components/agrello/actions/get-document/get-document.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import agrello from "../../agrello.app.mjs";

export default {
key: "agrello-get-document",
name: "Get Document",
description: "Get a document in Agrello. [See the documentation](https://api.agrello.io/public/webjars/swagger-ui/index.html)",
version: "0.0.1",
type: "action",
props: {
agrello,
folderId: {
propDefinition: [
agrello,
"folderId",
],
},
documentId: {
propDefinition: [
agrello,
"documentId",
({ folderId }) => ({
folderId,
}),
],
},
},
async run({ $ }) {
const response = await this.agrello.getDocument({
$,
documentId: this.documentId,
});
$.export("$summary", `Successfully retrieved document with ID ${this.documentId}`);
return response;
},
};
143 changes: 138 additions & 5 deletions components/agrello/agrello.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,144 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "agrello",
propDefinitions: {},
propDefinitions: {
folderId: {
type: "string",
label: "Folder ID",
description: "The ID of the folder",
async options({ page }) {
return await this.listFolders({
params: {
page,
},
});
},
},
documentId: {
type: "string",
label: "Document ID",
description: "The ID of the document",
async options({
folderId, page,
}) {
const { content } = await this.listDocuments({
folderId,
params: {
page,
},
});
return content.map(({
id: value, name: label,
}) => ({
label,
value,
}));
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://api.agrello.io/public/v3";
},
_headers(headers = {}) {
return {
...headers,
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
};
},
_makeRequest({
$ = this, path, headers, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(headers),
...opts,
});
},
async listFolders() {
const { content } = await this._makeRequest({
path: "/folders",
});

const folders = [];
for (const parent of content) {
folders.push({
label: `${parent.name}`,
value: parent.id,
});
folders.push(...await this.getSubFolders(parent.name, parent.id));
}

return folders;

},
async getSubFolders(parentName, parentId) {
const folders = [];
const { subspaces } = await this._makeRequest({
path: `/folders/${parentId}/folders`,
});
for (const folder of subspaces) {
const label = `${parentName} - ${folder.name}`;
folders.push({
label,
value: folder.id,
});
folders.push(...await this.getSubFolders(label, folder.id));
}
return folders;
},
listDocuments({
folderId, ...opts
}) {
return this._makeRequest({
path: `/folders/${folderId}/containers`,
...opts,
});
},
getDocument({ documentId }) {
return this._makeRequest({
path: `/containers/${documentId}`,
});
},
createWebhook(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/webhooks",
...opts,
});
},
deleteWebhook(hookId) {
return this._makeRequest({
method: "DELETE",
path: `/webhooks/${hookId}`,
});
},
async *paginate({
fn, params = {}, maxResults = null, ...opts
}) {
let hasMore = false;
let count = 0;
let page = 0;

do {
params.page = page++;
const { content } = await fn({
params,
...opts,
});
for (const d of content) {
yield d;

if (maxResults && ++count === maxResults) {
return count;
}
}

hasMore = content.length;

} while (hasMore);
},
},
};
};
31 changes: 31 additions & 0 deletions components/agrello/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const checkTmp = (filename) => {
if (!filename.startsWith("/tmp")) {
return `/tmp/${filename}`;
}
return filename;
};

export const parseObject = (obj) => {
if (!obj) return undefined;

if (Array.isArray(obj)) {
return obj.map((item) => {
if (typeof item === "string") {
try {
return JSON.parse(item);
} catch (e) {
return item;
}
}
return item;
});
}
if (typeof obj === "string") {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
return obj;
};
8 changes: 6 additions & 2 deletions components/agrello/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/agrello",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Agrello Components",
"main": "agrello.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.1"
}
}
}

44 changes: 44 additions & 0 deletions components/agrello/sources/common/base.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import agrello from "../../agrello.app.mjs";

export default {
props: {
agrello,
http: {
type: "$.interface.http",
customResponse: false,
},
db: "$.service.db",
},
methods: {
_setHookId(hookId) {
this.db.set("webhookId", hookId);
},
_getHookId() {
return this.db.get("webhookId");
},
},
hooks: {
async activate() {
const data = await this.agrello.createWebhook({
data: {
event: this.getEvent(),
url: this.http.endpoint,
},
});

this._setHookId(data.id);
},
async deactivate() {
const webhookId = this._getHookId();
await this.agrello.deleteWebhook(webhookId);
},
},
async run({ body: { event } }) {
const ts = Date.parse(new Date());
this.$emit(event, {
id: `${event.containerId}-${ts}`,
summary: this.getSummary(event),
ts: ts,
});
},
};
78 changes: 78 additions & 0 deletions components/agrello/sources/new-document/new-document.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
import agrello from "../../agrello.app.mjs";
import sampleEmit from "./test-event.mjs";

export default {
key: "agrello-new-document",
name: "New Document Added to Folder",
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)",
version: "0.0.1",
type: "source",
dedupe: "unique",
props: {
agrello,
db: "$.service.db",
timer: {
type: "$.interface.timer",
default: {
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},
folderId: {
propDefinition: [
agrello,
"folderId",
],
},
},
methods: {
_getLastDate() {
return this.db.get("lastDate") || 0;
},
_setLastDate(lastDate) {
this.db.set("lastDate", lastDate);
},
async emitEvent(maxResults = false) {
const lastDate = this._getLastDate();

const response = this.agrello.paginate({
fn: this.agrello.listDocuments,
folderId: this.folderId,
params: {
sort: "createdAt,DESC",
},
maxResults,
});

let responseArray = [];
for await (const item of response) {
if (Date.parse(item.createdAt) <= lastDate) break;
responseArray.push(item);
}

if (responseArray.length) {
if (maxResults && (responseArray.length > maxResults)) {
responseArray.length = maxResults;
}
this._setLastDate(Date.parse(responseArray[0].createdAt));
}

for (const item of responseArray.reverse()) {
this.$emit(item, {
id: item.id,
summary: `New Document: ${item.name}`,
ts: Date.parse(item.createdAt),
});
}
},
},
hooks: {
async deploy() {
await this.emitEvent(25);
},
},
async run() {
await this.emitEvent();
},
sampleEmit,
};
Loading