Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Components - documentpro #12515

Merged
merged 10 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions components/documentpro/actions/new-document/new-document.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import FormData from "form-data";
import fs from "fs";
import { checkTmp } from "../../common/utils.mjs";
import documentpro from "../../documentpro.app.mjs";

export default {
key: "documentpro-new-document",
name: "Upload New Document",
description: "Uploads a document to DocumentPro's parser. [See the documentation](https://docs.documentpro.ai/docs/using-api/manage-documents/import-files)",
version: "0.0.1",
type: "action",
props: {
documentpro,
parserId: {
propDefinition: [
documentpro,
"parserId",
],
},
document: {
propDefinition: [
documentpro,
"document",
],
},
},
async run({ $ }) {
const formData = new FormData();
const file = fs.createReadStream(checkTmp(this.document));
formData.append("file", file);

const response = await this.documentpro.uploadDocument({
parserId: this.parserId,
data: formData,
headers: formData.getHeaders(),
});

$.export("$summary", `Successfully uploaded document with Id: ${response.request_id}`);
return response;
},
};
6 changes: 6 additions & 0 deletions components/documentpro/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const checkTmp = (filename) => {
if (filename.indexOf("/tmp") === -1) {
return `/tmp/${filename}`;
}
return filename;
};
70 changes: 65 additions & 5 deletions components/documentpro/documentpro.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,71 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "documentpro",
propDefinitions: {},
propDefinitions: {
document: {
type: "string",
label: "Document",
description: "The path to the file saved to the `/tmp` directory (e.g. `/tmp/example.pdf`). [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory).",
},
parserId: {
type: "string",
label: "Parser ID",
description: "The ID of the parser to use for uploading the document",
async options() {
const { items } = await this.getParsers();
return items.map(({
template_title: label, template_id: value,
}) => ({
label,
value,
}));
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://api.documentpro.ai";
},
_headers(headers = {}) {
return {
...headers,
"x-api-key": this.$auth.api_key,
};
},
_makeRequest({
$ = this, path = "", headers, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(headers),
...opts,
});
},
getParsers(opts = {}) {
return this._makeRequest({
path: "/v1/templates",
...opts,
});
},
updateParser({
parserId, ...opts
}) {
return this._makeRequest({
method: "PUT",
path: `/v1/templates/${parserId}`,
...opts,
});
},
uploadDocument({
parserId, ...opts
}) {
return this._makeRequest({
method: "POST",
path: `/files/upload/${parserId}`,
...opts,
});
},
},
};
};
8 changes: 6 additions & 2 deletions components/documentpro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/documentpro",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream DocumentPro Components",
"main": "documentpro.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.0",
"form-data": "^4.0.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import documentpro from "../../documentpro.app.mjs";
import sampleEmit from "./test-event.mjs";

export default {
key: "documentpro-new-document-updated-instant",
name: "New Document Updated (Instant)",
description: "Emit new event when a file request status changes. You can only create one webhook in a parser at a time.",
version: "0.0.1",
type: "source",
dedupe: "unique",
props: {
documentpro,
http: {
type: "$.interface.http",
customResponse: true,
},
db: "$.service.db",
parserId: {
propDefinition: [
documentpro,
"parserId",
],
},
},
hooks: {
async activate() {
await this.documentpro.updateParser({
parserId: this.parserId,
data: {
webhook_url: this.http.endpoint,
},
});
},
async deactivate() {
await this.documentpro.updateParser({
parserId: this.parserId,
data: {
webhook_url: null,
},
});
},
},
async run({ body }) {
this.$emit(body, {
id: `${body.data.request_id}-${body.timestamp}`,
summary: `New document (${body.data.response_body.file_name}) status updated: ${body.event} - ${body.data.request_status}`,
ts: Date.parse(body.timestamp),
});

this.http.respond({
status: 200,
body: {
message: "Received",
},
});
},
sampleEmit,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default {
"event": "file_request_status_change",
"timestamp": "2023-07-30T19:05:29.565249",
"data": {
"request_id": "your-request-id",
"request_status": "completed",
"response_body": {
"file_name": "your-file-name",
"file_presigned_url": "temporary-url-to-your-file",
"user_error_msg": null,
"template_id": "your-template-id",
"template_type": "receipt",
"template_title": "Receipt",
"num_pages": 2,
"result_json_data": {}
},
"created_at": "2023-07-30T19:05:10.696893",
"updated_at": "2023-07-30T19:05:29.565249"
}
}
11 changes: 8 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading