o
    iB                     @   s   d dl Z d dlZd dlmZmZ d dlm  mZ	 ddl
mZ ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZ zd dlmZ W n e yf   d dl!mZ Y nw G dd dZ"G dd dZ#dS )    N)asynccontextmanagercontextmanager   )ApiError)AsyncClientWrapperSyncClientWrapper)jsonable_encoder)encode_query)remove_none_from_dict)RequestOptions)InvalidWebSocketStatusget_status_code   )AsyncV1SocketClientV1SocketClient)connectc                   @   ~   e Zd ZdefddZeddddddddeje deje deje d	eje d
eje deje	 dej
e fddZdS )RawV1Clientclient_wrapperc                C   
   || _ d S N_client_wrapperselfr    r   P/home/ubuntu/.local/lib/python3.10/site-packages/deepgram/speak/v1/raw_client.py__init__      
zRawV1Client.__init__Nencodingmip_opt_outmodelsample_rateauthorizationrequest_optionsr    r!   r"   r#   r$   r%   returnc             
   c   s*   | j  jd }ttt||||d|dur|di pi ni }|r0|d tj	| }| j 
 }	|dur?t||	d< |rLd|v rL|	|d  z"tj||	d}
t|
d	V  W d   W dS 1 sgw   Y  W dS  ty } zt|}|d
krt|t|	ddt|t|	ddd}~ww )a  
        Convert text into natural-sounding speech using Deepgram's TTS WebSocket

        Parameters
        ----------
        encoding : typing.Optional[str]

        mip_opt_out : typing.Optional[str]

        model : typing.Optional[str]

        sample_rate : typing.Optional[str]

        authorization : typing.Optional[str]
            Use your API key for authentication, or alternatively generate a [temporary token](/guides/fundamentals/token-based-authentication) and pass it via the `token` query parameter.

            **Example:** `token %DEEPGRAM_API_KEY%` or `bearer %DEEPGRAM_TOKEN%`

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        V1SocketClient
        	/v1/speakr    r!   r"   r#   Nadditional_query_parameters?Authorizationadditional_headers)r,   	websocket  /Websocket initialized with invalid credentials.status_codeheadersbody8Unexpected error when initializing websocket connection.)r   get_environment
productionr	   r   r
   geturllibparse	urlencodeget_headersstrupdatewebsockets_sync_clientr   r   r   r   r   dictr   r    r!   r"   r#   r$   r%   ws_url_encoded_query_paramsr3   protocolexcr2   r   r   r   r      sX   $
&zRawV1Client.connect)__name__
__module____qualname__r   r   r   typingOptionalr=   r   Iteratorr   r   r   r   r   r   r      0    	r   c                   @   r   )AsyncRawV1Clientr   c                C   r   r   r   r   r   r   r   r   k   r   zAsyncRawV1Client.__init__Nr   r    r!   r"   r#   r$   r%   r&   c             
   C  s<  | j  jd }ttt||||d|dur|di pi ni }|r0|d tj	| }| j 
 }	|dur?t||	d< |rLd|v rL|	|d  z+t||	d4 I dH }
t|
d	V  W d  I dH  W dS 1 I dH spw   Y  W dS  ty } zt|}|d
krt|t|	ddt|t|	ddd}~ww )a  
        Convert text into natural-sounding speech using Deepgram's TTS WebSocket

        Parameters
        ----------
        encoding : typing.Optional[str]

        mip_opt_out : typing.Optional[str]

        model : typing.Optional[str]

        sample_rate : typing.Optional[str]

        authorization : typing.Optional[str]
            Use your API key for authentication, or alternatively generate a [temporary token](/guides/fundamentals/token-based-authentication) and pass it via the `token` query parameter.

            **Example:** `token %DEEPGRAM_API_KEY%` or `bearer %DEEPGRAM_TOKEN%`

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncV1SocketClient
        r'   r(   Nr)   r*   r+   r,   )extra_headersr-   r/   r0   r1   r5   )r   r6   r7   r	   r   r
   r8   r9   r:   r;   r<   r=   r>   websockets_client_connectr   r   r   r   r@   rA   r   r   r   r   n   sX   $
2zAsyncRawV1Client.connect)rF   rG   rH   r   r   r   rI   rJ   r=   r   AsyncIteratorr   r   r   r   r   r   rM   j   rL   rM   )$rI   urllib.parser9   
contextlibr   r   websockets.sync.clientsyncclientr?   core.api_errorr   core.client_wrapperr   r   core.jsonable_encoderr   core.query_encoderr	   core.remove_none_from_dictr
   core.request_optionsr   core.websocket_compatr   r   socket_clientr   r   websockets.legacy.clientr   rO   ImportError
websocketsr   rM   r   r   r   r   <module>   s&   S