Skip to content

Developers: API Examples

Brandon Jank edited this page Nov 22, 2017 · 1 revision

From PR #33 Example CURL request:

curl -X POST https://smartsettia-backburn.c9users.io/api/image -H "Accept: application/json" \
-H "Authorization: Bearer u0tQGeyuGdikOOFWhfDfwxbR2Z7rTN9Hc5hZN1JHsg4uUfkTi8UUu5nh0XLm" \
-F "uuid=9dbc0776-9b44-11e7-abc4-cec278b6b50a" \
-F "token=u0tQGeyuGdikOOFWhfDfwxbR2Z7rTN9Hc5hZN1JHsg4uUfkTi8UUu5nh0XLm" \
-F "image=@/tmp/somepic.jpg"

Example Python request:

url = 'https://smartsettia.com/api/image'
data = {'uuid': ('', '4dbc0776-9b44-11e7-abc4-cec278b6b50a'), 'token': ('', 'u0tQGeyuGdikOOFWhfDfwxbR2Z7rTN9Hc5hZN1JHsg4uUfkTi8UUu5nh0XLm'), 'image': open('/tmp/somepic.jpg','rb')}
headers = {'Accept': 'application/json', 'Authorization': 'Bearer u0tQGeyuGdikOOFWhfDfwxbR2Z7rTN9Hc5hZN1JHsg4uUfkTi8UUu5nh0XLm'}
response = requests.post(url, files=data, headers=headers)

Complete Python example:

#!/usr/bin/python

# requires: sudo apt install fswebcam

import requests
import json
from subprocess import call

UUID = '9dbc0776-9b44-11e7-abc4-cec278b6b50a'
CHALLENGE = 'temppass'
TOKEN = 'u0tQGeyuGdikOOFWhfDfwxbR2Z7rTN9Hc5hZN1JHsg4uUfkTi8UUu5nh0XLm'
#DOMAIN = 'https://smartsettia.com/'
DOMAIN = 'https://smartsettia-backburn.c9users.io/'
IMAGEPATH = '/tmp/camera.jpg'

def post_api_register():
	"This registers with the smartsettia API"
	url = DOMAIN+'api/register'
	print url
	data = {"uuid": UUID, "challenge": CHALLENGE}
	print data
	headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
	response = requests.post(url, data=json.dumps(data), headers=headers)
	print response
	return
	
def post_api_update():
	"This sends a status update to the smartsettia API"
	url = DOMAIN+'api/update'
	data = {"uuid": UUID, "token": TOKEN, "version": "0.1.1", "hostname": "device.local", "ip": "192.168.1.213", "mac_address": "1122334455667788", "time": "2000-12-31 23:59:59", "cover_status": "closed", "error_msg": "", "limitsw_open": "0", "limitsw_closed": "1", "light_in": "0", "light_out": "100", "cpu_temp": "30", "temperature": "28", "humidity": "34"}
	headers = {'Content-type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Bearer '+TOKEN}
	response = requests.post(url, data=json.dumps(data), headers=headers)
	print response
	return

def post_api_image():
	"This sends the webcam image to the smartsettia API"
	webcam_capture()
	url = DOMAIN+'api/image'
	data = {'uuid': ('', UUID), 'token': ('', TOKEN), 'image': open(IMAGEPATH,'rb')}
	headers = {'Accept': 'application/json', 'Authorization': 'Bearer '+TOKEN}
	response = requests.post(url, files=data, headers=headers)
	print "status: ", response.status_code
	print "text: ", response.text
	return
	
def webcam_capture():
	call(["fswebcam", "-d","/dev/video0", "-r", "1280x720", "/tmp/camera.jpg"])
	return

post_api_image()
Clone this wiki locally