devices.py revision 12dd813c417478dffb4bf66994eebbb125db7a27
1# Copyright 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Module contains a simple client lib to the devices RPC."""
6
7import json
8import logging
9import urllib2
10
11import common
12from fake_device_server.client_lib import common_client
13from fake_device_server import devices as s_devices
14
15
16class DevicesClient(common_client.CommonClient):
17    """Client library for devices method."""
18
19    def __init__(self, *args, **kwargs):
20        common_client.CommonClient.__init__(
21                self, s_devices.DEVICES_PATH, *args, **kwargs)
22
23
24    def get_device(self, device_id):
25        """Returns info about the given |device_id|.
26
27        @param device_id: valid device_id.
28        """
29        url_h = urllib2.urlopen(self.get_url([device_id]))
30        return json.loads(url_h.read())
31
32
33    def list_devices(self):
34        """Returns the list of the devices the server currently knows about."""
35        url_h = urllib2.urlopen(self.get_url())
36        return json.loads(url_h.read())
37
38
39    def create_device(self, system_name, device_kind, channel, **kwargs):
40        """Creates a device using the args.
41
42        @param system_name: name to give the system.
43        @param device_kind: type of device.
44        @param channel: supported communication channel.
45        @param kwargs: additional dictionary of args to put in config.
46        """
47        data = dict(systemName=system_name,
48                    deviceKind=device_kind,
49                    channel=channel,
50                    **kwargs)
51        request = urllib2.Request(self.get_url(), json.dumps(data),
52                                  {'Content-Type': 'application/json'})
53        url_h = urllib2.urlopen(request)
54        return json.loads(url_h.read())
55