Skip to content

Commit c3178fb

Browse files
authored
Add support for custom ssl context in rt sdk (#66)
1 parent 505d999 commit c3178fb

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

sdk/rt/speechmatics/rt/_auth.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ class StaticKeyAuth(AuthBase):
4444
def __init__(self, api_key: Optional[str] = None):
4545
self._api_key = api_key or os.environ.get("SPEECHMATICS_API_KEY")
4646

47-
if not self._api_key:
48-
raise ValueError("API key required: provide api_key or set SPEECHMATICS_API_KEY")
49-
5047
async def get_auth_headers(self) -> dict[str, str]:
5148
return {"Authorization": f"Bearer {self._api_key}"}
5249

sdk/rt/speechmatics/rt/_models.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import ssl
34
from dataclasses import asdict
45
from dataclasses import dataclass
56
from dataclasses import field
@@ -462,9 +463,9 @@ class ConnectionConfig:
462463
close_timeout: Timeout for closing WebSocket connection.
463464
max_size: Maximum message size in bytes.
464465
max_queue: Maximum number of messages in receive queue.
465-
read_limit: Maximum number of bytes to read from WebSocket.
466-
write_limit: Maximum number of bytes to write to WebSocket.
467-
466+
read_limit: Maximum number of bytes to read from WebSocket (legacy websockets only).
467+
write_limit: Maximum number of bytes to write to WebSocket (legacy websockets only).
468+
ssl_context: SSL context for the WebSocket connection.
468469
Returns:
469470
Websocket connection configuration as a dict while excluding None values.
470471
"""
@@ -477,9 +478,29 @@ class ConnectionConfig:
477478
max_queue: Optional[int] = None
478479
read_limit: Optional[int] = None
479480
write_limit: Optional[int] = None
481+
ssl_context: ssl.SSLContext = field(default_factory=ssl.create_default_context)
480482

481483
def to_dict(self) -> dict[str, Any]:
482-
return asdict(self, dict_factory=lambda x: {k: v for (k, v) in x if v is not None})
484+
"""Convert to dict, excluding ssl field to avoid pickle errors."""
485+
result = {}
486+
if self.open_timeout is not None:
487+
result["open_timeout"] = self.open_timeout
488+
if self.ping_interval is not None:
489+
result["ping_interval"] = self.ping_interval
490+
if self.ping_timeout is not None:
491+
result["ping_timeout"] = self.ping_timeout
492+
if self.close_timeout is not None:
493+
result["close_timeout"] = self.close_timeout
494+
if self.max_size is not None:
495+
result["max_size"] = self.max_size
496+
if self.max_queue is not None:
497+
result["max_queue"] = self.max_queue
498+
if self.read_limit is not None:
499+
result["read_limit"] = self.read_limit
500+
if self.write_limit is not None:
501+
result["write_limit"] = self.write_limit
502+
503+
return result
483504

484505

485506
@dataclass

sdk/rt/speechmatics/rt/_transport.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
from websockets.asyncio.client import connect
2525

2626
WS_HEADERS_KEY = "additional_headers"
27+
IS_LEGACY_WEBSOCKETS = False
2728
except ImportError:
2829
# Fall back to legacy websockets
2930
from websockets.legacy.client import WebSocketClientProtocol
3031
from websockets.legacy.client import connect # type: ignore
3132

3233
WS_HEADERS_KEY = "extra_headers"
34+
IS_LEGACY_WEBSOCKETS = True
3335

3436

3537
class Transport:
@@ -116,8 +118,14 @@ async def connect(self, ws_headers: Optional[dict] = None) -> None:
116118
ws_kwargs: dict = {
117119
WS_HEADERS_KEY: ws_headers,
118120
**self._conn_config.to_dict(),
121+
"ssl": self._conn_config.ssl_context,
119122
}
120123

124+
# Filter out parameters not supported by new websockets >=13.0
125+
if not IS_LEGACY_WEBSOCKETS:
126+
ws_kwargs.pop("read_limit", None)
127+
ws_kwargs.pop("write_limit", None)
128+
121129
self._websocket = await connect(
122130
url_with_params,
123131
**ws_kwargs,

0 commit comments

Comments
 (0)