Simple Examples for REST calls

Posted on
Tue Sep 20, 2016 8:47 am
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Simple Examples for REST calls

I am attempting to make some RESTful API calls in order to build scripts for manipulating OSRAM Lightify bulbs. I am a python newbie so looking for simple examples demonstrating the following:
1) making REST calls - POST/GET/DELETE/etc
2) obtaining a security auth token and re-using for subsequent RESTful calls

Thanks in advance.. I starting poking around in the Hue Plugin but I am a bit too new on Python (I've done it in java).

Any pointers appreciated

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Posted on
Tue Sep 20, 2016 5:13 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Simple Examples for REST calls

How are you running these Python scripts?

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Sep 20, 2016 6:33 pm
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Simple Examples for REST calls

You should definitely still answer Jay's question, but here's a sample of one way to do it.

Code: Select all
#! /usr/bin/env python2.6
# -*- coding: utf-8 -*-

import requests
from requests.auth import HTTPDigestAuth

url = 'http://server_url_goes_here:8176/devices/Living%20Room%20%2D%20Vase?toggle=1&_method=put'

try:
    requests.get(url, auth=HTTPDigestAuth('username', 'password'))
   
except KeyError, error:
    indigo.server.log(u"restful_api_example.py KeyError: %s" % error, isError=True)

except TypeError, error:
    indigo.server.log(u"restful_api_example.py TypeError: %s" % error, isError=True)

except ValueError, error:
    indigo.server.log(u"restful_api_example.py ValueError: %s" % error, isError=True)

except Exception, error:
    indigo.server.log(u"restful_api_example.py Error: %s" % error, isError=True)

Note the part of the URL that includes a device name. You'll need to format yours depending on your device's name. You'll also need to add your server IP, username and password.

Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Sep 20, 2016 8:23 pm
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: Simple Examples for REST calls

Thanks and apologies for lack of clarity in my post.. I guess I haven't gotten that far in figuring out "How" I am gonna run it.. I was playing around with the "Scripting Shell" for now and running by command line. If I can get something working I am open to suggestions :)

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Posted on
Tue Sep 20, 2016 8:26 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Simple Examples for REST calls

I asked because you don't need to use the Indigo RESTful API if you're running scripts from Indigo. There's a much more direct object model that gives you direct access to Indigo "things" as Python objects - no need for RESTful API calls (or any kind of connectivity stuff).

If you're talking about communicating with the Lightify stuff from an Indigo Python script, then that's different. You'd need to identify what kind of API they provide and how it's authentication works. Then you can figure out how to do it in Python.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Sep 20, 2016 8:48 pm
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: Simple Examples for REST calls

Gotcha.. my bad again... My goal is to use the Indigo Python object model to call these OSRAM Lightify REST APIs. So I will give the Python libs mentioned below by @DaveL17. Thanks!

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Posted on
Tue Sep 20, 2016 9:07 pm
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: Simple Examples for REST calls

BTW.. any tip on how to install the python packages? When using the "Open Scripting Shell" tool to run the line 'import requests' I get the error:

File "<console>", line 1, in <module>
ImportError: No module named requests

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Posted on
Tue Sep 20, 2016 9:16 pm
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Simple Examples for REST calls

rbdubz3 wrote:
Gotcha.. my bad again... My goal is to use the Indigo Python object model to call these OSRAM Lightify REST APIs. So I will give the Python libs mentioned below by @DaveL17. Thanks!

Obviously, my example is meant to show how you can control Indigo from Python (outside Indigo.) You would also need to adapt the script to the OSRAM API, and as Jay point out, the authentication method may be different.

Re: your more recent post, I should've mentioned that you'd possibly need to install the requests module. Methods (and opinions) vary, but I've had success with this approach: Open a terminal window and enter the following command:
Code: Select all
sudo easy_install pip
and follow the prompts. After that's complete and presuming it's successful, then follow up with the following:

Code: Select all
sudo pip install requests

This *should* do it.

Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Sep 20, 2016 9:23 pm
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: Simple Examples for REST calls

yeah thanks Dave.. i did find some info on an older post of yours here - viewtopic.php?f=181&t=14572&p=110126&hilit=installing+modules#p100734 .. Hopefully this version of the module is ok

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Posted on
Tue Sep 20, 2016 11:49 pm
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: Simple Examples for REST calls

Ok.. thanks for the head start Dave and Jay.. I had some success with the Python scripts calling OSRAM REST APIs.. Able to do the following actions:
- authenticate on Osram and get a token for the Hub
- perform 'get' calls to turn groups of devices On/Off

Next Steps:
1) json parsing - I need to figure out how to use Python to parse the json responses in order to get the auth token for subsequent get calls.. The group On/Off commands issues above I just copied it by hand into the scripting tool
2) plug the script into Indigo.. I am planning to use my Insteon motion sensor to turn the Osram Lights On/Off based on whether motion is detected in the room (using Occupancy Mode)

Any tips appreciated

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Posted on
Wed Sep 21, 2016 3:47 am
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Simple Examples for REST calls

Glad to hear that you got it working. Here's a basic example that pulls the JSON from a URL. You'll need to adapt it to work with the OSRAM return data.
Code: Select all
import simplejson
import urllib2

f = urllib2.urlopen(url)
simplejson_string = f.read()
f.close()
parsed_simplejson = simplejson.loads(simplejson_string)

value = parsed_simplejson['title'])

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Sep 21, 2016 7:49 am
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: Simple Examples for REST calls

Thanks again.. I have my crude example running now.. Hooked it in by adding a trigger action to 'Execute Script' .. Try not to laugh.. but here it is.. Nothing fancy, zero error handling/etc .. Below is the one to turn 'ON' the bulbs .. I also have one working to flip them off

Code: Select all
################

import requests
import simplejson

authurl = 'https://us.lightify-api.org/lightify/services/session'
authpayload = {'username': 'userlarry', 'password': 'pwddaryl', 'serialNumber': 'someserno'}
authheaders = {'Content-Type': 'application/json'}

r = requests.post(authurl, headers=authheaders, data=simplejson.dumps(authpayload))

# parse the token
parsed_authresp = simplejson.loads(r.text)
securityToken = parsed_authresp['securityToken']

onurl='https://us.lightify-api.org/lightify/services/group/set?idx=3&onoff=1'

actionheaders={'Content-Type': 'application/json', 'authorization': securityToken}

r = requests.get(onurl, headers=actionheaders)

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Posted on
Wed Sep 21, 2016 10:04 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Simple Examples for REST calls

Nice! You're on your way to building a plugin.

One comment: you should use json rather than simplejson - the former is built-in to Python starting with 2.6 (which is what we're using) and the latter is a library we include with Indigo - but we won't be including it in the next version of Indigo since it's already built-in to Python now. The API is the same, just change your import line to import json

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Sep 21, 2016 11:41 am
rbdubz3 offline
User avatar
Posts: 224
Joined: Sep 18, 2016
Location: San Diego, CA

Re: Simple Examples for REST calls

Great.. thanks for the tip Jay.. I will swap out the json libs. Not sure about an official plugin yet but we'll see

Next tasks are as follows:
1) Move the Indigo server/setup and Lightify ON/OFF stuff to my home iMac to check stability (right now it is running on my work macbook).
2) Program an Insteon Keypad button to manipulate Lightify Bulbs in my media room - ON/OFF/COLOR for starters .. followed by dimming
3) Maybe work on holding the state of things such as color temp, brightness, etc

I automate because I am lazy :D - My Plugins: https://forums.indigodomo.com/viewforum.php?f=309

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests