Skip to content

Commit c717740

Browse files
committed
feat: support channels shortcut in wrapper
1 parent f851c5e commit c717740

3 files changed

Lines changed: 80 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ response = activitysmith.notifications.send(
4040
{
4141
"title": "Build Failed",
4242
"message": "CI pipeline failed on main branch",
43+
"channels": ["devs", "ops"], # Optional
4344
}
4445
)
4546

@@ -59,7 +60,8 @@ start = activitysmith.live_activities.start(
5960
"current_step": 1,
6061
"type": "segmented_progress",
6162
"color": "yellow",
62-
}
63+
},
64+
"channels": ["devs", "ops"], # Optional
6365
}
6466
)
6567

activitysmith/client.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,36 @@
1010
from activitysmith_openapi.api.push_notifications_api import PushNotificationsApi
1111

1212

13+
def _normalize_channels_target(request: Any) -> Any:
14+
if not isinstance(request, dict):
15+
return request
16+
17+
if "target" in request or "channels" not in request:
18+
return request
19+
20+
normalized = dict(request)
21+
raw_channels = normalized.pop("channels")
22+
23+
channels: list[str] = []
24+
if isinstance(raw_channels, str):
25+
channels = [item.strip() for item in raw_channels.split(",") if item.strip() != ""]
26+
elif isinstance(raw_channels, (list, tuple)):
27+
channels = [item.strip() for item in raw_channels if isinstance(item, str) and item.strip() != ""]
28+
29+
if channels:
30+
normalized["target"] = {"channels": channels}
31+
32+
return normalized
33+
34+
1335
class NotificationsResource:
1436
def __init__(self, api: PushNotificationsApi) -> None:
1537
self._api = api
1638

1739
def send(self, request: Any):
18-
return self._api.send_push_notification(push_notification_request=request)
40+
return self._api.send_push_notification(
41+
push_notification_request=_normalize_channels_target(request)
42+
)
1943

2044
# Backward-compatible alias.
2145
def send_push_notification(self, push_notification_request: Any):
@@ -27,7 +51,9 @@ def __init__(self, api: LiveActivitiesApi) -> None:
2751
self._api = api
2852

2953
def start(self, request: Any):
30-
return self._api.start_live_activity(live_activity_start_request=request)
54+
return self._api.start_live_activity(
55+
live_activity_start_request=_normalize_channels_target(request)
56+
)
3157

3258
def update(self, request: Any):
3359
return self._api.update_live_activity(live_activity_update_request=request)

tests/test_resources.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ def test_notifications_short_and_legacy_alias(monkeypatch):
4646
]
4747

4848

49+
def test_notifications_map_channels_to_target(monkeypatch):
50+
monkeypatch.setattr(client_module, "PushNotificationsApi", FakePushNotificationsApi)
51+
monkeypatch.setattr(client_module, "LiveActivitiesApi", FakeLiveActivitiesApi)
52+
53+
client = ActivitySmith(api_key="x")
54+
payload = {"title": "Build Failed", "channels": ["devs", "ops"]}
55+
56+
client.notifications.send(payload)
57+
client.notifications.send_push_notification({"title": "Build Failed", "channels": "devs,ops"})
58+
59+
assert client.notifications._api.calls == [
60+
{"push_notification_request": {"title": "Build Failed", "target": {"channels": ["devs", "ops"]}}},
61+
{"push_notification_request": {"title": "Build Failed", "target": {"channels": ["devs", "ops"]}}},
62+
]
63+
64+
4965
def test_live_activities_short_and_legacy_aliases(monkeypatch):
5066
monkeypatch.setattr(client_module, "PushNotificationsApi", FakePushNotificationsApi)
5167
monkeypatch.setattr(client_module, "LiveActivitiesApi", FakeLiveActivitiesApi)
@@ -84,3 +100,36 @@ def test_live_activities_short_and_legacy_aliases(monkeypatch):
84100
("update", {"live_activity_update_request": update_payload}),
85101
("end", {"live_activity_end_request": end_payload}),
86102
]
103+
104+
105+
def test_live_activities_start_maps_channels_to_target(monkeypatch):
106+
monkeypatch.setattr(client_module, "PushNotificationsApi", FakePushNotificationsApi)
107+
monkeypatch.setattr(client_module, "LiveActivitiesApi", FakeLiveActivitiesApi)
108+
109+
client = ActivitySmith(api_key="x")
110+
payload = {
111+
"content_state": {
112+
"title": "Deploy",
113+
"number_of_steps": 4,
114+
"current_step": 1,
115+
"type": "segmented_progress",
116+
},
117+
"channels": ["devs", "ops"],
118+
}
119+
120+
client.live_activities.start(payload)
121+
client.live_activities.start_live_activity(payload)
122+
123+
expected = {
124+
"content_state": {
125+
"title": "Deploy",
126+
"number_of_steps": 4,
127+
"current_step": 1,
128+
"type": "segmented_progress",
129+
},
130+
"target": {"channels": ["devs", "ops"]},
131+
}
132+
assert client.live_activities._api.calls == [
133+
("start", {"live_activity_start_request": expected}),
134+
("start", {"live_activity_start_request": expected}),
135+
]

0 commit comments

Comments
 (0)