o
    i
"                     @  s   d dl mZ 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 ddlm Z m!Z! ej"rhddl#m$Z$m%Z% zd dl&m'Z( W n e)y   d dl*m'Z( Y nw G dd dZ+G dd dZ,dS )    )annotationsN)asynccontextmanagercontextmanager   )ApiError)AsyncClientWrapperSyncClientWrapper)jsonable_encoder)encode_query)remove_none_from_dict)RequestOptions)InvalidWebSocketStatusget_status_code   )AsyncRawV1ClientRawV1Client)AsyncV1SocketClientV1SocketClient)AsyncAudioClientAudioClient)connectc                   @  N   e Zd ZdddZedddZed	d	d	d	d	d	d
dddZedd Zd	S )V1Clientclient_wrapperr   c                C     t |d| _|| _d | _d S Nr   )r   _raw_client_client_wrapper_audioselfr    r"   L/home/ubuntu/.local/lib/python3.10/site-packages/deepgram/speak/v1/client.py__init__      
zV1Client.__init__returnr   c                 C     | j S )z
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawV1Client
        r   r!   r"   r"   r#   with_raw_response#      	zV1Client.with_raw_responseNencodingmip_opt_outmodelsample_rateauthorizationrequest_optionsr-   typing.Optional[str]r.   r/   r0   r1   r2   typing.Optional[RequestOptions]typing.Iterator[V1SocketClient]c             
   c  s.   | j j jd }ttt||||d|dur|di pi ni }|r1|d tj	
| }| j j }	|durAt||	d< |rNd|v rN|	|d  z"tj||	d}
t|
d	V  W d   W dS 1 siw   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/   r0   Nadditional_query_parameters?Authorizationadditional_headers)r;   	websocket  /Websocket initialized with invalid credentials.status_codeheadersbody8Unexpected error when initializing websocket connection.)r   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/   r0   r1   r2   ws_url_encoded_query_paramsrB   protocolexcrA   r"   r"   r#   r   .   sX   $&zV1Client.connectc                 C  *   | j d u rddlm} || jd| _ | j S )Nr   )r   r   )r   audio.clientr   r   )r!   r   r"   r"   r#   audio|      
zV1Client.audio)r   r   )r&   r   )r-   r3   r.   r3   r/   r3   r0   r3   r1   r3   r2   r4   r&   r5   )	__name__
__module____qualname__r$   propertyr*   r   r   rW   r"   r"   r"   r#   r          

Mr   c                   @  r   )AsyncV1Clientr   r   c                C  r   r   )r   r   r   r   r    r"   r"   r#   r$      r%   zAsyncV1Client.__init__r&   r   c                 C  r'   )z
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawV1Client
        r(   r)   r"   r"   r#   r*      r+   zAsyncV1Client.with_raw_responseNr,   r-   r3   r.   r/   r0   r1   r2   r4   )typing.AsyncIterator[AsyncV1SocketClient]c             
   C s@  | j j jd }ttt||||d|dur|di pi ni }|r1|d tj	
| }| j j }	|durAt||	d< |rNd|v rN|	|d  z+t||	d4 I dH }
t|
d	V  W d  I dH  W dS 1 I dH srw   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
        r6   r7   Nr8   r9   r:   r;   )extra_headersr<   r>   r?   r@   rD   )r   r   rE   rF   r
   r	   r   rG   rH   rI   rJ   rK   rL   rM   websockets_client_connectr   r   r   r   rO   rP   r"   r"   r#   r      sX   $2zAsyncV1Client.connectc                 C  rU   )Nr   )r   r   )r   rV   r   r   )r!   r   r"   r"   r#   rW      rX   zAsyncV1Client.audio)r   r   )r&   r   )r-   r3   r.   r3   r/   r3   r0   r3   r1   r3   r2   r4   r&   r_   )	rY   rZ   r[   r$   r\   r*   r   r   rW   r"   r"   r"   r#   r^      r]   r^   )-
__future__r   typingurllib.parserH   
contextlibr   r   websockets.sync.clientsyncclientrN   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   
raw_clientr   r   socket_clientr   r   TYPE_CHECKINGrV   r   r   websockets.legacy.clientr   ra   ImportError
websocketsr   r^   r"   r"   r"   r#   <module>   s.   h