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