Python example
# Python3 example
import os
import requests
import json
import sys
import http.server
base_url = 'https://sandbox.spark.ai'
image_path = '/tmp/image.png'
# Example auth token. Replace with a provided token
auth_token = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBfdXNlcl9pZCI6IjY1Y2ExM2U0LTM3OTEtNDFlMC05ZmE0LTY5NTFmYzE5MDdlNyIsImlhdCI6MTYwNzg5MTIyNCwiZXhwIjoxNjM5NDI3MjI0LCJpc3MiOiJzcGFyay5haSIsInN1YiI6IjIuMCJ9.j5kKufsSXQWGfZcVL-FD7G1IVy-9dmEAbnbCR8YqOVcFXawcPxf_TOm-CzN-OSJE3Z9A6-BA3m3VOJl_FWyphcQmGL8nINoBszoMJCnwaHZahe6DIR_mx2SsRaQRrnDPPZ3vYUmKaDAUVZU3Woo2yvCF5XWQBQnHNLCW4RFVhCq-bWJptYz74ylWJPKSk-mith3qQF0L70hoTSvsZjz6rJfoYKrSdU1miG-3cdYqcFJZbFedSJ750XShLa3mNxPtbWZ608OHlroSuI0ZGTH_iqi3lRWW3vyO_7c1VxY-XTVeuVptMTWLgwI69OI_7Ww5xcdPd5JyR0KDCIow07Yh9vtod0PqCd5VfQYj6HNb4xq0boOfeBS4DjYbMVLNOsGUdNZVOysrKozJcoOYg-Xxqub-upgHj3Gbw5Tiw_z_I9jQ62-2ptGNh9GTeAjCQUzSYrwW1nY9Ihi104osIr6zVQyrQ6hgTHcmg51iFIt3rN11bHdNCdTo4SIyVGN5VRPF-9HEqZAojAqooSljBBWA9nNkM8-4zBNqjKQEpp-1-uAbe8Ft80XJ_bcP0pahJHqJpav8F-dA-9an6PG-75ZFuNWZKNdu2ms0ZzbQbTcmw4ujInjuvDcaYsZ3pL3zfI0tDIF2YUiQuZUa93D8G8xyecbF8PBogLYWJbMfZIg6-jQ'
instructions = 'Confirm there are no obstructions in the path of this robot'
# Example webhook URL. Replace or omit.
webhook_local_server_port = 3388
webhook_url = 'http://77bb049f3629.ngrok.io'
headers = {
'Authorization': 'Bearer ' + auth_token
}
# Upload an image to the SparkAI Image Server.
image_resource_url = '/v1/image'
binary_image = open(image_path, 'rb')
image_name = os.path.basename(image_path)
files= { 'image_file': (image_name, binary_image, 'multipart/form-data') }
r = requests.post(base_url + image_resource_url, headers=headers, files=files, data={})
if r.status_code == 200:
image_url = json.loads(r.content)['signedUrl']
print('Uploaded image to SparkAI image server', image_url)
else:
resp = json.loads(r.content)
print(resp['statusCode'], resp['message'])
sys.exit()
# Create a new engagement request
engagement_data = {
'content_location': image_url,
'webhook_url': webhook_url,
'instructions': instructions
}
# An alternate option
# engagement_data = {
# 'content_location': image_url,
# 'program_name': 'your_program',
# }
engagement_resource_url = '/v1/engagement'
r = requests.post(base_url + engagement_resource_url, headers=headers, json=engagement_data)
resp = json.loads(r.content)
if r.status_code == 200:
print('Created a new engagement request, with token', resp['token'])
else:
print(resp['statusCode'], resp['message'])
sys.exit()
# Check status of the engagement
r = requests.get(base_url + engagement_resource_url + '/' + resp['token'], headers=headers)
print('Checking engagement status')
print(json.dumps(json.loads(r.content), indent=2))
# Setup a simple HTTP server to receive webhook URL.
# This isnt necessary, but helps demonstrate the use of webhooks.
# This local server can be tunnelled to the internet by using a tool such as ngrok (www.ngrok.io)
class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
print('Received response from SparkAI service')
response = json.loads(body)
print(json.dumps(response, indent=2))
sys.exit()
# Create a local server that accepts the resolutions from SparkAI as a webhook
httpd = http.server.HTTPServer(('', webhook_local_server_port), SimpleHTTPRequestHandler)
httpd.serve_forever()
Updated over 1 year ago