Skip to content

Commit 7cfc5bb

Browse files
committed
Added type checks to cards.py
1 parent fe0b769 commit 7cfc5bb

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

webexteamssdk/cards/card.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
"""
2424

2525
from .abstract_components import Serializable
26+
from .actions import OpenUrl, ShowCard, Submit
27+
28+
from .utils import check_type
2629

2730
class AdaptiveCard(Serializable):
2831
"""AdaptiveCard class that represents a adaptive card python object.
@@ -34,7 +37,6 @@ class AdaptiveCard(Serializable):
3437
def __init__(self, body=None,
3538
actions=None,
3639
selectAction=None,
37-
style=None,
3840
fallbackText=None,
3941
lang=None):
4042
"""Creates a new adaptive card object.
@@ -52,20 +54,25 @@ def __init__(self, body=None,
5254
used for localization of date/time functions
5355
5456
"""
55-
super().__init__(serializable_properties=[
56-
'body', 'actions', 'selectAction', 'style'
57-
],
58-
simple_properties=[
59-
'version', 'fallbackText', 'lang', 'schema', 'type'
60-
])
57+
# Check types
58+
check_type(actions, (ShowCard, Submit, OpenUrl), True, True)
59+
check_type(selectAction, (Submit, OpenUrl), False, True)
60+
check_type(fallbackText, str, False, True)
61+
check_type(lang, str, False, True)
6162

6263
# Set properties
6364
self.type = "AdaptiveCard"
6465
self.version = "1.1" # This is the version currently supported in Teams
6566
self.body = body
6667
self.actions = actions
6768
self.selectAction = selectAction
68-
self.style = style
6969
self.fallbackText = fallbackText
7070
self.lang = lang
7171
self.schema = "http://adaptivecards.io/schemas/adaptive-card.json"
72+
73+
super().__init__(serializable_properties=[
74+
'body', 'actions', 'selectAction'
75+
],
76+
simple_properties=[
77+
'version', 'fallbackText', 'lang', 'schema', 'type'
78+
])

webexteamssdk/cards/utils.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,67 @@
2525
def set_if_not_none(property_name, property, export):
2626
if property is not None:
2727
export[property_name] = property.to_dict()
28+
29+
def check_type(obj, acceptable_types, is_list=False, may_be_none=False):
30+
"""Object is an instance of one of the acceptable types or None.
31+
32+
Args:
33+
obj: The object to be inspected.
34+
acceptable_types: A type or tuple of acceptable types.
35+
is_list(bool): Whether or not we expect a list of objects of acceptable
36+
type
37+
may_be_none(bool): Whether or not the object may be None.
38+
39+
Raises:
40+
TypeError: If the object is None and may_be_none=False, or if the
41+
object is not an instance of one of the acceptable types.
42+
43+
"""
44+
error_message = None
45+
if not isinstance(acceptable_types, tuple):
46+
acceptable_types = (acceptable_types,)
47+
48+
if may_be_none and obj is None:
49+
pass
50+
elif is_list:
51+
# Check that all objects in that list are of the required type
52+
if not isinstance(obj, list):
53+
error_message = (
54+
"We were expecting to receive a list of one of the following "
55+
"types: {types}{none}; but instead we received {o} which is a "
56+
"{o_type}.".format(
57+
types=", ".join([repr(t.__name__) for t in acceptable_types]),
58+
none="or 'None'" if may_be_none else "",
59+
o=obj,
60+
o_type=repr(type(obj).__name__)
61+
)
62+
)
63+
else:
64+
for o in obj:
65+
if not isinstance(o, acceptable_types):
66+
error_message = (
67+
"We were expecting to receive an instance of one of the following "
68+
"types: {types}{none}; but instead we received {o} which is a "
69+
"{o_type}.".format(
70+
types=", ".join([repr(t.__name__) for t in acceptable_types]),
71+
none="or 'None'" if may_be_none else "",
72+
o=o,
73+
o_type=repr(type(o).__name__)
74+
)
75+
)
76+
elif isinstance(obj, acceptable_types):
77+
pass
78+
else:
79+
# Object is something else.
80+
error_message = (
81+
"We were expecting to receive an instance of one of the following "
82+
"types: {types}{none}; but instead we received {o} which is a "
83+
"{o_type}.".format(
84+
types=", ".join([repr(t.__name__) for t in acceptable_types]),
85+
none="or 'None'" if may_be_none else "",
86+
o=obj,
87+
o_type=repr(type(obj).__name__)
88+
)
89+
)
90+
if error_message is not None:
91+
raise TypeError(error_message)

0 commit comments

Comments
 (0)