1111from ..helpers import convert_to_websocket_url , append_query_params
1212from ..errors import DeepgramError
1313
14+ from .response import LiveResultResponse , MetadataResponse , ErrorResponse
1415from .options import LiveOptions
1516
1617
@@ -38,14 +39,14 @@ def __init__(self, config: DeepgramClientOptions):
3839 self ._event_handlers = {event : [] for event in LiveTranscriptionEvents }
3940 self .websocket_url = convert_to_websocket_url (self .config .url , self .endpoint )
4041
41- async def __call__ (self , options : LiveOptions = None ):
42- """
43- Establishes a WebSocket connection for live transcription.
44- """
45- self .logger .debug ("AsyncLiveClient.__call__ ENTER" )
46- self .logger .info ("options: %s" , options )
42+ async def start (self , options : LiveOptions = None , ** kwargs ):
43+ self .logger .debug ("AsyncLiveClient.start ENTER" )
44+ self .logger .info ("kwargs: %s" , options )
45+ self .logger .info ("options: %s" , kwargs )
4746
4847 self .options = options
48+ self .kwargs = kwargs
49+
4950 if isinstance (options , LiveOptions ):
5051 self .logger .info ("LiveOptions switching class -> json" )
5152 self .options = self .options .to_dict ()
@@ -55,13 +56,13 @@ async def __call__(self, options: LiveOptions = None):
5556 self ._socket = await _socket_connect (url_with_params , self .config .headers )
5657 asyncio .create_task (self ._start ())
5758
58- self .logger .notice ("__call__ succeeded" )
59- self .logger .debug ("AsyncLiveClient.__call__ LEAVE" )
59+ self .logger .notice ("start succeeded" )
60+ self .logger .debug ("AsyncLiveClient.start LEAVE" )
6061 return self
6162 except websockets .ConnectionClosed as e :
6263 await self ._emit (LiveTranscriptionEvents .Close , e .code )
6364 self .logger .notice ("exception: websockets.ConnectionClosed" )
64- self .logger .debug ("AsyncLiveClient.__call__ LEAVE" )
65+ self .logger .debug ("AsyncLiveClient.start LEAVE" )
6566
6667 def on (self , event , handler ):
6768 """
@@ -74,7 +75,7 @@ async def _emit(
7475 self , event , * args , ** kwargs
7576 ): # triggers the registered event handlers for a specific event
7677 for handler in self ._event_handlers [event ]:
77- handler (* args , ** kwargs )
78+ handler (self , * args , ** kwargs )
7879
7980 async def _start (self ) -> None :
8081 self .logger .debug ("AsyncLiveClient._start ENTER" )
@@ -85,25 +86,45 @@ async def _start(self) -> None:
8586 response_type = data .get ("type" )
8687 match response_type :
8788 case LiveTranscriptionEvents .Transcript .value :
88- self .logger .verbose (
89+ self .logger .debug (
8990 "response_type: %s, data: %s" , response_type , data
9091 )
91- await self ._emit (LiveTranscriptionEvents .Transcript , data )
92- case LiveTranscriptionEvents .Error .value :
93- self .logger .verbose (
94- "response_type: %s, data: %s" , response_type , data
92+ result = LiveResultResponse .from_json (message )
93+ await self ._emit (
94+ LiveTranscriptionEvents .Transcript ,
95+ result = result ,
96+ kwargs = self .kwargs ,
9597 )
96- await self ._emit (LiveTranscriptionEvents .Error , data )
9798 case LiveTranscriptionEvents .Metadata .value :
98- self .logger .verbose (
99+ self .logger .debug (
99100 "response_type: %s, data: %s" , response_type , data
100101 )
101- await self ._emit (LiveTranscriptionEvents .Metadata , data )
102+ result = ErrorResponse .from_json (message )
103+ await self ._emit (
104+ LiveTranscriptionEvents .Metadata ,
105+ metadata = result ,
106+ kwargs = self .kwargs ,
107+ )
108+ case LiveTranscriptionEvents .Error .value :
109+ self .logger .debug (
110+ "response_type: %s, data: %s" , response_type , data
111+ )
112+ result = MetadataResponse .from_json (message )
113+ await self ._emit (
114+ LiveTranscriptionEvents .Error ,
115+ error = result ,
116+ kwargs = self .kwargs ,
117+ )
102118 case _:
103119 self .logger .error (
104120 "response_type: %s, data: %s" , response_type , data
105121 )
106- await self ._emit (LiveTranscriptionEvents .Error , data )
122+ error = ErrorResponse (
123+ type = "UnhandledMessage" ,
124+ description = "Unknown message type" ,
125+ message = f"Unhandle message type: { response_type } " ,
126+ )
127+ await self ._emit (LiveTranscriptionEvents .Error , error = error )
107128 except json .JSONDecodeError as e :
108129 await self ._emit (LiveTranscriptionEvents .Error , e .code )
109130 self .logger .error ("exception: json.JSONDecodeError: %s" , str (e ))
0 commit comments