-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add support for generated APIs (#63)
- Loading branch information
1 parent
136d295
commit 37c0228
Showing
16 changed files
with
1,824 additions
and
10,017 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.