|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import aiohttp |
| 4 | +from typing import Optional |
4 | 5 |
|
5 | 6 | from livekit.protocol.connector_whatsapp import ( |
6 | 7 | DialWhatsAppCallRequest, |
|
17 | 18 | ConnectTwilioCallResponse, |
18 | 19 | ) |
19 | 20 | from ._service import Service |
| 21 | +from ._dial_timeout import dial_timeout |
20 | 22 | from .access_token import VideoGrants |
21 | 23 |
|
22 | 24 | SVC = "Connector" |
@@ -106,23 +108,39 @@ async def connect_whatsapp_call( |
106 | 108 | ) |
107 | 109 |
|
108 | 110 | async def accept_whatsapp_call( |
109 | | - self, request: AcceptWhatsAppCallRequest |
| 111 | + self, |
| 112 | + request: AcceptWhatsAppCallRequest, |
| 113 | + *, |
| 114 | + timeout: Optional[float] = None, |
110 | 115 | ) -> AcceptWhatsAppCallResponse: |
111 | 116 | """ |
112 | 117 | Accept an inbound WhatsApp call |
113 | 118 |
|
114 | 119 | Args: |
115 | 120 | request: AcceptWhatsAppCallRequest containing call parameters and SDP |
| 121 | + timeout: Optional request timeout in seconds. When the request waits |
| 122 | + for an answer (wait_until_answered), it defaults to a longer value |
| 123 | + (dialing takes time) and is raised, if needed, to stay above the |
| 124 | + request's ringing_timeout. |
116 | 125 |
|
117 | 126 | Returns: |
118 | 127 | AcceptWhatsAppCallResponse with the room name |
119 | 128 | """ |
| 129 | + client_timeout: Optional[aiohttp.ClientTimeout] = None |
| 130 | + if request.wait_until_answered: |
| 131 | + # Waiting for the call to be answered dials a phone, which takes |
| 132 | + # longer than a normal request and must outlast ringing. |
| 133 | + client_timeout = aiohttp.ClientTimeout(total=dial_timeout(timeout, request)) |
| 134 | + elif timeout: |
| 135 | + client_timeout = aiohttp.ClientTimeout(total=timeout) |
| 136 | + |
120 | 137 | return await self._client.request( |
121 | 138 | SVC, |
122 | 139 | "AcceptWhatsAppCall", |
123 | 140 | request, |
124 | 141 | self._auth_header(VideoGrants(room_create=True)), |
125 | 142 | AcceptWhatsAppCallResponse, |
| 143 | + timeout=client_timeout, |
126 | 144 | ) |
127 | 145 |
|
128 | 146 | async def connect_twilio_call( |
|
0 commit comments