From 450099e9347df956b8623b603e1cccf0c291b0fc Mon Sep 17 00:00:00 2001 From: mjordan Date: Mon, 3 Mar 2014 19:57:32 -0600 Subject: [PATCH] deviceStates: Add support for device states in ari-py This patch adds support for the ARI deviceStates resource. This includes a sample-api resource as well as a very basic unit test for using PUT to create/set the state of a device. --- ari/client.py | 9 +++ ari/model.py | 18 ++++- ari_test/client_test.py | 9 +++ sample-api/deviceStates.json | 151 +++++++++++++++++++++++++++++++++++ sample-api/resources.json | 4 + 5 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 sample-api/deviceStates.json diff --git a/ari/client.py b/ari/client.py index 2738f6e..dabbf79 100644 --- a/ari/client.py +++ b/ari/client.py @@ -237,6 +237,15 @@ def on_endpoint_event(self, event_type, fn): """ return self.on_object_event(event_type, fn, Endpoint, 'Endpoint') + def on_device_state_event(self, event_type, fn): + """Register callback for DeviceState related events + + :param event_type: String name of the event to register for. + :param fn: Callback function + :type fn: (DeviceState, dict) -> None or (list[DeviceState], dict) -> None + """ + return self.on_object_event(event_type, fn, DeviceState, 'DeviceState') + def on_sound_event(self, event_type, fn): """Register callback for Sound related events diff --git a/ari/model.py b/ari/model.py index eb8367f..31ad29f 100644 --- a/ari/model.py +++ b/ari/model.py @@ -288,6 +288,21 @@ def __init__(self, client, endpoint_json): client.on_endpoint_event) +class DeviceState(BaseObject): + """First class object API. + + :param client: ARI client. + :type client: client.Client + :param endpoint_json: Instance data + """ + id_generator = DefaultObjectIdGenerator('deviceName', id_field='name') + + def __init__(self, client, device_state_json): + super(DeviceState, self).__init__( + client, client.swagger.devicestates, device_state_json, + client.on_device_state_event) + + class Sound(BaseObject): """First class object API. @@ -341,5 +356,6 @@ def promote(client, resp, operation_json): 'Endpoint': Endpoint, 'Playback': Playback, 'LiveRecording': LiveRecording, - 'StoredRecording': StoredRecording + 'StoredRecording': StoredRecording, + 'DeviceState': DeviceState, } diff --git a/ari_test/client_test.py b/ari_test/client_test.py index 80b3c0c..e4591a8 100644 --- a/ari_test/client_test.py +++ b/ari_test/client_test.py @@ -123,6 +123,15 @@ def test_stored_recording(self): recordingName='test-recording') recording.deleteStored() + def test_device_state(self): + self.serve(PUT, 'deviceStates', 'foobar', + body='{"name": "foobar", "state": "BUSY"}') + device_state = self.uut.deviceStates.update( + deviceName='foobar', + deviceState='BUSY') + self.assertEqual('foobar', device_state['name']) + self.assertEqual('BUSY', device_state['state']) + def setUp(self): super(ClientTest, self).setUp() self.uut = ari.connect('http://ari.py/', 'test', 'test') diff --git a/sample-api/deviceStates.json b/sample-api/deviceStates.json new file mode 100644 index 0000000..eca3a47 --- /dev/null +++ b/sample-api/deviceStates.json @@ -0,0 +1,151 @@ +{ + "_copyright": "Copyright (C) 2012 - 2013, Digium, Inc.", + "_author": "Kevin Harwell ", + "_svn_revision": "$Revision: 407402 $", + "apiVersion": "0.0.0-test", + "swaggerVersion": "1.1", + "basePath": "http://ari.py/ari", + "resourcePath": "/api-docs/deviceStates.{format}", + "apis": [ + { + "path": "/deviceStates", + "description": "Device states", + "operations": [ + { + "httpMethod": "GET", + "summary": "List all ARI controlled device states.", + "nickname": "list", + "responseClass": "List[DeviceState]" + } + ] + }, + { + "path": "/deviceStates/{deviceName}", + "description": "Device state", + "operations": [ + { + "httpMethod": "GET", + "summary": "Retrieve the current state of a device.", + "nickname": "get", + "responseClass": "DeviceState", + "parameters": [ + { + "name": "deviceName", + "description": "Name of the device", + "paramType": "path", + "required": true, + "allowMultiple": false, + "dataType": "string" + } + ] + }, + { + "httpMethod": "PUT", + "summary": "Change the state of a device controlled by ARI. (Note - implicitly creates the device state).", + "nickname": "update", + "responseClass": "void", + "parameters": [ + { + "name": "deviceName", + "description": "Name of the device", + "paramType": "path", + "required": true, + "allowMultiple": false, + "dataType": "string" + }, + { + "name": "deviceState", + "description": "Device state value", + "paramType": "query", + "required": true, + "allowMultiple": false, + "dataType": "string", + "allowableValues": { + "valueType": "LIST", + "values": [ + "NOT_INUSE", + "INUSE", + "BUSY", + "INVALID", + "UNAVAILABLE", + "RINGING", + "RINGINUSE", + "ONHOLD" + ] + } + + } + ], + "errorResponses": [ + { + "code": 404, + "reason": "Device name is missing" + }, + { + "code": 409, + "reason": "Uncontrolled device specified" + } + ] + }, + { + "httpMethod": "DELETE", + "summary": "Destroy a device-state controlled by ARI.", + "nickname": "delete", + "responseClass": "void", + "parameters": [ + { + "name": "deviceName", + "description": "Name of the device", + "paramType": "path", + "required": true, + "allowMultiple": false, + "dataType": "string" + } + ], + "errorResponses": [ + { + "code": 404, + "reason": "Device name is missing" + }, + { + "code": 409, + "reason": "Uncontrolled device specified" + } + ] + } + ] + } + ], + "models": { + "DeviceState": { + "id": "DeviceState", + "description": "Represents the state of a device.", + "properties": { + "name": { + "type": "string", + "description": "Name of the device.", + "required": true + }, + "state": { + "type": "string", + "description": "Device's state", + "required": true, + "allowableValues": { + "valueType": "LIST", + "values": [ + "UNKNOWN", + "NOT_INUSE", + "INUSE", + "BUSY", + "INVALID", + "UNAVAILABLE", + "RINGING", + "RINGINUSE", + "ONHOLD" + ] + } + } + } + } + } +} diff --git a/sample-api/resources.json b/sample-api/resources.json index 5e9e750..ecf93ad 100644 --- a/sample-api/resources.json +++ b/sample-api/resources.json @@ -33,6 +33,10 @@ "path": "/api-docs/playbacks.{format}", "description": "Playback control resources" }, + { + "path": "/api-docs/deviceStates.{format}", + "description": "Device state resources" + }, { "path": "/api-docs/events.{format}", "description": "WebSocket resource"