in src/gatherers/docai.js [135:212]
run(source, gathererOptions) {
try {
let fieldKeyOnly = gathererOptions.fieldKeyOnly;
let keyRemapList = gathererOptions.keyRemapList;
let projectId = gathererOptions.projectId;
let processorId = gathererOptions.processorId;
let authorization = gathererOptions.authorization;
let documentType = source.documentType;
let contentBase64 = source.contentBase64;
let outputData = {};
assert(projectId, 'projectId is missing in gathererOptions');
assert(processorId, 'processorId is missing in gathererOptions');
assert(authorization, 'authorization is missing in gathererOptions');
assert(contentBase64, 'contentBase64 is missing in gathererOptions');
// Make API call to DocAI endpoint.
let requestOptions = {
'payload': {
'rawDocument': {
'mimeType': 'application/pdf',
'content': contentBase64,
}
},
'headers': {
'Authorization': authorization,
},
};
let url = `https://us-documentai.googleapis.com/v1/projects/${projectId}/locations/us/processors/${processorId}:process`;
let response = this.apiHandler.post(url, requestOptions);
if (response.statusCode !== 200) {
return {
status: Status.ERROR,
statusText: 'Error',
metadata: {},
error: this.getErrorMessage(response),
errorDetail: `Sent request to ${url}`,
errorResponse: response,
};
}
let responseJson = JSON.parse(response.body);
let entities = this.getDocumentEntities(responseJson);
if (fieldKeyOnly) {
outputData = [];
Object.keys(entities).forEach(key => {
outputData.push({
documentType: documentType,
key: key,
newKey: key,
sampleValue: entities[key].value,
});
});
} else {
outputData = entities;
if (keyRemapList) outputData = this.remapKeys(outputData, keyRemapList);
}
return {
status: Status.RETRIEVED,
statusText: 'Success',
metadata: {},
data: outputData,
}
} catch (e) {
// console.error(e);
return {
status: Status.ERROR,
statusText: 'Error',
error: e.message,
errorDetail: e,
}
}
}