2022-08-01 13:47:55 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-10-21 14:32:40 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IBinaryKeyData,
|
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
|
|
|
IDataObject,
|
2022-02-05 13:55:43 -08:00
|
|
|
INodeCredentialTestResult,
|
2021-10-21 14:32:40 -07:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
NodeOperationError,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
awsApiRequestREST,
|
|
|
|
IExpenseDocument,
|
|
|
|
simplify,
|
2022-06-13 12:41:18 -07:00
|
|
|
validateCredentials,
|
2021-10-21 14:32:40 -07:00
|
|
|
} from './GenericFunctions';
|
|
|
|
|
|
|
|
export class AwsTextract implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'AWS Textract',
|
|
|
|
name: 'awsTextract',
|
|
|
|
icon: 'file:textract.svg',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"]}}',
|
|
|
|
description: 'Sends data to Amazon Textract',
|
|
|
|
defaults: {
|
|
|
|
name: 'AWS Textract',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'aws',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-10-21 14:32:40 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Analyze Receipt or Invoice',
|
|
|
|
value: 'analyzeExpense',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'analyzeExpense',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Input Data Field Name',
|
|
|
|
name: 'binaryPropertyName',
|
|
|
|
type: 'string',
|
|
|
|
default: 'data',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['analyzeExpense'],
|
2021-10-21 14:32:40 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
required: true,
|
2022-08-01 13:47:55 -07:00
|
|
|
description:
|
|
|
|
'The name of the input field containing the binary file data to be uploaded. Supported file types: PNG, JPEG.',
|
2021-10-21 14:32:40 -07:00
|
|
|
},
|
|
|
|
{
|
2022-05-20 14:47:24 -07:00
|
|
|
displayName: 'Simplify',
|
2021-10-21 14:32:40 -07:00
|
|
|
name: 'simple',
|
|
|
|
type: 'boolean',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['analyzeExpense'],
|
2021-10-21 14:32:40 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: true,
|
2022-08-01 13:47:55 -07:00
|
|
|
description:
|
|
|
|
'Whether to return a simplified version of the response instead of the raw data',
|
2021-10-21 14:32:40 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
credentialTest: {
|
2022-08-01 13:47:55 -07:00
|
|
|
async awsTextractApiCredentialTest(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
2021-10-21 14:32:40 -07:00
|
|
|
try {
|
2022-08-01 13:47:55 -07:00
|
|
|
await validateCredentials.call(
|
|
|
|
this,
|
|
|
|
credential.data as ICredentialDataDecryptedObject,
|
|
|
|
'sts',
|
|
|
|
);
|
2021-10-21 14:32:40 -07:00
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: 'The security token included in the request is invalid',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Connection successful!',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-10-21 14:32:40 -07:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
|
|
|
//https://docs.aws.amazon.com/textract/latest/dg/API_AnalyzeExpense.html
|
|
|
|
if (operation === 'analyzeExpense') {
|
|
|
|
const binaryProperty = this.getNodeParameter('binaryPropertyName', i) as string;
|
|
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
|
|
|
|
|
|
if (items[i].binary === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No binary data exists on item!', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2021-10-21 14:32:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((items[i].binary as IBinaryKeyData)[binaryProperty] === undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`No binary data property "${binaryProperty}" does not exists on item!`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-10-21 14:32:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const binaryPropertyData = (items[i].binary as IBinaryKeyData)[binaryProperty];
|
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
Document: {
|
|
|
|
Bytes: binaryPropertyData.data,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const action = 'Textract.AnalyzeExpense';
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = (await awsApiRequestREST.call(
|
|
|
|
this,
|
|
|
|
'textract',
|
|
|
|
'POST',
|
|
|
|
'',
|
|
|
|
JSON.stringify(body),
|
|
|
|
{ 'x-amz-target': action, 'Content-Type': 'application/x-amz-json-1.1' },
|
|
|
|
)) as IExpenseDocument;
|
2021-10-21 14:32:40 -07:00
|
|
|
if (simple) {
|
|
|
|
responseData = simplify(responseData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else {
|
|
|
|
returnData.push(responseData as unknown as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|