Python is my language of choice when 'poking around' to see what an API can generate based on different inputs. This helps me to come up to speed and start leveraging its full potential.

 

With Hub it is the same. You can also use a language that offers an interactive approach to play with its API.

In this example below, I connect to Hub via its REST API by simply using the httplib and json modules from Python 3.4:

 

try:
    from http.client import HTTPConnection  # Python 3
except ImportError:
    from httplib import HTTPConnection      # Python 2
 
import json
 
class PLTDevice:
    def __init__(self, spokes, uid):
        self.AttachURL = '/Spokes/DeviceServices/Attach?uid=' + uid
        self.attached = False
        self.session = None
        self.uid = uid
        self.spokes = spokes
    def attach(self):
        self.spokes.conn.request('GET', self.AttachURL)
        r = self.spokes.conn.getresponse()
        if r.status == 200:
            response = r.read().decode('utf-8')
            print(response)
            response = json.loads(response)
        if response['isError'] == False:
            self.attached = True
            self.session = response['Result']
    def release(self):
        self.ReleaseURL = '/Spokes/DeviceServices/Release?sess=' + self.session
        self.spokes.conn.request('GET', self.ReleaseURL)
        r = self.spokes.conn.getresponse()
        if r.status == 200:
            response = r.read().decode('utf-8')
            response = json.loads(response)
            if response['isError'] == False:
                self.attached = False
                self.session = None
    def get_events(self, queue=0):
        eventsURL = '/Spokes/DeviceServices/Events?sess='+self.session+'&queue='+str(queue)
        self.spokes.conn.request('GET', eventsURL)
        r = self.spokes.conn.getresponse()
        if r.status == 200: 
            response = r.read().decode('utf-8')
            response = json.loads(response)
            print(response)
 
class Spokes:
    def __init__(self):
        self.host = '127.0.0.1'
        self.port = '32017'
        self.conn = HTTPConnection(self.host + ":" + self.port)
        self.DeviceInfoURL = '/Spokes/DeviceServices/Info'
        self.deviceInfo = None
    def get_device_info(self):
        self.conn.request('GET', self.DeviceInfoURL)
        r = self.conn.getresponse()
        if r.status == 200:
            response = r.read()
            str_response = response.decode('utf-8')
            response = json.loads(str_response)
            if response['isError'] == True:
                print(response['Err']['Description'])
            else:
                self.deviceInfo = response
        return self.deviceInfo
This is by no means a full solution, it is simply a quick and dirty way to invoke the most basic APIs from the REST interface (I've added close to no error checks ). It does show how one could interact with Hub in an interactive way to learn the device events and how to use them. Now, after loading the code above and switching to the interactive mode:
  • Get device info example
>>> s=Spokes()
>>> print(s)
<__main__.Spokes object at 0x03474690>
>>> print(s.deviceInfo)
None
>>> dl=s.get_device_info()
>>> print(dl)
{'Type_Name': 'DeviceInfo', 'isError': False, 'Result': {'InternalName': 'Poseidon', 'BaseSerialNumber': '', 'Uid': 'edb3858128625c8def41a478992eb4cd', 'RemoteFirmwareVersion': '19.41', 'SerialNumber': '', 'BaseFirmwareVersion': '19.85', 'DevicePath': '\\\\?\\hid#vid_047f&pid_ac01&mi_03&col03#7&130933e3&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}', 'ManufacturerName': 'Plantronics', 'IsAttached': True, 'VendorId': 1151, 'BluetoothFirmwareVersion': '02.05.0a, 0d', 'ProductId': 44033, 'USBVersionNumber': '165', 'HeadsetSerialNumber': '', 'ProductName': 'Plantronics Savi 7xx'}, 'Description': 'Active Device Info', 'Type': 4}
>>> print(dl['Description'])
Active Device Info
  • Get device events (undocked and docked) example
>>> s=Spokes()
>>> d=PLTDevice(s,'0123456789')
>>> d.attach()
{"Description":"Session ID","Result":"8172b6a4d7b28660b1d49c091ca90e23","Type":7,"Type_Name":"SessionHash","isError":false}

>>> d.get_events()
{'Type_Name': 'DeviceEventArray', 'isError': False, 'Description': 'Device Events', 'Result': '', 'Type': 6}
>>> d.get_events()
{'Type_Name': 'DeviceEventArray', 'isError': False, 'Description': 'Device Events', 'Result': [{'Event_Log_Type_Name': 'HeadsetStateChange', 'Order': 0, 'Age': 2786, 'Event_Id': 11, 'Event_Log_Type_Id': 2, 'Event_Name': 'Undocked'}], 'Type': 6}
>>> d.get_events()
{'Type_Name': 'DeviceEventArray', 'isError': False, 'Description': 'Device Events', 'Result': [{'Event_Log_Type_Name': 'HeadsetStateChange', 'Order': 1, 'Age': 1632, 'Event_Id': 10, 'Event_Log_Type_Id': 2, 'Event_Name': 'Docked'}], 'Type': 6}
>>> d.release()
>>> print(d.attached)
False


Note that since I didn't continue implementing the code for the events and other APIs, the event list is displayed as it was returned. I simply parse the JSON result and print it in get_events(); I specially like Python for its flexibility on accessing the values on its data structures as demostrated above in PLTDevice.__getattr__. ProductName is not defined as a member in the class, but still gets accessed as one. Its content comes from the response we received from Hub.

>>> print(dl['Result']['ProductName'])
Plantronics Savi 7xx

From here you could start building your app to actually monitor the events queue and act on them, or keep on looking around in interactive mode to see what else you can do with the device and APIs.This approach usually works well for me on troubleshooting REST APIs and I hope it helps you as well.Just another way of doing it.

Cheers,
Ricardo


Download Link: RESTPythonSample.zip