Download Document
To download a document attached to an entity you need to know an internal ID of this document. The internal ID is stored in the in the value
field of the document property. To get this internal ID, use the Get Object’s Document.
References to documents are stored as Properties, so you can also get the internal ID with the Get Object Properties request.
|
Use the document’s internal ID to construct a document URL with the following template:
http://<saymon-hostname>/node-resources/docs/uid/<internal-ID>/pdf/1.pdf
Download a document specified by ID
This example shows how to download a document by its id
. To do this, it gets the document’s internal ID with the Get Object’s Document request and then uses the urllib.request library to download the file.
import requests
import urllib.request
import json
saymon_hostname = <saymon_hostname>
api_token = <api_token>
object_id = <object_id>
document_id = <document_id>
# Get the document's properties
url = "http://" + saymon_hostname + "/node/api/objects/" + object_id + "/docs/" + document_id
params = {
"api-token": api_token
}
response = requests.request("GET", url, params=params)
# Get document's internal ID and its name
doc = json.loads(response.text)
pdf_id = doc['value']
pdf_name = doc['name']
# Download the PDF file
urllib.request.urlretrieve("http://"+ saymon_hostname +"/node-resources/docs/uid/" + pdf_id + "/pdf/1.pdf?api-token=" + api_token, pdf_name)
Download all documents of a specified object
This example shows how to download all documents of a specified object. First, it gets all properties of an object with the Get Object Properties request. Then, it iterates over all properties and uses the urllib.request library to download the files, specified in the properties with the Type ID = 7 (Uploaded Document).
import requests
import urllib.request
import json
saymon_hostname = <saymon_hostname>
api_token = <api_token>
object_id = <object_id>
# Get all properties of an object
url = "http://" + saymon_hostname + "/node/api/objects/" + object_id + "/props"
params = {
"api-token": api_token
}
response = requests.request("GET", url, params=params)
props = json.loads(response.text)
# Filter properties of type PDF document (type_id = 7)
for prop in props:
if prop['type_id'] == 7:
# Get document's internal ID and its name
pdf_id = prop['value']
pdf_name = prop['name']
# Download the PDF file
urllib.request.urlretrieve("http://"+ saymon_hostname +"/node-resources/docs/uid/" + pdf_id + "/pdf/1.pdf?api-token=" + api_token, pdf_name)