Below is an end-to-end example of submitting a request for various methods using Node.js programming language:
// Simple script to showcasing how to queue up and run multiple requests using the LEADTOOLS CloudServices.
// In order to run this script, the following changes will need to be added:
// 1) Place your Application ID in the user portion of the getRequestOptions method.
// 2) Place your Application Password in the password portion of the getRequestOptions method.
//
// The script will perform the following operations in order:
// 1)Perform an UploadFile request to the LEADTOOLS CloudServices. A successfully made request will return a unique identifier.
// We will use this identifier to queue up further methods.
// 2)We will queue up an ExtractText method using the identifier that was returned from the UploadFile method.
// 3)After we verify the ExtractText method was queued, we will then queue up a Conversion method to convert the file to TIFF.
// 4)Once the Conversion request has been queued, we will make a call to the Run method. This will mark the file as ready to be processed.
// **Note** Once a file has been marked for processing, or has finished processing, no further requests can be queued or run on that file.
// 5)We will then query the services using the GUID -- if the file is finished processing, the body will contain all the
// request data.
// 6)We will take the json data that was returned, parse it, and display all the information that was returned.
// This script makes use of the following NodeJS libraries:
// 1) Request - More information about this library can be found here: https://github.com/request/request
// 2) File System - More information about this library can be found here: https://nodejs.org/api/fs.html
const request = require('request');
//If uploading a file as multi-part content, we will need the file-system library installed.
//const fs = require('fs');
var servicesUrl = "https://azure.leadtools.com/api/";
//The first page in the file to mark for processing
var firstPage = 1;
//Sending a value of -1 will indicate to the services that the rest of the pages in the file should be processed.
var lastPage = -1;
//Enum corresponding to the output format for the file. For the purposes of this script, we will be converting to tif.
var fileFormat = 4;
//We will be uploading the file via a URl. Files can also be passed by adding a PostFile to the request. Only 1 file will be accepted per request.
//The services will use the following priority when determining what a request is trying to do GUID > URL > Request Body Content
var fileURL = 'http://demo.leadtools.com/images/cloud_samples/ocr1-4.tif';
var guid = '';
var uploadUrl = servicesUrl + "UploadFile?fileurl=" + fileURL;
request.post(getRequestOptions(uploadUrl), uploadCallback);
//If uploading a file as multi-part content:
//var uploadUrl = servicesUrl + "UploadFile";
//var req = request.post(getRequestOptions(uploadUrl), uploadCallback);
//var form = req.form();
//form.append('file', fs.createReadStream('path\to\inputFile'));
function uploadCallback(error, response, body) {
if (!error && response.statusCode === 200) {
guid = body;
console.log("Unique ID returned by the Services: " + guid);
checkVerification();
}
}
function checkVerification(){
var queryUrl = servicesUrl + "Query?id=" + guid;
request.post(getRequestOptions(queryUrl), async function (error, response, body) {
var results = JSON.parse(body);
if (!error && results['FileStatus'] !== 123) {
console.log("File finished processing with return code: " + response.statusCode);
if(results['FileStatus'] == 122){
var recognitionUrl = servicesUrl + 'Recognition/ExtractText?firstPage=' + firstPage + '&lastPage=' + lastPage + '&guid=' + guid;
request.post(getRequestOptions(recognitionUrl), recognitionCallback);
}else{
console.log("File failed verification with File Status: " + results['FileStatus']);
}
} else {
//The file has not yet finished processing.
await function () {
return new Promise(resolve => setTimeout(resolve, 5000)); //Sleep for 5 seconds before trying again
};
checkVerification(); //Call the method again.
}
});
}
function recognitionCallback(error, response, body) {
if (!error && response.statusCode === 200) {
console.log("ExtractText successfully queued");
var conversionUrl = servicesUrl + 'Conversion/Convert?firstPage=' + firstPage + '&lastPage=' + lastPage + '&guid=' + guid + '&format=' + fileFormat;
request.post(getRequestOptions(conversionUrl), conversionCallback);
} else {
console.log("ExtractText failed to queue with HTTP code: " + response.statusCode);
console.log(body);
}
}
function conversionCallback(error, response, body) {
if (!error && response.statusCode === 200) {
console.log("Conversion successfully queued");
var runUrl = servicesUrl + 'Run?id=' + guid;
request.post(getRequestOptions(runUrl), runCallback);
} else {
console.log("Conversion failed to queue with HTTP code: " + response.statusCode);
console.log(body);
}
}
function runCallback(error, response, body) {
if (!error && response.statusCode === 200) {
console.log("File has been successfully marked to run");
queryServices();
} else {
console.log("Run failed with HTTP code: " + response.statusCode);
console.log(body);
}
}
function queryServices() {
//Function to query the status of a request. If the request has not yet finished, this function will recursively call itself until the file has finished.
var queryUrl = servicesUrl + "Query?id=" + guid;
request.post(getRequestOptions(queryUrl), async function (error, response, body) {
var results = JSON.parse(body);
if (!error && results['FileStatus'] !== 100) {
console.log("File finished processing with return code: " + response.statusCode);
if (results['FileStatus'] !== 200) {
return;
}
console.log("Results: \n");
parseJson(results['RequestData']);
} else {
//The file has not yet finished processing.
await function () {
return new Promise(resolve => setTimeout(resolve, 5000)); //Sleep for 5 seconds before trying again
};
queryServices(guid); //Call the method again.
}
});
}
function parseJson(jsonObject) {
//Function to decode the JSON object that was returned by the LEADTOOLS CloudServices.
for (let i = 0; i < jsonObject.length; i++) {
var currentRequest = jsonObject[i];
console.log("Service Type: " + currentRequest['ServiceType']);
if (currentRequest['ServiceType'] === 'Recognition' && currentRequest['RecognitionType'] === 'ExtractText') {
console.log("Recognition Method: " + currentRequest['RecognitionType']);
console.log("Data:" + currentRequest['data']);
} else if (currentRequest['ServiceType'] === 'Conversion') {
console.log("Urls: ");
currentRequest['urls'].forEach(url => {
console.log(url);
});
} else {
console.log("Unanticipated service type");
}
}
}
function getRequestOptions(url) {
//Function to generate and return HTTP request options.
var requestOptions = {
url: url,
//If uploading a file as multi-part content, remove the Content-Length header.
headers: {
'Content-Length': 0
},
auth: {
user: "Replace with Application ID",
password: "Replace with Application Password"
}
};
return requestOptions;
}