forked from livekit/python-sdks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsip_service.py
More file actions
451 lines (394 loc) · 13 KB
/
Copy pathsip_service.py
File metadata and controls
451 lines (394 loc) · 13 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from __future__ import annotations
import aiohttp
from typing import Optional
from livekit.protocol.models import ListUpdate
from livekit.protocol.sip import (
SIPTrunkInfo,
CreateSIPInboundTrunkRequest,
UpdateSIPInboundTrunkRequest,
SIPInboundTrunkInfo,
SIPInboundTrunkUpdate,
CreateSIPOutboundTrunkRequest,
UpdateSIPOutboundTrunkRequest,
SIPOutboundTrunkInfo,
SIPOutboundTrunkUpdate,
ListSIPInboundTrunkRequest,
ListSIPInboundTrunkResponse,
ListSIPOutboundTrunkRequest,
ListSIPOutboundTrunkResponse,
DeleteSIPTrunkRequest,
SIPDispatchRule,
SIPDispatchRuleInfo,
SIPDispatchRuleUpdate,
CreateSIPDispatchRuleRequest,
UpdateSIPDispatchRuleRequest,
ListSIPDispatchRuleRequest,
ListSIPDispatchRuleResponse,
DeleteSIPDispatchRuleRequest,
CreateSIPParticipantRequest,
TransferSIPParticipantRequest,
SIPParticipantInfo,
SIPTransport,
)
from ._service import Service
from .access_token import VideoGrants, SIPGrants
SVC = "SIP"
"""@private"""
class SipService(Service):
"""Client for LiveKit SIP Service API
Recommended way to use this service is via `livekit.api.LiveKitAPI`:
```python
from livekit import api
lkapi = api.LiveKitAPI()
sip_service = lkapi.sip
```
"""
def __init__(self, session: aiohttp.ClientSession, url: str, api_key: str, api_secret: str):
super().__init__(session, url, api_key, api_secret)
async def create_sip_inbound_trunk(
self, create: CreateSIPInboundTrunkRequest
) -> SIPInboundTrunkInfo:
"""Create a new SIP inbound trunk.
Args:
create: Request containing trunk details
Returns:
Created SIP inbound trunk
"""
return await self._client.request(
SVC,
"CreateSIPInboundTrunk",
create,
self._admin_headers(),
SIPInboundTrunkInfo,
)
async def update_sip_inbound_trunk(
self,
trunk_id: str,
trunk: SIPInboundTrunkInfo,
) -> SIPInboundTrunkInfo:
"""Updates an existing SIP inbound trunk by replacing it entirely.
Args:
trunk_id: ID of the SIP inbound trunk to update
trunk: SIP inbound trunk to update with
Returns:
Updated SIP inbound trunk
"""
return await self._client.request(
SVC,
"UpdateSIPInboundTrunk",
UpdateSIPInboundTrunkRequest(
sip_trunk_id=trunk_id,
replace=trunk,
),
self._admin_headers(),
SIPInboundTrunkInfo,
)
async def update_sip_inbound_trunk_fields(
self,
trunk_id: str,
*,
numbers: Optional[list[str]] = None,
allowed_addresses: Optional[list[str]] = None,
allowed_numbers: Optional[list[str]] = None,
auth_username: Optional[str] = None,
auth_password: Optional[str] = None,
name: Optional[str] = None,
metadata: Optional[str] = None,
) -> SIPInboundTrunkInfo:
"""Updates specific fields of an existing SIP inbound trunk.
Only provided fields will be updated.
"""
update = SIPInboundTrunkUpdate(
auth_username=auth_username,
auth_password=auth_password,
name=name,
metadata=metadata,
)
if numbers is not None:
update.numbers = ListUpdate(set=numbers)
if allowed_addresses is not None:
update.allowed_addresses = ListUpdate(set=allowed_addresses)
if allowed_numbers is not None:
update.allowed_numbers = ListUpdate(set=allowed_numbers)
return await self._client.request(
SVC,
"UpdateSIPInboundTrunk",
UpdateSIPInboundTrunkRequest(
sip_trunk_id=trunk_id,
update=update,
),
self._admin_headers(),
SIPInboundTrunkInfo,
)
async def create_sip_outbound_trunk(
self, create: CreateSIPOutboundTrunkRequest
) -> SIPOutboundTrunkInfo:
"""Create a new SIP outbound trunk.
Args:
create: Request containing trunk details
Returns:
Created SIP outbound trunk
"""
return await self._client.request(
SVC,
"CreateSIPOutboundTrunk",
create,
self._admin_headers(),
SIPOutboundTrunkInfo,
)
async def update_sip_outbound_trunk(
self,
trunk_id: str,
trunk: SIPOutboundTrunkInfo,
) -> SIPOutboundTrunkInfo:
"""Updates an existing SIP outbound trunk by replacing it entirely.
Args:
trunk_id: ID of the SIP outbound trunk to update
trunk: SIP outbound trunk to update with
Returns:
Updated SIP outbound trunk
"""
return await self._client.request(
SVC,
"UpdateSIPOutboundTrunk",
UpdateSIPOutboundTrunkRequest(
sip_trunk_id=trunk_id,
replace=trunk,
),
self._admin_headers(),
SIPOutboundTrunkInfo,
)
async def update_sip_outbound_trunk_fields(
self,
trunk_id: str,
*,
address: str | None = None,
transport: SIPTransport | None = None,
numbers: list[str] | None = None,
auth_username: str | None = None,
auth_password: str | None = None,
name: str | None = None,
metadata: str | None = None,
) -> SIPOutboundTrunkInfo:
"""Updates specific fields of an existing SIP outbound trunk.
Only provided fields will be updated.
"""
update = SIPOutboundTrunkUpdate(
address=address,
transport=transport,
auth_username=auth_username,
auth_password=auth_password,
name=name,
metadata=metadata,
)
if numbers is not None:
update.numbers = ListUpdate(set=numbers)
return await self._client.request(
SVC,
"UpdateSIPOutboundTrunk",
UpdateSIPOutboundTrunkRequest(
sip_trunk_id=trunk_id,
update=update,
),
self._admin_headers(),
SIPOutboundTrunkInfo,
)
async def list_sip_inbound_trunk(
self, list: ListSIPInboundTrunkRequest
) -> ListSIPInboundTrunkResponse:
"""List SIP inbound trunks with optional filtering.
Args:
list: Request with optional filtering parameters
Returns:
Response containing list of SIP inbound trunks
"""
return await self._client.request(
SVC,
"ListSIPInboundTrunk",
list,
self._admin_headers(),
ListSIPInboundTrunkResponse,
)
async def list_sip_outbound_trunk(
self, list: ListSIPOutboundTrunkRequest
) -> ListSIPOutboundTrunkResponse:
"""List SIP outbound trunks with optional filtering.
Args:
list: Request with optional filtering parameters
Returns:
Response containing list of SIP outbound trunks
"""
return await self._client.request(
SVC,
"ListSIPOutboundTrunk",
list,
self._admin_headers(),
ListSIPOutboundTrunkResponse,
)
async def delete_sip_trunk(self, delete: DeleteSIPTrunkRequest) -> SIPTrunkInfo:
"""Delete a SIP trunk.
Args:
delete: Request containing trunk ID to delete
Returns:
Deleted trunk information
"""
return await self._client.request(
SVC,
"DeleteSIPTrunk",
delete,
self._admin_headers(),
SIPTrunkInfo,
)
async def create_sip_dispatch_rule(
self, create: CreateSIPDispatchRuleRequest
) -> SIPDispatchRuleInfo:
"""Create a new SIP dispatch rule.
Args:
create: Request containing rule details
Returns:
Created SIP dispatch rule
"""
return await self._client.request(
SVC,
"CreateSIPDispatchRule",
create,
self._admin_headers(),
SIPDispatchRuleInfo,
)
async def update_sip_dispatch_rule(
self,
rule_id: str,
rule: SIPDispatchRuleInfo,
) -> SIPDispatchRuleInfo:
"""Updates an existing SIP dispatch rule by replacing it entirely.
Args:
rule_id: ID of the SIP dispatch rule to update
rule: SIP dispatch rule to update with
Returns:
Updated SIP dispatch rule
"""
return await self._client.request(
SVC,
"UpdateSIPDispatchRule",
UpdateSIPDispatchRuleRequest(sip_dispatch_rule_id=rule_id, replace=rule),
self._admin_headers(),
SIPDispatchRuleInfo,
)
async def update_sip_dispatch_rule_fields(
self,
rule_id: str,
*,
trunk_ids: Optional[list[str]] = None,
rule: Optional[SIPDispatchRule] = None,
name: Optional[str] = None,
metadata: Optional[str] = None,
attributes: Optional[dict[str, str]] = None,
) -> SIPDispatchRuleInfo:
"""Updates specific fields of an existing SIP dispatch rule.
Only provided fields will be updated.
"""
update = SIPDispatchRuleUpdate(
name=name,
metadata=metadata,
rule=rule,
attributes=attributes,
trunk_ids=ListUpdate(set=trunk_ids) if trunk_ids else None,
)
return await self._client.request(
SVC,
"UpdateSIPDispatchRule",
UpdateSIPDispatchRuleRequest(sip_dispatch_rule_id=rule_id, update=update),
self._admin_headers(),
SIPDispatchRuleInfo,
)
async def list_sip_dispatch_rule(
self, list: ListSIPDispatchRuleRequest
) -> ListSIPDispatchRuleResponse:
"""List SIP dispatch rules with optional filtering.
Args:
list: Request with optional filtering parameters
Returns:
Response containing list of SIP dispatch rules
"""
return await self._client.request(
SVC,
"ListSIPDispatchRule",
list,
self._admin_headers(),
ListSIPDispatchRuleResponse,
)
async def delete_sip_dispatch_rule(
self, delete: DeleteSIPDispatchRuleRequest
) -> SIPDispatchRuleInfo:
"""Delete a SIP dispatch rule.
Args:
delete: Request containing rule ID to delete
Returns:
Deleted rule information
"""
return await self._client.request(
SVC,
"DeleteSIPDispatchRule",
delete,
self._admin_headers(),
SIPDispatchRuleInfo,
)
async def create_sip_participant(
self,
create: CreateSIPParticipantRequest,
*,
timeout: Optional[float] = None,
) -> SIPParticipantInfo:
"""Create a new SIP participant.
Args:
create: Request containing participant details
timeout: Optional request timeout in seconds
Returns:
Created SIP participant
Raises:
SIPError: If the SIP operation fails
"""
client_timeout: Optional[aiohttp.ClientTimeout] = None
if timeout:
# obay user specified timeout
client_timeout = aiohttp.ClientTimeout(total=timeout)
elif create.wait_until_answered:
# ensure default timeout isn't too short when using sync mode
if (
self._client._session.timeout
and self._client._session.timeout.total
and self._client._session.timeout.total < 20
):
client_timeout = aiohttp.ClientTimeout(total=20)
return await self._client.request(
SVC,
"CreateSIPParticipant",
create,
self._auth_header(VideoGrants(), sip=SIPGrants(call=True)),
SIPParticipantInfo,
timeout=client_timeout,
)
async def transfer_sip_participant(
self, transfer: TransferSIPParticipantRequest
) -> SIPParticipantInfo:
"""Transfer a SIP participant to a different room.
Args:
transfer: Request containing transfer details
Returns:
Updated SIP participant information
"""
return await self._client.request(
SVC,
"TransferSIPParticipant",
transfer,
self._auth_header(
VideoGrants(
room_admin=True,
room=transfer.room_name,
),
sip=SIPGrants(call=True),
),
SIPParticipantInfo,
)
def _admin_headers(self) -> dict[str, str]:
return self._auth_header(VideoGrants(), sip=SIPGrants(admin=True))