Here we present a minimal example Python script showing the workflow of how to make a complete request to our API endpoint and download back the result.
This script is for demostration purpose only. It lacks some cirtical elements for a production-ready script like error handling.
import timeimport osimport requests# Define the base URL, the API key and Pathsbase_url ="https://vurse.deemos.dev/api/v2"api_key ="your api key"image_path ="/your/image/path/robot.jpg"result_path ="/your/result/path"# Define the headers for the requestsheaders ={"Authorization":f"Bearer {api_key}","Content-Type":"application/json"}# Function to submit a task to the rodin endpointdefsubmit_task(): url =f"{base_url}/rodin"# Read the image filewithopen(image_path,'rb')as image_file: image_data = image_file.read()# Prepare the multipart form data files ={'images':(os.path.basename(image_path), image_data,'image/jpeg')}# Set the tier to Rodin Regular data ={'tier':'Regular'}# Prepare the headers. headers ={'Authorization':f'Bearer {api_key}',}# Note that we are not sending the data as JSON, but as form data.# This is because we are sending a file as well. response = requests.post(url,files=files,data=data,headers=headers)return response.json()# Function to check the status of a taskdefcheck_status(subscription_key): url =f"{base_url}/status" data ={"subscription_key": subscription_key} response = requests.post(url,headers=headers,json=data)return response.json()# Function to download the results of a taskdefdownload_results(task_uuid): url =f"{base_url}/download" data ={"task_uuid": task_uuid} response = requests.post(url,headers=headers,json=data)return response.json()# Submit the task and get the task UUIDtask_response =submit_task()task_uuid = task_response['uuid']subscription_key = task_response['jobs']['subscription_key']# Poll the status endpoint every 5 seconds until the task is donestatus =[]whilelen(status)==0ornotall(s['status']in['Done','Failed']for s in status): time.sleep(5) status_response =check_status(subscription_key) status = status_response['jobs']for s in status:print(f"job {s['uuid']}: {s['status']}")# Download the results once the task is donedownload_response =download_results(task_uuid)download_items = download_response['list']# Print the download URLs and download them locally.for item in download_items:print(f"File Name: {item['name']}, URL: {item['url']}") dest_fname = os.path.join(result_path, item['name']) os.makedirs(os.path.dirname(dest_fname),exist_ok=True)withopen(dest_fname,'wb')as f: response = requests.get(item['url']) f.write(response.content)print(f"Downloaded {dest_fname}")