Home | History | Annotate | Download | only in client_lib
      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 
      7 import json
      8 import logging
      9 import urllib2
     10 
     11 import common
     12 from fake_device_server.client_lib import common_client
     13 from fake_device_server import devices as s_devices
     14 
     15 
     16 class 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         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, channel, **kwargs):
     44         """Creates a device using the args.
     45 
     46         @param system_name: name to give the system.
     47         @param channel: supported communication channel.
     48         @param kwargs: additional dictionary of args to put in config.
     49         """
     50         data = dict(name=system_name,
     51                     channel=channel,
     52                     **kwargs)
     53         headers = self.add_auth_headers({'Content-Type': 'application/json'})
     54         request = urllib2.Request(self.get_url(), json.dumps(data),
     55                                   headers=headers)
     56         url_h = urllib2.urlopen(request)
     57         return json.loads(url_h.read())
     58