Below is an end-to-end example of submitting a request for various methods using Python 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 appId variable.
# 2) Place your Application Password in the password variable
#
# 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 Requests Python library.
# More information about this library can be found here: http://docs.python-requests.org/en/master/
import requests
import sys
import time
servicesUrl = "https://azure.leadtools.com/api/"
# The application ID.
appId = "Replace with Application ID"
# The application password.
password = "Replace with Application Password"
# The first page in the file to mark for processing
firstPage = 1
# Sending a value of -1 will indicate to the services that the rest of the pages in the file should be processed.
lastPage = -1
# Enum corresponding to the output format for the file. We will be converting a file to tif.
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
fileURL = 'http://demo.leadtools.com/images/cloud_samples/ocr1-4.tif'
baseUploadUrl = '{}UploadFile?fileurl={}'
formattedUploadUrl = baseUploadUrl.format(servicesUrl, fileURL)
request = requests.post(formattedUploadUrl, auth=(appId, password))
# If uploading a file alongside the HTTP request
#baseUploadUrl ='{}UploadFile'
#formattedUploadUrl = baseUploadUrl.format(servicesUrl)
#file = {'file' : open('path/to/file', 'rb')}
#request = requests.post(
# formattedRecognitionUrl, auth=(appId, password), files = file)
if request.status_code != 200:
print("Error sending the conversion request")
print(request.text)
sys.exit()
# The File has been uploaded successfully. Now that we have the GUID, we can use it to queue up requests for the file
guid = request.text
print("Unique ID returned by the services: " + guid + "\n")
baseQueryUrl = '{}Query?id={}'
formattedQueryUrl = baseQueryUrl.format(servicesUrl, guid)
while True: # Poll the services to determine if the request has finished processing
request = requests.post(formattedQueryUrl, auth=(appId, password))
returnedData = request.json()
if returnedData['FileStatus'] != 123:
break
time.sleep(5)
baseRecognitionUrl = '{}Recognition/ExtractText?firstPage={}&lastPage={}&guid={}'
formattedRecognitionUrl = baseRecognitionUrl.format(
servicesUrl, firstPage, lastPage, guid)
request = requests.post(formattedRecognitionUrl, auth=(appId, password))
if request.status_code != 200:
print("Error sending the conversion request")
print(request.text)
sys.exit()
print("ExtractText queued successfully")
baseConversionUrl = '{}Conversion/Convert?firstPage={}&lastPage={}&format={}&guid={}'
formattedConversionUrl = baseConversionUrl.format(
servicesUrl, firstPage, lastPage, fileFormat, guid)
request = requests.post(formattedConversionUrl, auth=(appId, password))
if request.status_code != 200:
print("Error sending the conversion request")
print(request.text)
sys.exit()
print("Conversion queued successfully")
baseRunUrl = '{}Run?id={}'
formattedRunUrl = baseRunUrl.format(servicesUrl, guid)
request = requests.post(formattedRunUrl, auth=(appId, password))
if request.status_code != 200:
print("Error sending the conversion request")
print(request.text)
sys.exit()
print("File has successfully been marked to run")
while True: # Poll the services to determine if the request has finished processing
request = requests.post(formattedQueryUrl, auth=(appId, password))
returnedData = request.json()
if returnedData['FileStatus'] != 100:
break
time.sleep(5)
print("File finished processing with file status: " +
str(returnedData['FileStatus']))
if returnedData['FileStatus'] != 200:
sys.exit()
try:
print("Results:")
returnedJson = returnedData['RequestData']
for requestObject in returnedJson:
print("Service Type: " + requestObject['ServiceType'])
if requestObject['ServiceType'] == 'Conversion':
print("Returned URLS:")
for url in requestObject['urls']:
print(url)
elif requestObject['ServiceType'] == 'Recognition' and requestObject['RecognitionType'] == 'ExtractText':
print("Extract Text URL: " + requestObject['data'])
sys.exit()
except Exception as e:
print("Failed to Parse JSON")
print(str(e))