• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 registration RPC."""
6
7import json
8import logging
9from six.moves import urllib
10
11import common
12from fake_device_server.client_lib import common_client
13from fake_device_server import commands as s_commands
14
15
16class CommandsClient(common_client.CommonClient):
17    """Client library for commands method."""
18
19    def __init__(self, *args, **kwargs):
20        common_client.CommonClient.__init__(
21                self, s_commands.COMMANDS_PATH, *args, **kwargs)
22
23
24    def get_command(self, command_id):
25        """Returns info about the given command using |command_id|.
26
27        @param command_id: valid id for a command.
28        """
29        request = urllib.request.Request(self.get_url([command_id]),
30                                         headers=self.add_auth_headers())
31        url_h = urllib.request.urlopen(request)
32        return json.loads(url_h.read())
33
34
35    def list_commands(self, device_id):
36        """Returns the list of commands for the given |device_id|.
37
38        @param command_id: valid id for a command.
39        """
40        request = urllib.request.Request(
41            self.get_url(params={'deviceId':device_id}),
42            headers=self.add_auth_headers())
43        url_h = urllib.request.urlopen(request)
44        return json.loads(url_h.read())
45
46
47    def update_command(self, command_id, data, replace=False):
48        """Updates the command with |data|.
49
50        @param command_id: id of the command to update.
51        @param data: data to update command with.
52        @param replace: If True, replace all data with the given data using the
53                PUT operation.
54        """
55        if not data:
56            return
57
58        headers = self.add_auth_headers({'Content-Type': 'application/json'})
59        request = urllib.request.Request(
60            self.get_url([command_id]), json.dumps(data),
61            headers=headers)
62        if replace:
63            request.get_method = lambda: 'PUT'
64        else:
65            request.get_method = lambda: 'PATCH'
66
67        url_h = urllib.request.urlopen(request)
68        return json.loads(url_h.read())
69
70
71    def create_command(self, device_id, data):
72        """Creates a new command.
73
74        @device_id: ID of device to send command to.
75        @param data: command.
76        """
77        headers = self.add_auth_headers({'Content-Type': 'application/json'})
78        data['deviceId'] = device_id
79        request = urllib.request.Request(self.get_url(),
80            json.dumps(data),
81            headers=headers)
82        url_h = urllib.request.urlopen(request)
83        return json.loads(url_h.read())
84