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)AsyncSettingsClientSettingsClient)connectc                   @  F   e Zd ZdddZedddZe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	_settingsselfr    r"   L/home/ubuntu/.local/lib/python3.10/site-packages/deepgram/agent/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authorizationrequest_optionsr-   typing.Optional[str]r.   typing.Optional[RequestOptions]typing.Iterator[V1SocketClient]c          	   
   c  s$   | j j jd }ttti |dur|di pi ni }|r,|d tj	
| }| j j }|dur<t||d< |rId|v rI||d  z"tj||d}t|dV  W d   W dS 1 sdw   Y  W dS  ty } zt|}|d	krt|t|d
dt|t|ddd}~ww )a]  
        Build a conversational voice agent using Deepgram's Voice Agent WebSocket

        Parameters
        ----------
        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/agent/converseNadditional_query_parameters?Authorizationadditional_headers)r6   	websocket  /Websocket initialized with invalid credentials.status_codeheadersbody8Unexpected error when initializing websocket connection.)r   r   get_environmentagentr
   r	   r   geturllibparse	urlencodeget_headersstrupdatewebsockets_sync_clientr   r   r   r   r   dict	r!   r-   r.   ws_url_encoded_query_paramsr=   protocolexcr<   r"   r"   r#   r   .   sP   &zV1Client.connectc                 C  *   | j d u rddlm} || jd| _ | j S )Nr   )r   r   )r   settings.clientr   r   )r!   r   r"   r"   r#   settingsi      
zV1Client.settings)r   r   )r&   r   )r-   r/   r.   r0   r&   r1   )	__name__
__module____qualname__r$   propertyr*   r   r   rR   r"   r"   r"   r#   r          

:r   c                   @  r   )AsyncV1Clientr   r   c                C  r   r   )r   r   r   r   r    r"   r"   r#   r$   s   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*   x   r+   zAsyncV1Client.with_raw_responseNr,   r-   r/   r.   r0   )typing.AsyncIterator[AsyncV1SocketClient]c          	   
   C s6  | j j jd }ttti |dur|di pi ni }|r,|d tj	
| }| j j }|dur<t||d< |rId|v rI||d  z+t||d4 I dH }t|dV  W d  I dH  W dS 1 I dH smw   Y  W dS  ty } zt|}|d	krt|t|d
dt|t|ddd}~ww )ab  
        Build a conversational voice agent using Deepgram's Voice Agent WebSocket

        Parameters
        ----------
        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
        r2   Nr3   r4   r5   r6   )extra_headersr7   r9   r:   r;   r?   )r   r   r@   rA   r
   r	   r   rB   rC   rD   rE   rF   rG   rH   websockets_client_connectr   r   r   r   rJ   rK   r"   r"   r#   r      sP   2zAsyncV1Client.connectc                 C  rP   )Nr   )r   r   )r   rQ   r   r   )r!   r   r"   r"   r#   rR      rS   zAsyncV1Client.settings)r   r   )r&   r   )r-   r/   r.   r0   r&   rZ   )	rT   rU   rV   r$   rW   r*   r   r   rR   r"   r"   r"   r#   rY   r   rX   rY   )-
__future__r   typingurllib.parserC   
contextlibr   r   websockets.sync.clientsyncclientrI   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_CHECKINGrQ   r   r   websockets.legacy.clientr   r\   ImportError
websocketsr   rY   r"   r"   r"   r#   <module>   s.   U