Skip to content

Commit 755e0ab

Browse files
committed
Adding the broadcast commands and api calls
1 parent 7246c6f commit 755e0ab

2 files changed

Lines changed: 186 additions & 0 deletions

File tree

api.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,96 @@ def list_alert_comments(self, alert_id: str, limit: int = 10,
135135
"offset": offset
136136
}
137137

138+
# BROADCASTS
139+
# =========
140+
141+
def create_broadcast(
142+
self,
143+
title: str,
144+
description: Optional[str] = None,
145+
destination_account_user_ids: Optional[List[str]] = None,
146+
destination_team_ids: Optional[List[str]] = None
147+
) -> Dict[str, Any]:
148+
"""Create a new broadcast in PagerTree."""
149+
payload = {
150+
"title": title,
151+
"description": description,
152+
"destination_account_user_ids": destination_account_user_ids or [],
153+
"destination_team_ids": destination_team_ids or []
154+
}
155+
payload = {k: v for k, v in payload.items() if v is not None}
156+
response = self.session.post(f"{self.base_url}/broadcasts", json=payload)
157+
response.raise_for_status()
158+
return response.json()
159+
160+
def list_broadcasts(self, limit: int = 10, offset: int = 0) -> Dict[str, Any]:
161+
"""List all broadcasts in PagerTree."""
162+
params = {k: v for k, v in {"limit": limit, "offset": offset}.items() if v is not None}
163+
response = self.session.get(f"{self.base_url}/broadcasts", params=params)
164+
response.raise_for_status()
165+
data = response.json()
166+
return {
167+
"data": data.get("data", []),
168+
"total": data.get("total_count", 0),
169+
"has_more": data.get("has_more", False),
170+
"limit": limit,
171+
"offset": offset
172+
}
173+
174+
def show_broadcast(self, broadcast_id: str) -> Dict[str, Any]:
175+
"""Fetch a single broadcast by ID from PagerTree."""
176+
response = self.session.get(f"{self.base_url}/broadcasts/{broadcast_id}")
177+
response.raise_for_status()
178+
return response.json()
179+
180+
def update_broadcast(
181+
self,
182+
broadcast_id: str,
183+
title: Optional[str] = None,
184+
description: Optional[str] = None,
185+
destination_account_user_ids: Optional[List[str]] = None,
186+
destination_team_ids: Optional[List[str]] = None,
187+
response_requested: Optional[bool] = None,
188+
response_requested_by: Optional[str] = None,
189+
status: Optional[str] = None,
190+
notify_sms: Optional[bool] = None,
191+
notify_push: Optional[bool] = None,
192+
notify_email: Optional[bool] = None,
193+
notify_slack: Optional[bool] = None,
194+
notify_voice: Optional[bool] = None,
195+
notify_whatsapp: Optional[bool] = None
196+
) -> Dict[str, Any]:
197+
"""Update a broadcast in PagerTree."""
198+
payload = {
199+
"title": title,
200+
"description": description,
201+
"destination_account_user_ids": destination_account_user_ids,
202+
"destination_team_ids": destination_team_ids,
203+
"response_requested": response_requested,
204+
"response_requested_by": response_requested_by,
205+
"status": status,
206+
"meta": {
207+
"notify_sms": notify_sms,
208+
"notify_push": notify_push,
209+
"notify_email": notify_email,
210+
"notify_slack": notify_slack,
211+
"notify_voice": notify_voice,
212+
"notify_whatsapp": notify_whatsapp
213+
}
214+
}
215+
payload = {k: v for k, v in payload.items() if v is not None}
216+
if "meta" in payload:
217+
payload["meta"] = {k: v for k, v in payload["meta"].items() if v is not None}
218+
response = self.session.put(f"{self.base_url}/broadcasts/{broadcast_id}", json=payload)
219+
response.raise_for_status()
220+
return response.json()
221+
222+
def delete_broadcast(self, broadcast_id: str) -> Dict[str, Any]:
223+
"""Delete a broadcast in PagerTree."""
224+
response = self.session.delete(f"{self.base_url}/broadcasts/{broadcast_id}")
225+
response.raise_for_status()
226+
return response.json() if response.content else {"message": "Broadcast deleted successfully"}
227+
138228
# TEAMS
139229
# ======
140230

commands/broadcasts.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import click
2+
from utils import display_paginated_results, handle_api_error, format_item_details
3+
from datetime import datetime
4+
5+
@click.group()
6+
def broadcasts():
7+
"""Commands for managing PagerTree broadcasts."""
8+
pass
9+
10+
@broadcasts.command(name="create")
11+
@click.option("--title", required=True, help="Title of the broadcast")
12+
@click.option("--description", help="Description of the broadcast")
13+
@click.option("--user-id", "user_ids", multiple=True, help="Account user IDs to receive the broadcast")
14+
@click.option("--team-id", "team_ids", multiple=True, help="Team IDs to receive the broadcast")
15+
@click.pass_context
16+
def create_broadcast_cmd(
17+
ctx, title, description, user_ids, team_ids
18+
):
19+
"""Create a new broadcast in PagerTree."""
20+
try:
21+
client = ctx.obj # Get PagerTreeClient from context
22+
result = client.create_broadcast(
23+
title=title,
24+
description=description,
25+
destination_account_user_ids=list(user_ids) if user_ids else None,
26+
destination_team_ids=list(team_ids) if team_ids else None
27+
)
28+
click.echo(f"Broadcast created successfully: {result.get('id', 'N/A')}")
29+
except Exception as e:
30+
handle_api_error(e, action="creating broadcast")
31+
32+
@broadcasts.command(name="list")
33+
@click.option("--limit", default=10, type=click.IntRange(1, 100), help="Number of broadcasts per page")
34+
@click.option("--offset", default=0, type=click.IntRange(0), help="Starting point for pagination")
35+
@click.pass_context
36+
def list_broadcasts_cmd(ctx, limit, offset):
37+
"""List broadcasts in PagerTree with pagination."""
38+
try:
39+
client = ctx.obj # Get PagerTreeClient from context
40+
result = client.list_broadcasts(limit=limit, offset=offset)
41+
broadcasts_list = result["data"]
42+
total = result["total"]
43+
# Prepare table data
44+
headers = ["ID", "Title", "Status", "Created At"]
45+
table_data = [
46+
[
47+
broadcast.get("id", "N/A"),
48+
broadcast.get("title", "N/A"),
49+
broadcast.get("status", "N/A"),
50+
broadcast.get("created_at", "N/A")
51+
]
52+
for broadcast in broadcasts_list
53+
]
54+
display_paginated_results(broadcasts_list, total, limit, offset, "broadcast", headers, table_data)
55+
except Exception as e:
56+
handle_api_error(e, action="listing broadcasts")
57+
58+
@broadcasts.command(name="show")
59+
@click.argument("broadcast_id", required=True)
60+
@click.pass_context
61+
def show_broadcast_cmd(ctx, broadcast_id):
62+
"""Show details of a specific broadcast in PagerTree."""
63+
try:
64+
client = ctx.obj # Get PagerTreeClient from context
65+
broadcast = client.show_broadcast(broadcast_id)
66+
fields = {
67+
"id": "Broadcast ID",
68+
"title": "Title",
69+
"description": "Description",
70+
"status": "Status"
71+
}
72+
formatted_broadcast = {
73+
"id": broadcast.get("id", "N/A"),
74+
"title": broadcast.get("title", "N/A"),
75+
"description": broadcast.get("description", "N/A"),
76+
"status": broadcast.get("status", "N/A")
77+
}
78+
format_item_details(formatted_broadcast, fields)
79+
except Exception as e:
80+
handle_api_error(e, action="showing broadcast")
81+
82+
@broadcasts.command(name="delete")
83+
@click.argument("broadcast_id", required=True)
84+
@click.option("--force", is_flag=True, help="Delete the broadcast without confirmation")
85+
@click.pass_context
86+
def delete_broadcast_cmd(ctx, broadcast_id, force):
87+
"""Delete a broadcast in PagerTree."""
88+
if not force and not click.confirm(f"Are you sure you want to delete broadcast {broadcast_id}?"):
89+
click.echo("Deletion cancelled.")
90+
return
91+
try:
92+
client = ctx.obj # Get PagerTreeClient from context
93+
result = client.delete_broadcast(broadcast_id)
94+
click.echo(f"Broadcast deleted successfully: {broadcast_id}")
95+
except Exception as e:
96+
handle_api_error(e, action="deleting broadcast")

0 commit comments

Comments
 (0)