Advertisement
AshoreAppCTO

New Version Demonstration

Jan 24th, 2024 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.38 KB | Software | 0 0
  1. import os
  2. import requests
  3. import json
  4. from dotenv import load_dotenv
  5.  
  6. load_dotenv()
  7. AccessToken = os.getenv('ACCESSTOKEN')
  8.  
  9.  
  10. # Function to get the latest proof version ID
  11. def get_proof(proof_id):
  12.     url = f"https://api.ashoreapp.com/proof/{proof_id}/0?loadAllVersions=false"
  13.     headers = {
  14.         'accept': 'application/json',
  15.         'accesstoken': AccessToken,
  16.         'content-type': 'application/json'
  17.     }
  18.     response = requests.get(url, headers=headers)
  19.     if response.status_code == 200:
  20.         data = response.json()
  21.         return data
  22.     else:
  23.         raise Exception("Failed to get proof version ID: Status Code {}".format(response.status_code))
  24.  
  25.  
  26. # Function to upload a new version of the document
  27. def create_new_proof_version(proof_id, current_proof_stage_id=0):
  28.     url = "https://api.ashoreapp.com/proof/new-version"
  29.     headers = {
  30.         'accept': 'application/json',
  31.         'accesstoken': AccessToken,
  32.         'content-type': 'application/json'
  33.     }
  34.     payload = {
  35.         "senderUserId": 2,
  36.         "proofId": proof_id,
  37.         "proofVersionFileIdsToPromoteToTheNextVersion": [],
  38.         "proofName": "test",
  39.         "subjectLine": "You Have A Proof To Review",
  40.         "message": "<p>Please Review This Proof At Your Convenience</p>",
  41.         "templateId": 0,
  42.         "sendToWorkflowStageId": current_proof_stage_id,
  43.         "sendOptions": 0  # SendToAll
  44.     }
  45.     # ATTN: sendToWorkflowStageId defaults to current stage (stages are attached to proof versions leaving this blank
  46.     # will put the file on the new version) so you don't need to get the proof object first in order to upload files
  47.     # to to the current stage
  48.     # ATTN: SendOptions enumeration explains the behavior of sendOptions field:
  49.     # 0 - SendToAll: Send to all approvers and reviewers.
  50.     # 1 - SendToUnApprovedWithReviewers: Send only to unapproved approvers with reviewers.
  51.     # 2 - SendToNone: Do not send to any approvers or reviewers.
  52.     # 3 - Manual: Manually specify approvers and reviewers to send to.
  53.     # 1 - SendToUnApprovedWithoutReviewers: Send only to unapproved approvers without reviewers.
  54.     response = requests.post(url, headers=headers, data=json.dumps(payload))
  55.     if response.status_code == 200:
  56.         print("New proof version created successfully.")
  57.         return response.json()
  58.     else:
  59.         raise Exception("Failed to create a new proof version: Status Code {}".format(response.status_code))
  60.  
  61.  
  62. # Function to upload a file to the specified proof version
  63. def upload_file(proof_id, proof_version_id, file_path):
  64.     url = f"https://api.ashoreapp.com/proof/{proof_id}/proof-version/{proof_version_id}/file"
  65.     headers = {
  66.         'accept': 'application/json',
  67.         'accesstoken': AccessToken
  68.     }
  69.     with open(file_path, 'rb') as file:
  70.         files = {'file': (os.path.basename(file_path), file, 'image/png')}
  71.         response = requests.put(url, headers=headers, files=files)
  72.         if response.status_code == 200:
  73.             print("File uploaded successfully.")
  74.         else:
  75.             raise Exception("Failed to upload file: Status Code {}".format(response.status_code))
  76.  
  77.  
  78. # Function to upload a file from a URL to the specified proof version
  79. def upload_file_from_url(proof_id, proof_version_id, file_name, file_url):
  80.     url = f"https://api.ashoreapp.com/proof/{proof_id}/proof-version/{proof_version_id}/file-from-url"
  81.     headers = {
  82.         'accept': 'application/json',
  83.         'accesstoken': AccessToken,
  84.         'content-type': 'application/json'
  85.     }
  86.     payload = {
  87.         'fileName': file_name,
  88.         'fileUrl': file_url
  89.     }
  90.     response = requests.post(url, headers=headers, data=json.dumps(payload))
  91.     if response.status_code == 200:
  92.         print("File from URL uploaded successfully.")
  93.     else:
  94.         raise Exception("Failed to upload file from URL: Status Code {}".format(response.status_code))
  95.  
  96.  
  97. # Main execution
  98. if __name__ == "__main__":
  99.     try:
  100.         proofId = 197656
  101.         proof = get_proof(proofId)
  102.         proofVersion = create_new_proof_version(proofId, proof['currentWorkflowStageId'])
  103.         upload_file(197656, proofVersion['id'], './ashore-logo.png')
  104.         upload_file_from_url(197656, proofVersion['id'], 'ashore-logo.png',
  105.                              'https://ashoreapp.com/wp-content/uploads/2023/07/ashore-logo.png')
  106.  
  107.     except Exception as e:
  108.         print(str(e))
  109.  
Tags: Ashoreapp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement