|
| 1 | + |
| 2 | +# Copyright 2019 Objectif Libre |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | +# |
| 16 | +from cloudkittyclient import exc |
| 17 | +from cloudkittyclient.v1.client import rating |
| 18 | + |
| 19 | + |
| 20 | +class RatingManager(rating.RatingManager): |
| 21 | + """Class used to handle /v2/rating/modules endpoint""" |
| 22 | + |
| 23 | + url = '/v2/rating/modules' |
| 24 | + |
| 25 | + def get_module(self, **kwargs): |
| 26 | + """Returns the given module. |
| 27 | +
|
| 28 | + If module_id is not specified, returns the list of loaded modules. |
| 29 | +
|
| 30 | + :param module_id: ID of the module on which you want information. |
| 31 | + :type module_id: str |
| 32 | + """ |
| 33 | + module_id = kwargs.get('module_id', None) |
| 34 | + if module_id is not None: |
| 35 | + url = "{}/{}".format(self.url, module_id) |
| 36 | + else: |
| 37 | + url = self.url |
| 38 | + return self.api_client.get(url).json() |
| 39 | + |
| 40 | + def update_module(self, **kwargs): |
| 41 | + """Update the given module. |
| 42 | +
|
| 43 | + :param module_id: Id of the module to update. |
| 44 | + :type module_id: str |
| 45 | + :param enabled: Set to True to enable the module, False to disable it. |
| 46 | + :type enabled: bool |
| 47 | + :param priority: New priority of the module. |
| 48 | + :type priority: int |
| 49 | + """ |
| 50 | + if not kwargs.get('module_id', None): |
| 51 | + raise exc.ArgumentRequired("'module_id' argument is required.") |
| 52 | + mutable_fields = ['enabled', 'priority'] |
| 53 | + changes = {} |
| 54 | + for key, value in kwargs.items(): |
| 55 | + if value is not None and key in mutable_fields: |
| 56 | + changes[key] = value |
| 57 | + self.api_client.put("{}/{}".format(self.url, kwargs['module_id']), |
| 58 | + json=changes) |
| 59 | + return self.get_module(**kwargs) |
0 commit comments