|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Webex Teams Adaptive Card Component base class. |
| 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 | + |
| 28 | +class AdaptiveCardComponent: |
| 29 | + """Base class for all Adaptive Card components. |
| 30 | +
|
| 31 | + Each component should inherit from this class and specify which of its |
| 32 | + properties fall into the following two categories: |
| 33 | +
|
| 34 | + * Simple properties are basic types (int, float, str, etc.). |
| 35 | +
|
| 36 | + * Serializable properties are properties that can themselves be serialized. |
| 37 | + This includes lists of items (i.e. the 'body' field of the adaptive card) |
| 38 | + or single objects that also inherit from Serializable |
| 39 | + """ |
| 40 | + def __init__(self, serializable_properties, simple_properties): |
| 41 | + """Initialize a serializable object. |
| 42 | +
|
| 43 | + Args: |
| 44 | + serializable_properties(list): List of all serializable properties |
| 45 | + simple_properties(list): List of all simple properties. |
| 46 | + """ |
| 47 | + self.serializable_properties = serializable_properties |
| 48 | + self.simple_properties = simple_properties |
| 49 | + |
| 50 | + def to_dict(self): |
| 51 | + """Serialize the component into a Python dictionary. |
| 52 | +
|
| 53 | + The to_dict() method recursively serializes the object's data into |
| 54 | + a Python dictionary. |
| 55 | +
|
| 56 | + Returns: |
| 57 | + dict: Dictionary representation of this component. |
| 58 | + """ |
| 59 | + serialized_data = {} |
| 60 | + |
| 61 | + # Serialize simple properties |
| 62 | + for property_name in self.simple_properties: |
| 63 | + property_value = getattr(self, property_name, None) |
| 64 | + |
| 65 | + if property_value is not None: |
| 66 | + serialized_data[property_name] = str(property_value) |
| 67 | + |
| 68 | + # Recursively serialize sub-components |
| 69 | + for property_name in self.serializable_properties: |
| 70 | + property_value = getattr(self, property_name, None) |
| 71 | + |
| 72 | + if property_value is not None: |
| 73 | + if isinstance(property_value, list): |
| 74 | + serialized_data[property_name] = [ |
| 75 | + item.to_dict() for item in property_value |
| 76 | + ] |
| 77 | + else: |
| 78 | + serialized_data[property_name] = property_value.to_dict() |
| 79 | + |
| 80 | + return serialized_data |
| 81 | + |
| 82 | + def to_json(self, **kwargs): |
| 83 | + """Serialize the component into JSON text. |
| 84 | +
|
| 85 | + Any keyword arguments provided are passed through the Python JSON |
| 86 | + encoder. |
| 87 | + """ |
| 88 | + return json.dumps(self.to_dict(), **kwargs) |
0 commit comments