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 base class for patching or updating a resource.""" 6 7from cherrypy import tools 8 9import common 10from fake_device_server import common_util 11from fake_device_server import server_errors 12 13 14class ResourceMethod(object): 15 """A base class for methods that expose a simple PATCH/PUT mechanism.""" 16 17 def __init__(self, resource): 18 """ 19 @param resource: A resource delegate for storing devices. 20 """ 21 self.resource = resource 22 23 24 @tools.json_out() 25 def PATCH(self, *args, **kwargs): 26 """Updates the given resource with the incoming json blob. 27 28 Format of this call is: 29 PATCH .../resource_id 30 31 Caller must define a json blob to patch the resource with. 32 33 Raises: 34 server_errors.HTTPError if the resource doesn't exist. 35 """ 36 id, api_key, _ = common_util.parse_common_args(args, kwargs) 37 if not id: 38 server_errors.HTTPError(400, 'Missing id for operation') 39 40 data = common_util.parse_serialized_json() 41 return self.resource.update_data_val(id, api_key, data_in=data) 42 43 44 @tools.json_out() 45 def PUT(self, *args, **kwargs): 46 """Replaces the given resource with the incoming json blob. 47 48 Format of this call is: 49 PUT .../resource_id 50 51 Caller must define a json blob to patch the resource with. 52 53 Raises: 54 server_errors.HTTPError if the resource doesn't exist. 55 """ 56 id, api_key, _ = common_util.parse_common_args(args, kwargs) 57 if not id: 58 server_errors.HTTPError(400, 'Missing id for operation') 59 60 data = common_util.parse_serialized_json() 61 return self.resource.update_data_val( 62 id, api_key, data_in=data, update=False) 63