Skip to content

Commit 8988efd

Browse files
authored
fix: AS-86 Update constants http timeout (#26)
* Update constants.py * Refactor: Introduce DEFAULT_HTTP_TIMEOUT constant and update client timeout handling - Added DEFAULT_HTTP_TIMEOUT constant in constants.py for consistent timeout management. - Updated AiolaClient and related classes to utilize the new timeout constant. - Adjusted HTTP client instantiation in auth client to use DEFAULT_HTTP_TIMEOUT. - Updated .gitignore to include .vscode directory. These changes enhance code maintainability and ensure consistent timeout behavior across the SDK. * Refactor: Update transcribe_file to use environment variable for API key - Replaced hardcoded API key in transcribe_file.py with a call to os.getenv('AIOLA_API_KEY') for improved security and flexibility. - Removed the timeout parameter from AiolaClient instantiation to align with recent changes in client timeout handling. These updates enhance code maintainability and security by adhering to best practices for sensitive information management.
1 parent d77b5ed commit 8988efd

6 files changed

Lines changed: 24 additions & 15 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,4 @@ streamed_audio.wav
175175
example_output/
176176
.coveragerc
177177
.DS_Store
178+
.vscode

aiola/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .clients.auth.client import AsyncAuthClient, AuthClient
44
from .clients.stt.client import AsyncSttClient, SttClient
55
from .clients.tts.client import AsyncTtsClient, TtsClient
6-
from .constants import DEFAULT_AUTH_BASE_URL, DEFAULT_BASE_URL, DEFAULT_WORKFLOW_ID
6+
from .constants import DEFAULT_AUTH_BASE_URL, DEFAULT_BASE_URL, DEFAULT_HTTP_TIMEOUT, DEFAULT_WORKFLOW_ID
77
from .errors import AiolaError, AiolaValidationError
88
from .types import AiolaClientOptions, GrantTokenResponse, SessionCloseResponse
99

@@ -19,6 +19,7 @@ def __init__(
1919
base_url: str | None = None,
2020
auth_base_url: str | None = None,
2121
workflow_id: str = DEFAULT_WORKFLOW_ID,
22+
timeout: int = DEFAULT_HTTP_TIMEOUT,
2223
):
2324
# Initialize lazy-loaded clients
2425
self._stt: SttClient | None = None
@@ -32,6 +33,7 @@ def __init__(
3233
api_key=api_key,
3334
access_token=access_token,
3435
workflow_id=workflow_id,
36+
timeout=timeout,
3537
)
3638
except (ValueError, TypeError) as exc:
3739
raise AiolaValidationError(str(exc)) from exc

aiola/clients/auth/client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import httpx
99

10-
from ...constants import DEFAULT_HEADERS, HTTP_TIMEOUT
10+
from ...constants import DEFAULT_HEADERS, DEFAULT_HTTP_TIMEOUT
1111
from ...errors import AiolaError
1212
from ...types import AiolaClientOptions, GrantTokenResponse, SessionCloseResponse
1313

@@ -85,7 +85,7 @@ def grant_token(api_key: str, auth_base_url: str, workflow_id: str) -> GrantToke
8585
"Authorization": f"Bearer {api_key}",
8686
}
8787
# Generate temporary token
88-
with httpx.Client(timeout=HTTP_TIMEOUT) as client:
88+
with httpx.Client(timeout=DEFAULT_HTTP_TIMEOUT) as client:
8989
token_response = client.post(
9090
token_endpoint,
9191
headers=headers,
@@ -152,7 +152,7 @@ async def async_grant_token(api_key: str, auth_base_url: str, workflow_id: str)
152152
session_endpoint = f"{auth_base_url}/voip-auth/session"
153153

154154
# Generate temporary token
155-
async with httpx.AsyncClient(timeout=HTTP_TIMEOUT) as client:
155+
async with httpx.AsyncClient(timeout=DEFAULT_HTTP_TIMEOUT) as client:
156156
token_response = await client.post(
157157
token_endpoint, headers={**DEFAULT_HEADERS, "Authorization": f"Bearer {api_key}"}
158158
)
@@ -214,7 +214,7 @@ async def async_close_session(access_token: str, auth_base_url: str) -> SessionC
214214
auth_base_url = auth_base_url.rstrip("/")
215215
session_endpoint = f"{auth_base_url}/voip-auth/session"
216216

217-
async with httpx.AsyncClient(timeout=HTTP_TIMEOUT) as client:
217+
async with httpx.AsyncClient(timeout=DEFAULT_HTTP_TIMEOUT) as client:
218218
response = await client.delete(
219219
session_endpoint,
220220
headers={
@@ -283,7 +283,7 @@ def get_access_token(self, access_token: str, api_key: str, workflow_id: str) ->
283283
def _api_key_to_token(self, api_key: str) -> str:
284284
"""Generate a temporary JWT token from API key."""
285285
try:
286-
with httpx.Client(timeout=HTTP_TIMEOUT) as client:
286+
with httpx.Client(timeout=DEFAULT_HTTP_TIMEOUT) as client:
287287
response = client.post(
288288
f"{self._options.auth_base_url}/voip-auth/apiKey2Token",
289289
headers={**DEFAULT_HEADERS, "Authorization": f"Bearer {api_key}"},
@@ -316,7 +316,7 @@ def _create_session(self, token: str, workflow_id: str) -> dict[str, str]:
316316
body = {"workflow_id": workflow_id}
317317
headers = {**DEFAULT_HEADERS, "Content-Type": "application/json", "Authorization": f"Bearer {token}"}
318318

319-
with httpx.Client(timeout=HTTP_TIMEOUT) as client:
319+
with httpx.Client(timeout=DEFAULT_HTTP_TIMEOUT) as client:
320320
response = client.post(
321321
f"{self._options.auth_base_url}/voip-auth/session",
322322
headers=headers,
@@ -375,7 +375,7 @@ def close_session(access_token: str, auth_base_url: str) -> SessionCloseResponse
375375
auth_base_url = auth_base_url.rstrip("/")
376376
session_endpoint = f"{auth_base_url}/voip-auth/session"
377377

378-
with httpx.Client(timeout=HTTP_TIMEOUT) as client:
378+
with httpx.Client(timeout=DEFAULT_HTTP_TIMEOUT) as client:
379379
response = client.delete(
380380
session_endpoint,
381381
headers={
@@ -516,7 +516,7 @@ async def get_access_token(self, access_token: str, api_key: str, workflow_id: s
516516
async def _api_key_to_token(self, api_key: str) -> str:
517517
"""Generate a temporary JWT token from API key."""
518518
try:
519-
async with httpx.AsyncClient(timeout=HTTP_TIMEOUT) as client:
519+
async with httpx.AsyncClient(timeout=DEFAULT_HTTP_TIMEOUT) as client:
520520
response = await client.post(
521521
f"{self._options.auth_base_url}/voip-auth/apiKey2Token",
522522
headers={**DEFAULT_HEADERS, "Authorization": f"Bearer {api_key}"},
@@ -548,7 +548,7 @@ async def _create_session(self, token: str, workflow_id: str) -> dict[str, str]:
548548
try:
549549
body = {"workflow_id": workflow_id}
550550

551-
async with httpx.AsyncClient(timeout=HTTP_TIMEOUT) as client:
551+
async with httpx.AsyncClient(timeout=DEFAULT_HTTP_TIMEOUT) as client:
552552
response = await client.post(
553553
f"{self._options.auth_base_url}/voip-auth/session",
554554
headers={**DEFAULT_HEADERS, "Content-Type": "application/json", "Authorization": f"Bearer {token}"},

aiola/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"User-Agent": "@aiola/aiola-python",
66
}
77

8-
HTTP_TIMEOUT = 60
8+
DEFAULT_HTTP_TIMEOUT = 150
99

1010
DEFAULT_WORKFLOW_ID = "2c78fcf1-9265-408f-b8c3-d9d7e7ebc1bc"

aiola/http_client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import httpx
77

8-
from .constants import DEFAULT_BASE_URL, DEFAULT_HEADERS, HTTP_TIMEOUT
8+
from .constants import DEFAULT_BASE_URL, DEFAULT_HEADERS
99
from .errors import AiolaError
1010

1111
if TYPE_CHECKING:
@@ -34,8 +34,9 @@ def create_authenticated_client(options: AiolaClientOptions, auth: AuthClient) -
3434

3535
# Create client with base URL and headers
3636
full_base_url = (options.base_url or DEFAULT_BASE_URL).rstrip("/")
37+
timeout = options.timeout
3738

38-
return httpx.Client(base_url=full_base_url, headers=headers, timeout=HTTP_TIMEOUT)
39+
return httpx.Client(base_url=full_base_url, headers=headers, timeout=timeout)
3940
except Exception as exc:
4041
raise AiolaError("Failed to create authenticated HTTP client") from exc
4142

@@ -53,7 +54,8 @@ async def create_async_authenticated_client(options: AiolaClientOptions, auth: A
5354

5455
# Create client with base URL and headers
5556
full_base_url = (options.base_url or DEFAULT_BASE_URL).rstrip("/")
57+
timeout = options.timeout
5658

57-
return httpx.AsyncClient(base_url=full_base_url, headers=headers, timeout=HTTP_TIMEOUT)
59+
return httpx.AsyncClient(base_url=full_base_url, headers=headers, timeout=timeout)
5860
except Exception as exc:
5961
raise AiolaError("Failed to create authenticated async HTTP client") from exc

aiola/types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from dataclasses import dataclass
66
from typing import IO, TypedDict, Union
77

8-
from .constants import DEFAULT_AUTH_BASE_URL, DEFAULT_BASE_URL, DEFAULT_WORKFLOW_ID
8+
from .constants import DEFAULT_AUTH_BASE_URL, DEFAULT_BASE_URL, DEFAULT_HTTP_TIMEOUT, DEFAULT_WORKFLOW_ID
99

1010

1111
@dataclass
@@ -17,6 +17,7 @@ class AiolaClientOptions:
1717
api_key: str | None = None
1818
access_token: str | None = None
1919
workflow_id: str = DEFAULT_WORKFLOW_ID
20+
timeout: float | None = DEFAULT_HTTP_TIMEOUT
2021

2122
def __post_init__(self) -> None:
2223
"""Validate options after initialization."""
@@ -38,6 +39,9 @@ def __post_init__(self) -> None:
3839
if not isinstance(self.workflow_id, str):
3940
raise TypeError("Workflow ID must be a string")
4041

42+
if self.timeout is not None and not isinstance(self.timeout, (int | float)):
43+
raise TypeError("Timeout must be a number")
44+
4145

4246
class LiveEvents(str, enum.Enum):
4347
Transcript = "transcript"

0 commit comments

Comments
 (0)