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