Skip to content

Commit c09bd89

Browse files
authored
Fix/switch method for exporting cards as dicts (#23)
* [doc] Add TOC and documentation about sending cards resolves #21 * [feat] Rename example and add sending card functionality resolves #21 * [feat] Add MS Teams client for sending cards resolves #21 * [feat] Add sending card functionality to example resolves #21 * [refactor] Auto re-formatting applied * Merge branch 'main' of https://github.com/dennis6p/adaptive-cards into main * [fix] Switch method for exporting card as dict in client.send() * [refactor] Remove unused import * [refactor] Remove superfluous file
1 parent e7b3eab commit c09bd89

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ If you are interested in the general concepts of adaptive cards and want to dig
2626
## About
2727

2828
This library is intended to provide a clear and simple interface for creating adaptive cards with only a few lines of code in a more robust way. The heavy usage of Python's typing library should
29-
prevent one from creating invalid schemes and structures. Instead, creating cards should be intuitive and work like a breeze.
29+
prevent one from creating invalid schemes and structures. Instead, creating cards and sending cards should be intuitive and work like a breeze.
3030

3131
For a comprehensive introduction into the main ideas and patterns of adaptive cards, have a look on the [**official documentation**](https://docs.microsoft.com/en-us/adaptive-cards). I also recommend using the [**schema explorer**](https://adaptivecards.io/explorer) page alongside the implementation, since the library's type system relies on these schemes.
3232

@@ -39,12 +39,14 @@ For a comprehensive introduction into the main ideas and patterns of adaptive ca
3939
+ Schema validation for version compatibility
4040
+ Simple `JSON` export
4141
+ Compliant with the official structures and ideas
42+
+ Send cards to MS Teams via `TeamsClient`
4243

4344
## Dependencies
4445

4546
* Python 3.10+
4647
* `dataclasses-json`
4748
* `StrEnum`
49+
* `requests`
4850

4951
## Installation
5052

adaptive_cards/card.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Implementation of the adaptive card type"""
22

33
from dataclasses import dataclass, field
4-
from typing import Optional, Sequence
4+
from typing import Optional, Sequence, Any
55
from dataclasses_json import dataclass_json, LetterCase
66
from adaptive_cards.actions import SelectAction, ActionTypes
77
from adaptive_cards.containers import ContainerTypes
@@ -375,3 +375,12 @@ def to_json(self) -> str:
375375
str: Adaptive card schema as JSON string.
376376
"""
377377
return self.to_json()
378+
379+
def to_dict(self) -> dict[str, Any]:
380+
"""
381+
Converts the full adaptive card schema into a dictionary.
382+
383+
Returns:
384+
str: Adaptive card schema as dictionary.
385+
"""
386+
return self.to_dict()

adaptive_cards/client.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
""" # pylint: disable=line-too-long
88

99
from typing import Any
10-
from dataclasses import asdict
1110
from http import HTTPStatus
1211
import requests
1312

@@ -33,7 +32,7 @@ def _create_attachment(card: AdaptiveCard) -> dict:
3332
Returns:
3433
dict: A dictionary representing the attachment.
3534
"""
36-
content: dict[str, Any] = asdict(card)
35+
content: dict[str, Any] = card.to_dict()
3736
return {
3837
"contentType": "application/vnd.microsoft.card.adaptive",
3938
"contentUrl": None,

examples/simple_card/simple_card.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Example: simple card"""
22

3+
from requests import Response
34
from adaptive_cards.elements import TextBlock
45
import adaptive_cards.card_types as types
56
from adaptive_cards.card import AdaptiveCard
67
from adaptive_cards.validation import SchemaValidator, Result
78
from adaptive_cards.client import TeamsClient
89

10+
911
text_block: TextBlock = TextBlock(
1012
text="It's your second card",
1113
color=types.Colors.ACCENT,
@@ -26,4 +28,4 @@
2628
# send card
2729
webhook_url: str = "YOUR-URL"
2830
client: TeamsClient = TeamsClient(webhook_url)
29-
client.send(card)
31+
response: Response = client.send(card)

examples/wrap_up_card/wrap_up_card.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# pylint: disable=C0413
44

55
import sys
6+
from requests import Response
67

78
sys.path.append("../../")
89
import adaptive_cards.card_types as types
@@ -169,4 +170,4 @@
169170
# send card
170171
webhook_url: str = "YOUR-URL"
171172
client: TeamsClient = TeamsClient(webhook_url)
172-
client.send(card)
173+
response: Response = client.send(card)

0 commit comments

Comments
 (0)