22# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
33# SPDX-License-Identifier: MIT
44
5- from .enums import LiveTranscriptionEvents
6- from .helpers import convert_to_websocket_url , append_query_params
5+ from .v1_client import LiveClientV1
6+ from .v1_options import LiveOptionsV1
77
8- from deepgram .errors import DeepgramApiError
8+ '''
9+ The client.py points to the current supported version in the SDK.
10+ Older versions are supported in the SDK for backwards compatibility.
11+ '''
12+ class LiveOptions (LiveOptionsV1 ):
13+ pass
914
10- import asyncio
11- import json
12- import websockets
13-
14- class LiveClient :
15+ class LiveClient (LiveClientV1 ):
1516 """
16- Client for interacting with Deepgram's live transcription services over WebSockets.
17-
18- This class provides methods to establish a WebSocket connection for live transcription and handle real-time transcription events.
19-
20- Args:
21- base_url (str): The base URL for WebSocket connection.
22- api_key (str): The Deepgram API key used for authentication.
23- headers (dict): Additional HTTP headers for WebSocket connection.
24-
25- Attributes:
26- endpoint (str): The API endpoint for live transcription.
27- _socket (websockets.WebSocketClientProtocol): The WebSocket connection object.
28- _event_handlers (dict): Dictionary of event handlers for specific events.
29- websocket_url (str): The WebSocket URL used for connection.
30-
31- Methods:
32- __call__: Establishes a WebSocket connection for live transcription.
33- on: Registers event handlers for specific events.
34- send: Sends data over the WebSocket connection.
35- finish: Closes the WebSocket connection gracefully.
17+ Please see LiveClientV1 for details
3618 """
3719 def __init__ (self , base_url , api_key , headers ):
38- self .base_url = base_url
39- self .api_key = api_key
40- self .headers = headers
41- self .endpoint = "v1/listen"
42- self ._socket = None
43- self ._event_handlers = { event : [] for event in LiveTranscriptionEvents }
44- self .websocket_url = convert_to_websocket_url (base_url , self .endpoint )
45-
46- async def __call__ (self , options = None ):
47- url_with_params = append_query_params (self .websocket_url , options )
48- try :
49- self ._socket = await _socket_connect (url_with_params , self .headers )
50- asyncio .create_task (self ._start ())
51- return self
52- except websockets .ConnectionClosed as e :
53- await self ._emit (LiveTranscriptionEvents .Close , e .code )
54-
55-
56- def on (self , event , handler ): # registers event handlers for specific events
57- if event in LiveTranscriptionEvents and callable (handler ):
58- self ._event_handlers [event ].append (handler )
59-
60- async def _emit (self , event , * args , ** kwargs ): # triggers the registered event handlers for a specific event
61- for handler in self ._event_handlers [event ]:
62- handler (* args , ** kwargs )
63-
64- async def _start (self ) -> None :
65- async for message in self ._socket :
66- try :
67- data = json .loads (message )
68- response_type = data .get ("type" )
69- if response_type == LiveTranscriptionEvents .Transcript .value :
70- await self ._emit (LiveTranscriptionEvents .Transcript , data )
71- if "metadata" in data :
72- await self ._emit (LiveTranscriptionEvents .Metadata , data ["metadata" ])
73- except json .JSONDecodeError as e :
74- await self ._emit (LiveTranscriptionEvents .Error , e .code )
75-
76- async def send (self , data ):
77- if self ._socket :
78- await self ._socket .send (data )
79-
80- async def finish (self ):
81- if self ._socket :
82- await self ._socket .send (json .dumps ({"type" : "CloseStream" }))
83- # await self._socket.send("") # Send a zero-byte message
84- await self ._socket .wait_closed ()
85-
86- async def _socket_connect (websocket_url , headers ):
87- destination = websocket_url
88- updated_headers = headers
89-
90- async def attempt ():
91- try :
92- return await websockets .connect (
93- destination , extra_headers = updated_headers , ping_interval = 5
94- )
95- except websockets .exceptions .InvalidHandshake as exc :
96- raise DeepgramApiError (exc , http_library_error = exc ) from exc
97-
98- # tries = 4
99- # while tries > 0:
100- # try:
101- # return await attempt()
102- # except Exception as exc:
103- # tries -= 1
104- # continue
105- return await attempt ()
20+ super ().__init__ (base_url , api_key , headers )
21+
0 commit comments