Start a Workflow and Attach a File¶
We will start a workflow from an existing template and attach a local file.
What you need before starting:
- workflow template and it’s template_id
- placeholder file inside the workflow and it’s file_id
- username/password for logging in
On A high level you have to do 4 API calls for this:
- Login and get a session_id (See also: Login)
- Create the workflow (See also: Create)
- Attach/replace the file (See also: Add File)
- Start the workflow (See also: Start)
1. Login¶
- POST https://api.cloudplan.biz/api/user/login
- Headers:
- Content-Type: application/json
Payload (JSON):
{
"email":"youremail@provider.de",
"pw":"yourpassword"
}
Reply:
{
"session_id": "..." #needed for the next calls
}
2. Create the Workflow¶
- POST https://api.cloudplan.biz/api/workflow/create
- Headers:
- Content-Type: application/json
- session_id: id from /user/login
POST PAYLOAD (JSON):
{
"cp_id":"..." #your workflow template id
}
Reply:
{
"data": {
"cp_id": "..." #this is your workflow id for the next call
}
}
3. Attach the File¶
- POST https://api.cloudplan.biz/api/workflow/add_file
- Headers:
- Content-Type: file/pdf
- session_id: id from /user/login
- cp_id: new wf id from step 2 (cp_id)
- file_name: the name of the file
- file_cp_id_to_replace: id of the placeholder file in the workflow
- POST PAYLOAD : binary filedata
4. Start the Workflow¶
- POST https://api.cloudplan.biz/api/workflow/start
- Headers:
- Content-Type: application/json
- session_id: id from /user/login
POST PAYLOAD (JSON):
{
"cp_id":"..." #new wf id from step 2 (cp_id)
}
Example Python Script¶
This is a fully functional example script you can download here
.
import os
import sys
import json
import argparse
import urllib3
def main(argv):
base_url = "https://uni.cloudplan.biz/"
username = ""
password = ""
workflow_template_id = ""
workflow_file_id = ""
# process command line args
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", help="the file to upload", default="testpdf.pdf")
parser.add_argument("-u", "--user", help="username for logging in", default=username)
parser.add_argument("-p", "--password", help="password for logging in", default=password)
parser.add_argument("-t", "--template_id", help="the workflow template to use", default=workflow_template_id, type=str)
parser.add_argument("-i", "--file_id", help="id of the file inside the wf that should be replaced", default=workflow_file_id, type=str)
args = parser.parse_args()
print(args)
#GET ARGS
filepath = args.file
username = args.user
password = args.password
workflow_template_id = args.template_id
workflow_file_id = args.file_id
if not filepath or not username or not password or not workflow_template_id:
print("ERROR: MISSING OPTIONS")
parser.print_help()
return
print("FILE: " + filepath)
print("USER: " + username)
print("PW: " + "".rjust(len(password), '*'))
print("TEMPLATE: " + workflow_template_id)
print("FILE ID: " + workflow_file_id)
#PREPARATION: GET THE FILE
filedata = None
if not os.path.isfile(filepath):
print("ERROR: File not found at " + filepath)
return
with open(filepath, 'rb+') as pdf_file:
filedata = pdf_file.read()
filename = os.path.basename(filepath.replace("\\", "/"))
#0. USER LOGIN
http = urllib3.PoolManager(cert_reqs = 'CERT_NONE')
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
request_data = {
"email": username,
"pw": password
}
r = http.request(
"POST",
base_url + "api/user/login",
body=json.dumps(request_data).encode('utf-8'),
headers={"Content-Type": "application/json"})
if r.status != 200:
print("ERROR REPLY: [/api/user/login]" + r.data.decode('utf-8'))
print("ERROR STATUS: " + str(r.status))
return
reply = json.loads(r.data.decode('utf-8'))
session_id= reply.get("session_id", "")
#1. CREATE NEW WORKFLOW FROM TEMPLATE
request_data = {"cp_id": workflow_template_id}
r = http.request(
"POST",
base_url + "api/workflow/create",
body=json.dumps(request_data).encode('utf-8'),
headers={"Content-Type": "application/json", "session_id": session_id})
if r.status != 200:
print("ERROR REPLY: [/api/workflow/create] " + r.data.decode('utf-8'))
print("ERROR STATUS: " + str(r.status))
return
reply = json.loads(r.data.decode('utf-8'))
wf_id= reply.get("data", {}).get("cp_id", "")
#2. ADD FILE TO WORKFLOW
headers = {
"Content-Type": "file/pdf",
"session_id": session_id,
"cp_id": wf_id,
"file_name": filename,
"file_cp_id_to_replace": workflow_file_id
}
r = http.request(
"POST",
base_url + "api/workflow/add_file",
body=filedata,
headers=headers)
if r.status != 200:
print("ERROR REPLY: [/api/workflow/add_file] " + r.data.decode('utf-8'))
print("ERROR STATUS: " + str(r.status))
return
#3. START THE WF
request_data = {"cp_id": wf_id}
encoded_data = json.dumps(request_data).encode('utf-8')
r = http.request(
"POST",
base_url + "api/workflow/start",
body=encoded_data,
headers={"Content-Type": "application/json",
"session_id": session_id})
if r.status != 200:
print("ERROR REPLY: [/api/workflow/start] " + r.data.decode('utf-8'))
print("ERROR STATUS: " + str(r.status))
return
print("SUCCESS")
if __name__ == '__main__':
main(sys.argv[1:])