Retrieving Documents
Assetic Document Retrieval Sample
The sample script below demonstrates how to retrieve a document from Assetic via the Assetic document API. For a more general overview of document retrieval, refer to this article on the Integrations section.
The Assetic Python SDK usually only returns the response body, and not headers. To support document download the Assetic Python SDK has an optional configuration to also return the "Content-Disposition" to allow the filename to be extracted.
"""
Example script to download a document (Assetic.DocumentGet.py)
For a given document id download the document
"""
import assetic
from assetic.rest import ApiException
import re
# Assetic SDK instance
asseticsdk = assetic.AsseticSDK("c:/users/you/assetic.ini", None, "Error")
filedir = "C:/temp/"
docid = "c31b7d80-9a53-4d5c-a3b2-14a9f39077ec" # document to get
# Assetic document API instance.
docapi = assetic.DocumentApi()
try:
# get with http info because filename is in response header
getfile = docapi.document_get_document_file_with_http_info(docid)
except ApiException as e:
asseticsdk.logger.error("Status {0}, Reason: {1} {2}"
.format(e.status, e.reason, e.body))
exit()
if getfile is not None:
# initially set filename to unknown
fullfilename = filedir + "unknownfilename"
# get document name from response. The response is a tuple.
if "Content-Disposition" in getfile[2]:
# extract document name from response.
if ("attachment" in getfile[2]["Content-Disposition"]
and "filename=" in getfile[2]["Content-Disposition"]):
# overwrite unknown filename if found in response
filename = getfile[2]["Content-Disposition"].split("filename=", 1)[1]
if '"' in filename or "'" in filename:
filename = filename[1:-1]
# check and replace any invalid characters in the filename
invalid_char_match = r'[^\w\-_\.]+'
replace_char = '_'
filename = re.sub(invalid_char_match, replace_char, filename)
fullfilename = filedir + filename
# get document data from response
data = getfile[0]
if type(data) is bytes:
# cater for different binary data
with open(fullfilename, "wb") as out_file:
out_file.write(getfile[0])
print("Created file: {0}".format(fullfilename))
elif type(data) is str:
# string data
with open(fullfilename, "w", newline="", encoding="utf-8") as out_file:
try:
out_file.write(getfile[0])
print("Created file: {0}".format(fullfilename))
except UnicodeEncodeError as ex:
print("Encoding error: ".format(str(ex)))
else:
print("File not created, data type unhandled: {0}".format(
str(type(data))))
How it works
Initiate the Assetic Python SDK
import assetic
asseticsdk = assetic.AsseticSDK("c:/users/you/assetic.ini",None,"Error")
Define the directory to save the document to, and the GUID of the document
filedir = "C:/temp/" docid = "c1ef6a41-aea4-e611-946c-06edd62964d7"
Create an instance of the document API.
docapi = assetic.DocumentApi()
docapi = assetic.DocumentApi()
Since the response header contains the document name, ensure the header details are included by using the python method document_get_document_file_with_http_info() rather than the simpler method document_get_document_file()
The API request is wrapped in a try block to catch any exceptions which are raised by the API. The error reason and response status can be written to the log as an error using "asseticsdk.logger"
try:
# get with http info because filename is in response header
getfile = docapi.document_get_document_file_with_http_info(docid)
except assetic.rest.ApiException as e:
asseticsdk.logger.error(
"Status {0}, Reason: {1} {2}".format(e.status,e.reason,e.body))
exit();
The document content and header is returned as an array.
First check it is not None. The header containing the file name the second value in the array.
if getfile is not None:
# get document name from response. The response is a tuple
if "Content-Disposition" in getfile[2]:
# get document name from response.
if "attachment" in getfile[2]["Content-Disposition"] \
and "filename=" in getfile[2]["Content-Disposition"]:
filename = getfile[2]["Content-Disposition"].split("filename=",1)[1]
if '"' in filename or "'" in filename:
filename = filename[1:-1]
fullfilename = filedir + filename
else:
fullfilename = filedir + "unknownfilename"
The document content is the first value of the array
##get document data from response array
data = getfile[0]
Depending on the data type of the response the data is saved as either a binary or text file
if type(data) == bytes:
# cater for different binary data
with open(fullfilename, "wb") as out_file:
out_file.write(getfile[0])
print("Created file: {0}".format(fullfilename))
elif type(data) == str:
# string data
with open(fullfilename, "w", newline="",encoding="utf-8") as out_file:
try:
out_file.write(getfile[0])
print("Created file: {0}".format(fullfilename))
except UnicodeEncodeError as ex:
print("Encoding error: ".format(str(ex)))
else:
print("File not created, data type unhandled: {0}".format(
str(type(data))))
