-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
389 lines (352 loc) · 16.4 KB
/
api.py
File metadata and controls
389 lines (352 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import click
import requests
import os
import configparser
from typing import Optional, List, Dict, Any
class PagerTreeClient:
def __init__(self):
"""Initialize PagerTree client with configuration."""
# Set up base URL and API key
self.base_url = os.getenv('PAGERTREE_BASE_URL', 'https://api.pagertree.com/api/v4')
self.api_key = os.getenv('PAGERTREE_API_KEY')
self.user_agent = "PagerTree-Python-CLI-Client/1.0"
if not self.api_key:
raise click.UsageError("PAGERTREE_API_KEY must be provided via environment variable or .env config file")
# Set up default headers
self.default_headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
"User-Agent": self.user_agent
}
# Create a session for persistent connections
self.session = requests.Session()
self.session.headers.update(self.default_headers)
# ALERTS
# =======
def create_alert(self, title: str, description: Optional[str] = None,
team_ids: Optional[List[str]] = None,
destination_router_ids: Optional[List[str]] = None,
destination_account_user_ids: Optional[List[str]] = None,
urgency: str = "medium", tags: Optional[List[str]] = None,
alias: Optional[str] = None, incident: bool = False,
incident_severity: Optional[str] = None,
incident_message: Optional[str] = None) -> Dict[str, Any]:
"""Create a new alert in PagerTree."""
payload = {
"title": title,
"description": description,
"destination_team_ids": team_ids,
"destination_route_ids": destination_router_ids,
"destination_account_user_ids": destination_account_user_ids,
"urgency": urgency,
"tags": tags,
"thirdparty_id": alias,
"meta": {
"incident": incident,
"incident_severity": incident_severity,
"incident_message": incident_message
}
}
payload = {k: v for k, v in payload.items() if v is not None}
response = self.session.post(f"{self.base_url}/alerts", json=payload)
response.raise_for_status()
return response.json()
def list_alerts(self, limit: int = 10, offset: int = 0,
status: Optional[str] = None, search: Optional[str] = None, alias: Optional[str] = None) -> Dict[str, Any]:
"""List all alerts in PagerTree."""
params = {k: v for k, v in {"limit": limit, "offset": offset, "status": status, "q": search, "thirdparty_id": alias}.items()
if v is not None}
response = self.session.get(f"{self.base_url}/alerts", params=params)
response.raise_for_status()
data = response.json()
return {
"data": data.get("data", []),
"total": data.get("total_count", 0),
"has_more": data.get("has_more", False),
"limit": limit,
"offset": offset
}
def show_alert(self, alert_id: str) -> Dict[str, Any]:
"""Fetch a single alert by ID from PagerTree."""
response = self.session.get(f"{self.base_url}/alerts/{alert_id}")
response.raise_for_status()
return response.json()
def delete_alert(self, alert_id: str) -> Dict[str, Any]:
"""Delete an alert in PagerTree."""
response = self.session.delete(f"{self.base_url}/alerts/{alert_id}")
response.raise_for_status()
return response.json() if response.content else {"message": "Alert deleted successfully"}
def acknowledge_alert(self, alert_id: str) -> Dict[str, Any]:
"""Acknowledge an alert in PagerTree."""
response = self.session.post(f"{self.base_url}/alerts/{alert_id}/acknowledge")
response.raise_for_status()
return response.json()
def reject_alert(self, alert_id: str) -> Dict[str, Any]:
"""Reject an alert in PagerTree."""
response = self.session.post(f"{self.base_url}/alerts/{alert_id}/reject")
response.raise_for_status()
return response.json()
def resolve_alert(self, alert_id: str) -> Dict[str, Any]:
"""Resolve an alert in PagerTree."""
response = self.session.post(f"{self.base_url}/alerts/{alert_id}/resolve")
response.raise_for_status()
return response.json()
def create_alert_comment(self, alert_id: str, comment: str) -> Dict[str, Any]:
"""Create a comment on an alert in PagerTree."""
payload = {"body": comment}
response = self.session.post(f"{self.base_url}/alerts/{alert_id}/comments", json=payload)
response.raise_for_status()
return response.json()
def list_alert_comments(self, alert_id: str, limit: int = 10,
offset: int = 0) -> Dict[str, Any]:
"""List all comments for a specific alert in PagerTree."""
params = {k: v for k, v in {"limit": limit, "offset": offset}.items() if v is not None}
response = self.session.get(f"{self.base_url}/alerts/{alert_id}/comments", params=params)
response.raise_for_status()
data = response.json()
return {
"data": data.get("data", []),
"total": data.get("total_count", 0),
"has_more": data.get("has_more", False),
"limit": limit,
"offset": offset
}
# BROADCASTS
# =========
def create_broadcast(
self,
title: str,
description: Optional[str] = None,
destination_account_user_ids: Optional[List[str]] = None,
destination_team_ids: Optional[List[str]] = None
) -> Dict[str, Any]:
"""Create a new broadcast in PagerTree."""
payload = {
"title": title,
"description": description,
"destination_account_user_ids": destination_account_user_ids or [],
"destination_team_ids": destination_team_ids or []
}
payload = {k: v for k, v in payload.items() if v is not None}
response = self.session.post(f"{self.base_url}/broadcasts", json=payload)
response.raise_for_status()
return response.json()
def list_broadcasts(self, limit: int = 10, offset: int = 0) -> Dict[str, Any]:
"""List all broadcasts in PagerTree."""
params = {k: v for k, v in {"limit": limit, "offset": offset}.items() if v is not None}
response = self.session.get(f"{self.base_url}/broadcasts", params=params)
response.raise_for_status()
data = response.json()
return {
"data": data.get("data", []),
"total": data.get("total_count", 0),
"has_more": data.get("has_more", False),
"limit": limit,
"offset": offset
}
def show_broadcast(self, broadcast_id: str) -> Dict[str, Any]:
"""Fetch a single broadcast by ID from PagerTree."""
response = self.session.get(f"{self.base_url}/broadcasts/{broadcast_id}")
response.raise_for_status()
return response.json()
def update_broadcast(
self,
broadcast_id: str,
title: Optional[str] = None,
description: Optional[str] = None,
destination_account_user_ids: Optional[List[str]] = None,
destination_team_ids: Optional[List[str]] = None,
response_requested: Optional[bool] = None,
response_requested_by: Optional[str] = None,
status: Optional[str] = None,
notify_sms: Optional[bool] = None,
notify_push: Optional[bool] = None,
notify_email: Optional[bool] = None,
notify_slack: Optional[bool] = None,
notify_voice: Optional[bool] = None,
notify_whatsapp: Optional[bool] = None
) -> Dict[str, Any]:
"""Update a broadcast in PagerTree."""
payload = {
"title": title,
"description": description,
"destination_account_user_ids": destination_account_user_ids,
"destination_team_ids": destination_team_ids,
"response_requested": response_requested,
"response_requested_by": response_requested_by,
"status": status,
"meta": {
"notify_sms": notify_sms,
"notify_push": notify_push,
"notify_email": notify_email,
"notify_slack": notify_slack,
"notify_voice": notify_voice,
"notify_whatsapp": notify_whatsapp
}
}
payload = {k: v for k, v in payload.items() if v is not None}
if "meta" in payload:
payload["meta"] = {k: v for k, v in payload["meta"].items() if v is not None}
response = self.session.put(f"{self.base_url}/broadcasts/{broadcast_id}", json=payload)
response.raise_for_status()
return response.json()
def delete_broadcast(self, broadcast_id: str) -> Dict[str, Any]:
"""Delete a broadcast in PagerTree."""
response = self.session.delete(f"{self.base_url}/broadcasts/{broadcast_id}")
response.raise_for_status()
return response.json() if response.content else {"message": "Broadcast deleted successfully"}
# INTEGRATIONS
# ============
def list_integrations(self, limit: int = 10, offset: int = 0, search: Optional[str] = None, enabled: Optional[bool] = None) -> Dict[str, Any]:
"""List all integrations in PagerTree."""
params = {k: v for k, v in {"limit": limit, "offset": offset, "q": search, "enabled": enabled}.items() if v is not None}
response = self.session.get(f"{self.base_url}/integrations", params=params)
response.raise_for_status()
data = response.json()
return {
"data": data.get("data", []),
"total": data.get("total_count", 0),
"has_more": data.get("has_more", False),
"limit": limit,
"offset": offset
}
def show_integration(self, integration_id: str) -> Dict[str, Any]:
"""Fetch a single integration by ID from PagerTree."""
response = self.session.get(f"{self.base_url}/integrations/{integration_id}")
response.raise_for_status()
return response.json()
def update_integration(self, integration_id: str, enabled: Optional[bool] = None) -> Dict[str, Any]:
"""Enable an integration in PagerTree."""
payload = {"enabled": enabled}
payload = {k: v for k, v in payload.items() if v is not None}
response = self.session.put(f"{self.base_url}/integrations/{integration_id}", json=payload)
response.raise_for_status()
return response.json()
# TEAMS
# ======
def create_team(
self,
name: str,
notes: Optional[str] = None,
member_account_user_ids: Optional[List[str]] = None,
admin_account_user_ids: Optional[List[str]] = None
) -> Dict[str, Any]:
"""Create a new team in PagerTree."""
payload = {
"name": name,
"notes": notes,
"member_account_user_ids": member_account_user_ids or [],
"admin_account_user_ids": admin_account_user_ids or []
}
payload = {k: v for k, v in payload.items() if v is not None}
response = self.session.post(f"{self.base_url}/teams", json=payload)
response.raise_for_status()
return response.json()
def list_teams(self, limit: int = 10, offset: int = 0, search: Optional[str] = None) -> Dict[str, Any]:
"""List all teams in PagerTree."""
params = {k: v for k, v in {"limit": limit, "offset": offset, "q": search}.items() if v is not None}
response = self.session.get(f"{self.base_url}/teams", params=params)
response.raise_for_status()
data = response.json()
return {
"data": data.get("data", []),
"total": data.get("total_count", 0),
"has_more": data.get("has_more", False),
"limit": limit,
"offset": offset
}
def show_team(self, team_id: str) -> Dict[str, Any]:
"""Fetch a single team by ID from PagerTree."""
response = self.session.get(f"{self.base_url}/teams/{team_id}")
response.raise_for_status()
return response.json()
def update_team(
self,
team_id: str,
name: Optional[str] = None,
notes: Optional[str] = None,
member_account_user_ids: Optional[List[str]] = None,
admin_account_user_ids: Optional[List[str]] = None
) -> Dict[str, Any]:
"""Update a team in PagerTree."""
payload = {
"name": name,
"notes": notes,
"member_account_user_ids": member_account_user_ids,
"admin_account_user_ids": admin_account_user_ids
}
payload = {k: v for k, v in payload.items() if v is not None}
response = self.session.put(f"{self.base_url}/teams/{team_id}", json=payload)
response.raise_for_status()
return response.json()
def delete_team(self, team_id: str) -> Dict[str, Any]:
"""Delete a team in PagerTree."""
response = self.session.delete(f"{self.base_url}/teams/{team_id}")
response.raise_for_status()
return response.json() if response.content else {"message": "Team deleted successfully"}
def get_team_current_oncall(self, team_id: str) -> Dict[str, Any]:
"""Fetch current on-call users for a team in PagerTree."""
response = self.session.get(f"{self.base_url}/teams/{team_id}/current_oncall")
response.raise_for_status()
return response.json()
def get_team_alerts(self, team_id: str, limit: int = 10, offset: int = 0) -> Dict[str, Any]:
"""Fetch alerts for a specific team in PagerTree."""
params = {k: v for k, v in {"limit": limit, "offset": offset}.items() if v is not None}
response = self.session.get(f"{self.base_url}/teams/{team_id}/alerts", params=params)
response.raise_for_status()
data = response.json()
return {
"data": data.get("data", []),
"total": data.get("total_count", 0),
"has_more": data.get("has_more", False),
"limit": limit,
"offset": offset
}
# USERS
# ======
def create_user(self, name: str, email: str, roles: Optional[Dict[str, bool]] = None, team_ids: Optional[List[str]] = None) -> Dict[str, Any]:
"""Create a new account user in PagerTree."""
payload = {
"user_attributes": {
"name": name,
"emails_attributes": [{"email": email}]
},
"roles": roles or {},
"team_ids": team_ids or []
}
response = self.session.post(f"{self.base_url}/account_users", json=payload)
response.raise_for_status()
return response.json()
def list_users(self, limit: int = 10, offset: int = 0, search: Optional[str] = None) -> Dict[str, Any]:
"""List all users in PagerTree."""
params = {k: v for k, v in {"limit": limit, "offset": offset, "q": search}.items() if v is not None}
response = self.session.get(f"{self.base_url}/account_users", params=params)
response.raise_for_status()
data = response.json()
return {
"data": data.get("data", []),
"total": data.get("total_count", 0),
"has_more": data.get("has_more", False),
"limit": limit,
"offset": offset
}
def show_user(self, user_id: str) -> Dict[str, Any]:
"""Fetch a single user by ID from PagerTree."""
response = self.session.get(f"{self.base_url}/account_users/{user_id}")
response.raise_for_status()
return response.json()
def update_user(self, user_id: str, name: Optional[str] = None) -> Dict[str, Any]:
"""Update an account user in PagerTree."""
payload = {}
if name:
payload["user_attributes"] = {}
payload["user_attributes"]["name"] = name
payload = {k: v for k, v in payload.items() if v}
response = self.session.put(f"{self.base_url}/account_users/{user_id}", json=payload)
response.raise_for_status()
return response.json()
def delete_user(self, user_id: str) -> Dict[str, Any]:
"""Delete a user in PagerTree."""
response = self.session.delete(f"{self.base_url}/account_users/{user_id}")
response.raise_for_status()
return response.json() if response.content else {"message": "User deleted successfully"}