Skip to content

Commit

Permalink
✨ add support for generated APIs (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianMindee authored Mar 12, 2024
1 parent 136d295 commit 37c0228
Show file tree
Hide file tree
Showing 16 changed files with 1,824 additions and 10,017 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "16"
node-version: "18"
cache: "npm"

- name: Install Node.js dependencies
Expand Down
42 changes: 42 additions & 0 deletions creates/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const defaults = require("./defaults");
module.exports = {
enqueueAndParse: async (z, inputDocument, apiOwner, apiName, apiVersion, headers, maxRetries = 60) => {
const enqueueResponse = await z.request({
url: `https://api.mindee.net/v1/products/${apiOwner}/${apiName}/v${apiVersion}/predict_async`,
method: 'POST',
body: {
'document': inputDocument
},
headers: headers,
});

const enqueueJson = JSON.parse(enqueueResponse.content);

let retryCount = 0;
if (enqueueJson?.job?.id) {
const jobId = enqueueJson.job.id;
let parseQueuedResponse = null;
while (retryCount < maxRetries) {
await new Promise(resolve => setTimeout(resolve, 2000));
parseQueuedResponse = await z.request({
url: `https://api.mindee.net/v1/products/${apiOwner}/${apiName}/v${apiVersion}/documents/queue/${jobId}`,
method: 'GET',
redirect: "follow",
headers: headers,
});

retryCount++;
let parseQueuedJson = JSON.parse(parseQueuedResponse.content);

if (parseQueuedJson && parseQueuedJson?.job && parseQueuedJson.job?.status === "completed" || parseQueuedJson.job?.status === "failed") {
return parseQueuedResponse;
}
}
}
if (enqueueJson?.api_request?.error && Object.keys(enqueueJson.api_request.error).length > 0) {
throw new Error(JSON.stringify(enqueueJson.api_request.error));
} else {
throw new Error(`Could not enqueue file properly.`);
}
}
}
6 changes: 3 additions & 3 deletions creates/mindee_custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE for full license details.
*/

const defaults = require('./defaults');
const defaults = require("./defaults.js");

module.exports = {
operation: {
Expand All @@ -13,7 +13,7 @@ module.exports = {
key: 'api_url',
required: true,
label: 'Your API URL',
helpText: 'Go on the page of the concerned API, on platform.mindee.com and paste here the URL of the page',
helpText: 'Go on the page of the relevant API, on platform.mindee.com and paste here the URL of the page',
type:'string',
placeholder: 'Example: https://platform.mindee.com/account_name/api_name'
},
Expand Down Expand Up @@ -54,7 +54,7 @@ module.exports = {
],
"status": "success",
"status_code": 201,
"url": "http://api.mindee.net/v1/products/test/field_test/v1/predict"
"url": "https://api.mindee.net/v1/products/test/field_test/v1/predict"
},
"document": {
"id": "37067c22-4cea-40d3-9809-b9ca1bfedd33",
Expand Down
2 changes: 1 addition & 1 deletion creates/mindee_financial_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE for full license details.
*/

const defaults = require("./defaults");
const defaults = require("./defaults.js");

module.exports = {
operation: {
Expand Down
2 changes: 1 addition & 1 deletion creates/mindee_fr_bank_account_details.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE for full license details.
*/

const defaults = require("./defaults");
const defaults = require("./defaults.js")

module.exports = {
operation: {
Expand Down
124 changes: 124 additions & 0 deletions creates/mindee_generated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/** Copyright (C) 2021-2024, Mindee.
*
* This program is licensed under the MIT License.
* See LICENSE for full license details.
*/

const defaults = require("./defaults.js");
const {enqueueAndParse} = require("./async");

module.exports = {
operation: {
inputFields: [
{
key: 'api_url',
required: true,
label: 'Your API URL',
helpText: 'Go on the page of the relevant API, on platform.mindee.com and paste here the URL of the page',
type: 'string',
placeholder: 'Example: https://platform.mindee.com/account_name/api_name'
},
{
key: 'api_version',
required: false,
label: 'API Version (latest by default)',
helpText: 'Available in the live interface.',
type: 'string',
placeholder: 'Example: 1.6'
},
defaults.documentInputField,
],
perform: (z, bundle) => {
const apiVersion = bundle.inputData.api_version ? bundle.inputData.api_version : 1;
const splitUrl = bundle.inputData.api_url
.replace("https://platform.mindee.com/", "")
.replace("https://api.mindee.net/v1/products/", "")
.split('/');
const apiOwner = splitUrl[0];
const apiName = splitUrl[1];
const response = enqueueAndParse(z, bundle.inputData.document, apiOwner, apiName, apiVersion, defaults.postHeaders, 60);
return response.then((res) => JSON.parse(res.content));
},
sample: {
"api_request": {
"error": {},
"resources": [
"document",
"job"
],
"status": "success",
"status_code": 200,
"url": "https://api.mindee.net/v1/products/mindee/invoice_splitter_async/v1/documents/d88406ed-47bd-4db0-b3f3-145c8667a343"
},
"document": {
"id": "d88406ed-47bd-4db0-b3f3-145c8667a343",
"inference": {
"extras": {},
"finished_at": "2023-03-21T13:53:00.974000",
"is_rotation_applied": null,
"pages": [
{
"extras": {},
"id": 0,
"orientation": {
"value": null
},
"prediction": {}
},
{
"extras": {},
"id": 1,
"orientation": {
"value": null
},
"prediction": {}
}
],
"prediction": {
"invoice_page_groups": [
{
"confidence": 1.0,
"page_indexes": [
0
]
},
{
"confidence": 1.0,
"page_indexes": [
1
]
}
]
},
"processing_time": 4.266,
"product": {
"features": [
"invoice_page_groups"
],
"name": "Mindee/invoice_splitter",
"type": "standard",
"version": "1.0"
},
"started_at": "2023-03-21T13:52:56.326107+00:00"
},
"n_pages": 2,
"name": "2_invoices.pdf"
},
"job": {
"available_at": "2023-03-21T13:53:00.990339",
"id": "b6caf9e8-9bcc-4412-bcb7-f5b416678f0d",
"issued_at": "2023-03-21T13:52:56.326107",
"status": "completed",
"error": {}
}
},
},
key: 'Mindee_generated_api',
noun: 'Any async API',
display: {
label: 'DocTI Parsing',
description:
'Extract data from any document using any asynchronous API. E.g. Invoice Splitter, DocTI APIs.',
hidden: false
},
};
2 changes: 1 addition & 1 deletion creates/mindee_idcard_fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE for full license details.
*/

const defaults = require("./defaults");
const defaults = require("./defaults.js");

module.exports = {
operation: {
Expand Down
2 changes: 1 addition & 1 deletion creates/mindee_invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE for full license details.
*/

const defaults = require("./defaults");
const defaults = require("./defaults.js");

module.exports = {
operation: {
Expand Down
2 changes: 1 addition & 1 deletion creates/mindee_passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE for full license details.
*/

const defaults = require("./defaults");
const defaults = require("./defaults.js");

module.exports = {
operation: {
Expand Down
2 changes: 1 addition & 1 deletion creates/mindee_receipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE for full license details.
*/

const defaults = require("./defaults");
const defaults = require("./defaults.js");

module.exports = {
operation: {
Expand Down
2 changes: 1 addition & 1 deletion creates/mindee_us_bank_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE for full license details.
*/

const defaults = require("./defaults");
const defaults = require("./defaults.js");

module.exports = {
operation: {
Expand Down
2 changes: 1 addition & 1 deletion creates/mindee_us_driving_license.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* See LICENSE for full license details.
*/

const defaults = require("./defaults");
const defaults = require("./defaults.js");

module.exports = {
operation: {
Expand Down
32 changes: 20 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,35 @@
* See LICENSE for full license details.
*/

const authentication = require('./authentication');
const mindeeInvoice = require('./creates/mindee_invoice.js');
const mindeePassport = require('./creates/mindee_passport.js');
const mindeeReceipt = require('./creates/mindee_receipt.js');
const mindeeFrenchId = require('./creates/mindee_idcard_fr.js');
const mindeeCustomAPI = require('./creates/mindee_custom.js');
const mindeeFinancialDocumentAPI = require('./creates/mindee_financial_document.js');
const mindeeFrenchBankAccountDetails = require('./creates/mindee_fr_bank_account_details.js');
const mindeeUSBankCheck = require('./creates/mindee_us_bank_check.js');
const mindeeUSDrivingLicense = require('./creates/mindee_us_driving_license.js');
const authentication = require("./authentication.js");

const mindeeInvoice = require("./creates/mindee_invoice.js");
const mindeePassport = require("./creates/mindee_passport.js");
const mindeeReceipt = require("./creates/mindee_receipt.js");
const mindeeFrenchId = require("./creates/mindee_idcard_fr.js");
const mindeeCustomAPI = require("./creates/mindee_custom.js");
const mindeeGeneratedAPI = require("./creates/mindee_generated.js");
const mindeeFinancialDocumentAPI = require("./creates/mindee_financial_document.js");
const mindeeFrenchBankAccountDetails = require("./creates/mindee_fr_bank_account_details.js");
const mindeeUSBankCheck = require("./creates/mindee_us_bank_check.js");
const mindeeUSDrivingLicense = require("./creates/mindee_us_driving_license.js");
const fs = require("fs");
const zapierPlatform = require("zapier-platform-core");


const packageVersion = JSON.parse(fs.readFileSync('package.json', 'utf8'))['version'];

module.exports = {
version: require('./package.json').version,
platformVersion: require('zapier-platform-core').version,
version: packageVersion,
platformVersion: zapierPlatform.version,
authentication: authentication,
creates: {
[mindeeInvoice.key]: mindeeInvoice,
[mindeeReceipt.key]: mindeeReceipt,
[mindeePassport.key]: mindeePassport,
[mindeeFrenchId.key]: mindeeFrenchId,
[mindeeCustomAPI.key]: mindeeCustomAPI,
[mindeeGeneratedAPI.key]: mindeeGeneratedAPI,
[mindeeFinancialDocumentAPI.key]: mindeeFinancialDocumentAPI,
[mindeeFrenchBankAccountDetails.key]: mindeeFrenchBankAccountDetails,
[mindeeUSBankCheck.key]: mindeeUSBankCheck,
Expand Down
Loading

0 comments on commit 37c0228

Please sign in to comment.