diff --git a/creates/async.js b/creates/async.js index ef37837..1b3fc78 100644 --- a/creates/async.js +++ b/creates/async.js @@ -1,6 +1,6 @@ const defaults = require("./defaults"); module.exports = { - enqueueAndParse: async (z, inputDocument, apiOwner, apiName, apiVersion, headers, maxRetries=60) => { + 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', @@ -12,9 +12,9 @@ module.exports = { const enqueueJson = JSON.parse(enqueueResponse.content); - if (enqueueJson?.api_request?.job?.id) { - const jobId = enqueueJson.api_request.job.id; - let retryCount = 0; + 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)); @@ -28,7 +28,7 @@ module.exports = { retryCount++; let parseQueuedJson = JSON.parse(parseQueuedResponse.content); - if (parseQueuedJson && parseQueuedJson?.job && parseQueuedJson.job === "completed" || parseQueuedJson.job === "failed") { + if (parseQueuedJson && parseQueuedJson?.job && parseQueuedJson.job?.status === "completed" || parseQueuedJson.job?.status === "failed") { return parseQueuedResponse; } } @@ -36,7 +36,7 @@ module.exports = { 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."); + throw new Error(`Could not enqueue file properly.`); } } } \ No newline at end of file diff --git a/creates/mindee_generated.js b/creates/mindee_generated.js index 9ef7586..2da49a7 100644 --- a/creates/mindee_generated.js +++ b/creates/mindee_generated.js @@ -36,8 +36,8 @@ module.exports = { .split('/'); const apiOwner = splitUrl[0]; const apiName = splitUrl[1]; - const response = enqueueAndParse(z, bundle.inputData.document, apiOwner, apiName, apiVersion, defaults.postHeaders, bundle.inputData.requestSettingsData.max_async_retries); - return promise.then((response) => JSON.parse(response.content)); + const response = enqueueAndParse(z, bundle.inputData.document, apiOwner, apiName, apiVersion, defaults.postHeaders, 60); + return response.then((res) => JSON.parse(res.content)); }, sample: { "api_request": { diff --git a/test/mindee.js b/test/mindee.js index 3779ca3..8093711 100644 --- a/test/mindee.js +++ b/test/mindee.js @@ -17,9 +17,6 @@ const bundle = { oauth_token: process.env.OAUTH_TOKEN, oauth_token_secret: process.env.OAUTH_TOKEN_SECRET, }, - requestSettingsData: { - max_async_retries: 60, - }, inputData: {}, }; zapier.tools.env.inject();