3333logger = logging .getLogger (__name__ )
3434
3535
36- def _user_agent () -> str :
37- vi = sys .version_info
38- python_version = f"{ vi .major } .{ vi .minor } .{ vi .micro } "
39- return (
40- f"AssemblyAI/1.0 (sdk=Python/{ __version__ } runtime_env=Python/{ python_version } )"
41- )
42-
43-
4436def _dump_model (model : BaseModel ):
4537 if hasattr (model , "model_dump" ):
4638 return model .model_dump (exclude_none = True )
@@ -53,10 +45,20 @@ def _dump_model_json(model: BaseModel):
5345 return model .json (exclude_none = True )
5446
5547
48+ def _user_agent () -> str :
49+ vi = sys .version_info
50+ python_version = f"{ vi .major } .{ vi .minor } .{ vi .micro } "
51+ return (
52+ f"AssemblyAI/1.0 (sdk=Python/{ __version__ } runtime_env=Python/{ python_version } )"
53+ )
54+
55+
5656class StreamingClient :
5757 def __init__ (self , options : StreamingClientOptions ):
5858 self ._options = options
5959
60+ self ._client = _HTTPClient (api_host = options .api_host , api_key = options .api_key )
61+
6062 self ._handlers : Dict [StreamingEvents , List [Callable ]] = {}
6163
6264 for event in StreamingEvents .__members__ .values ():
@@ -73,7 +75,9 @@ def connect(self, params: StreamingParameters) -> None:
7375
7476 uri = f"wss://{ self ._options .api_host } /v3/ws?{ params_encoded } "
7577 headers = {
76- "Authorization" : self ._options .api_key ,
78+ "Authorization" : self ._options .token
79+ if self ._options .token
80+ else self ._options .api_key ,
7781 "User-Agent" : _user_agent (),
7882 "AssemblyAI-Version" : "2025-05-12" ,
7983 }
@@ -253,14 +257,44 @@ def _parse_error(
253257 message = f"Unknown error: { error } " ,
254258 )
255259
260+ def create_temporary_token (
261+ self ,
262+ expires_in_seconds : int ,
263+ max_session_duration_seconds : int ,
264+ ) -> str :
265+ return self ._client .create_temporary_token (
266+ expires_in_seconds = expires_in_seconds ,
267+ max_session_duration_seconds = max_session_duration_seconds ,
268+ )
256269
257- class HTTPClient :
258- def __init__ (self , options : StreamingClientOptions ):
259- headers = {
260- "Authorization" : options .api_key ,
261- "User-Agent" : _user_agent (),
262- }
263270
264- base_url = f"https://{ options .api_host } "
271+ class _HTTPClient :
272+ def __init__ (self , api_host : str , api_key : Optional [str ] = None ):
273+ vi = sys .version_info
274+ python_version = f"{ vi .major } .{ vi .minor } .{ vi .micro } "
275+ user_agent = f"{ httpx ._client .USER_AGENT } AssemblyAI/1.0 (sdk=Python/{ __version__ } runtime_env=Python/{ python_version } )"
276+
277+ headers = {"User-Agent" : user_agent }
265278
266- self ._http_client = httpx .Client (base_url = base_url , headers = headers , timeout = 30 )
279+ if api_key :
280+ headers ["Authorization" ] = api_key
281+
282+ self ._http_client = httpx .Client (
283+ base_url = "https://" + api_host ,
284+ headers = headers ,
285+ )
286+
287+ def create_temporary_token (
288+ self ,
289+ expires_in_seconds : int ,
290+ max_session_duration_seconds : int ,
291+ ) -> str :
292+ response = self ._http_client .get (
293+ "/v3/token" ,
294+ params = {
295+ "expires_in" : expires_in_seconds ,
296+ "max_session_duration" : max_session_duration_seconds ,
297+ },
298+ )
299+ response .raise_for_status ()
300+ return response .json ()["token" ]
0 commit comments