Skip to content

Commit 6073ffe

Browse files
committed
refactor: move url source block element to documented elements
1 parent 8745c7b commit 6073ffe

5 files changed

Lines changed: 62 additions & 54 deletions

File tree

‎slack_sdk/models/blocks/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
StaticSelectElement,
5656
TimePickerElement,
5757
UrlInputElement,
58+
UrlSourceElement,
5859
UserMultiSelectElement,
5960
UserSelectElement,
6061
)
@@ -112,6 +113,7 @@
112113
"PlainTextInputElement",
113114
"EmailInputElement",
114115
"UrlInputElement",
116+
"UrlSourceElement",
115117
"NumberInputElement",
116118
"RadioButtonsElement",
117119
"SelectElement",

‎slack_sdk/models/blocks/block_elements.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,48 @@ def __init__(
16541654
self.dispatch_action_config = dispatch_action_config
16551655

16561656

1657+
# -------------------------------------------------
1658+
# Url Source Element
1659+
# -------------------------------------------------
1660+
1661+
1662+
class UrlSourceElement(BlockElement):
1663+
type = "url"
1664+
1665+
@property
1666+
def attributes(self) -> Set[str]:
1667+
return super().attributes.union(
1668+
{
1669+
"url",
1670+
"text",
1671+
"icon_url",
1672+
}
1673+
)
1674+
1675+
def __init__(
1676+
self,
1677+
*,
1678+
url: str,
1679+
text: str,
1680+
icon_url: Optional[str] = None,
1681+
**others: Dict,
1682+
):
1683+
"""
1684+
A URL source element to reference in a task card block.
1685+
https://docs.slack.dev/reference/block-kit/block-elements/url-source-element
1686+
1687+
Args:
1688+
url (required): The URL type source.
1689+
text (required): Display text for the URL.
1690+
icon_url: Optional icon URL to display with the source.
1691+
"""
1692+
super().__init__(type=self.type)
1693+
show_unknown_key_warning(self, others)
1694+
self.url = url
1695+
self.text = text
1696+
self.icon_url = icon_url
1697+
1698+
16571699
# -------------------------------------------------
16581700
# Number Input Element
16591701
# -------------------------------------------------

‎slack_sdk/models/blocks/blocks.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from slack_sdk.models import show_unknown_key_warning
77
from slack_sdk.models.basic_objects import JsonObject, JsonValidator
8-
from slack_sdk.models.messages.chunk import URLSource
98

109
from ...errors import SlackObjectFormationError
1110
from .basic_components import MarkdownTextObject, PlainTextObject, SlackFile, TextObject
@@ -17,6 +16,7 @@
1716
InputInteractiveElement,
1817
InteractiveElement,
1918
RichTextElement,
19+
UrlSourceElement,
2020
)
2121

2222
# -------------------------------------------------
@@ -805,7 +805,7 @@ def __init__(
805805
title: str,
806806
details: Optional[Union[RichTextBlock, dict]] = None,
807807
output: Optional[Union[RichTextBlock, dict]] = None,
808-
sources: Optional[Sequence[Union[URLSource, dict]]] = None,
808+
sources: Optional[Sequence[Union[UrlSourceElement, dict]]] = None,
809809
status: str, # pending, in_progress, complete, error
810810
block_id: Optional[str] = None,
811811
**others: dict,

‎slack_sdk/models/messages/chunk.py‎

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import logging
2-
from typing import Any, Dict, Optional, Sequence, Set, Union
2+
from typing import Dict, Optional, Sequence, Set, Union
33

4-
from slack_sdk.errors import SlackObjectFormationError
54
from slack_sdk.models import show_unknown_key_warning
65
from slack_sdk.models.basic_objects import JsonObject
6+
from slack_sdk.models.blocks.block_elements import UrlSourceElement
77

88

99
class Chunk(JsonObject):
@@ -67,44 +67,6 @@ def __init__(
6767
self.text = text
6868

6969

70-
class URLSource(JsonObject):
71-
type = "url"
72-
73-
@property
74-
def attributes(self) -> Set[str]:
75-
return super().attributes.union(
76-
{
77-
"url",
78-
"text",
79-
"icon_url",
80-
}
81-
)
82-
83-
def __init__(
84-
self,
85-
*,
86-
url: str,
87-
text: str,
88-
icon_url: Optional[str] = None,
89-
**others: Dict,
90-
):
91-
show_unknown_key_warning(self, others)
92-
self._url = url
93-
self._text = text
94-
self._icon_url = icon_url
95-
96-
def to_dict(self) -> Dict[str, Any]:
97-
self.validate_json()
98-
json: Dict[str, Union[str, Dict]] = {
99-
"type": self.type,
100-
"url": self._url,
101-
"text": self._text,
102-
}
103-
if self._icon_url:
104-
json["icon_url"] = self._icon_url
105-
return json
106-
107-
10870
class TaskUpdateChunk(Chunk):
10971
type = "task_update"
11072

@@ -129,7 +91,7 @@ def __init__(
12991
status: str, # "pending", "in_progress", "complete", "error"
13092
details: Optional[str] = None,
13193
output: Optional[str] = None,
132-
sources: Optional[Sequence[Union[Dict, URLSource]]] = None,
94+
sources: Optional[Sequence[Union[Dict, UrlSourceElement]]] = None,
13395
**others: Dict,
13496
):
13597
"""Used for displaying tool execution progress in a timeline-style UI.
@@ -144,12 +106,4 @@ def __init__(
144106
self.status = status
145107
self.details = details
146108
self.output = output
147-
if sources is not None:
148-
self.sources = []
149-
for src in sources:
150-
if isinstance(src, Dict):
151-
self.sources.append(src)
152-
elif isinstance(src, URLSource):
153-
self.sources.append(src.to_dict())
154-
else:
155-
raise SlackObjectFormationError(f"Unsupported type for source in task update chunk: {type(src)}")
109+
self.sources = sources

‎tests/slack_sdk/models/test_chunks.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22

3-
from slack_sdk.models.messages.chunk import MarkdownTextChunk, TaskUpdateChunk, URLSource
3+
from slack_sdk.models.blocks.block_elements import UrlSourceElement
4+
from slack_sdk.models.messages.chunk import MarkdownTextChunk, TaskUpdateChunk
45

56

67
class MarkdownTextChunkTests(unittest.TestCase):
@@ -47,7 +48,11 @@ def test_json(self):
4748
status="complete",
4849
output="Found a solution",
4950
sources=[
50-
URLSource(
51+
UrlSourceElement(
52+
text="Discussion of Life's Questions",
53+
url="https://www.answers.com",
54+
),
55+
UrlSourceElement(
5156
text="The Free Encyclopedia",
5257
url="https://wikipedia.org",
5358
icon_url="https://example.com/globe.png",
@@ -61,6 +66,11 @@ def test_json(self):
6166
"status": "complete",
6267
"output": "Found a solution",
6368
"sources": [
69+
{
70+
"type": "url",
71+
"text": "Discussion of Life's Questions",
72+
"url": "https://www.answers.com",
73+
},
6474
{
6575
"type": "url",
6676
"text": "The Free Encyclopedia",

0 commit comments

Comments
 (0)