|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Webex Teams Access-Tokens API wrapper. |
| 3 | +
|
| 4 | +Copyright (c) 2016-2019 Cisco and/or its affiliates. |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +""" |
| 24 | + |
| 25 | +import json |
| 26 | + |
| 27 | +class Serializable: |
| 28 | + """Parent class for all components of adaptive cards. |
| 29 | +
|
| 30 | + Each component should inherit from this class and then specify, from |
| 31 | + its properties, which fall into the following two categories: |
| 32 | +
|
| 33 | + * Simple properties are text properties like "type" or "id" |
| 34 | + * Serializable properties are properties that can themselfes be serilized. |
| 35 | + This includes lists of items (i.e. the 'body' field of the adaptive card) or |
| 36 | + single objects that also inherit from Serializable |
| 37 | + """ |
| 38 | + def __init__(self, serializable_properties, simple_properties): |
| 39 | + """Creates a serializable object. |
| 40 | +
|
| 41 | + See class docstring for an explanation what the different types of |
| 42 | + properties are. |
| 43 | +
|
| 44 | + Args: |
| 45 | + serializable_properties(list): List of all serializable properties |
| 46 | + simple_properties(list): List of all simple properties. |
| 47 | + """ |
| 48 | + self.serializable_properties = serializable_properties |
| 49 | + self.simple_properties = simple_properties |
| 50 | + |
| 51 | + def to_json(self, pretty=False): |
| 52 | + """Create json from a serializable component |
| 53 | +
|
| 54 | + This function is used to render the json from a component. While all |
| 55 | + components do support this operation it is mainly used on the |
| 56 | + AdaptiveCard to generate the json for the attachment. |
| 57 | +
|
| 58 | + Args: |
| 59 | + pretty(boolean): If true, the returned json will be sorted by keys |
| 60 | + and indented with 4 spaces to make it more human-readable |
| 61 | +
|
| 62 | + Returns: |
| 63 | + A Json representation of this component |
| 64 | + """ |
| 65 | + ret = None |
| 66 | + if pretty: |
| 67 | + ret = json.dumps(self.to_dict(), indent=4, sort_keys=True) |
| 68 | + else: |
| 69 | + ret = json.dumps(self.to_dict()) |
| 70 | + |
| 71 | + return ret |
| 72 | + |
| 73 | + def to_dict(self): |
| 74 | + """Export a dictionary representation of this card/component by |
| 75 | + parsing all simple and serializable properties. |
| 76 | +
|
| 77 | + A simple_component is a single-text property of the exported card |
| 78 | + (i.e. {'version': "1.2"}) while a serializable property is another |
| 79 | + subcomponent that also implements a to_dict() method. |
| 80 | +
|
| 81 | + The to_dict() method is used to recursively create a dict representation |
| 82 | + of the adaptive card. This dictionary representation can then be |
| 83 | + converted into json for usage with the API. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + dict: Dictionary representation of this component. |
| 87 | + """ |
| 88 | + export = {} |
| 89 | + |
| 90 | + # Export simple properties (i.e. properties that are only single text) |
| 91 | + for sp in self.simple_properties: |
| 92 | + o = getattr(self, sp, None) |
| 93 | + |
| 94 | + if o is not None: |
| 95 | + export[sp] = str(o) |
| 96 | + |
| 97 | + # Export all complex properties by calling its respective serialization |
| 98 | + for cp in self.serializable_properties: |
| 99 | + o = getattr(self, cp, None) |
| 100 | + |
| 101 | + if o is not None: |
| 102 | + # Check if it is a list or a single component |
| 103 | + l = [] |
| 104 | + if isinstance(o, list): |
| 105 | + for i in o: |
| 106 | + l.append(i.to_dict()) |
| 107 | + else: |
| 108 | + l.append(o.to_dict()) |
| 109 | + export[cp] = l |
| 110 | + |
| 111 | + return export |
0 commit comments