o
    i                     @   s   U d Z ddlZddlZddlmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZ g dZg dZi Ze	ee	eef f ed	< dae
e ed
< dae
e ed< G dd dZG dd dZdddde
e de
e ddfddZdddZdS )aa  Custom WebSocket transport support for the Deepgram Python SDK.

Allows users to swap in custom transports (BiDi/SSE, test doubles, proxied
connections) by providing a ``transport_factory`` when constructing a
:class:`DeepgramClient` or :class:`AsyncDeepgramClient`.

A transport factory is a callable: ``factory(url, headers)`` that returns a
context manager (sync) or async context manager (async) yielding a transport
object with ``send()``, ``recv()``, and iteration support.

Usage::

    from deepgram import DeepgramClient

    client = DeepgramClient(api_key="...", transport_factory=MySyncTransport)
    N)asynccontextmanagercontextmanager)AnyCallableDictOptional   )AsyncTransportSyncTransport)r
   r	   install_transportrestore_transport)zdeepgram.listen.v1.raw_clientzdeepgram.listen.v1.clientzdeepgram.listen.v2.raw_clientzdeepgram.listen.v2.clientzdeepgram.speak.v1.raw_clientzdeepgram.speak.v1.clientzdeepgram.agent.v1.raw_clientzdeepgram.agent.v1.client
_originals_active_sync_factory_active_async_factoryc                	   @   sF   e Zd ZdZdeddfddZeddedee	eef  fd	d
Z
dS )_SyncTransportShimzDrop-in replacement for ``websockets.sync.client``.

    The auto-generated code calls::

        websockets_sync_client.connect(ws_url, additional_headers=headers)

    This shim's ``.connect()`` delegates to the user-supplied factory.
    factoryreturnNc                 C   
   || _ d S N_factoryselfr    r   F/home/ubuntu/.local/lib/python3.10/site-packages/deepgram/transport.py__init__=      
z_SyncTransportShim.__init__urladditional_headersc                 c   s6    |pi }|  ||}z
|V  W |  d S |  w r   r   close)r   r   r   headers	transportr   r   r   connect@   s   z_SyncTransportShim.connectr   )__name__
__module____qualname____doc__r   r   r   strr   r   r#   r   r   r   r   r   3   s
    	&r   c                	   @   sf   e Zd ZdZdeddfddZeddedee	eef  fd	d
Z
ddedee	eef  fddZdS )_AsyncTransportShimzDrop-in replacement for ``websockets_client_connect``.

    The auto-generated code calls::

        websockets_client_connect(ws_url, extra_headers=headers)

    This shim is callable and returns an async context manager.
    r   r   Nc                 C   r   r   r   r   r   r   r   r   T   r   z_AsyncTransportShim.__init__r   extra_headersc                 C  sB   |pi }|  ||}z|V  W | I d H  d S | I d H  w r   r   )r   r   r*   r!   r"   r   r   r   _connectW   s   "z_AsyncTransportShim._connectc                 C   s   | j ||dS )N)r*   )r+   )r   r   r*   r   r   r   __call__`   s   z_AsyncTransportShim.__call__r   )r$   r%   r&   r'   r   r   r   r(   r   r   r+   r,   r   r   r   r   r)   J   s    	"$r)   )sync_factoryasync_factoryr-   r.   r   c              	   C   s  | durt durt | urtd|dur tdur t|ur td| dur&| a |dur,|a| r2t| nd}|r:t|nd}tD ]H}tj|}|du r[zt	
|}W n	 tyZ   Y q>w |tvrnt|ddt|dddt|< |durzt|drz||_|durt|dr||_q>dS )a  Monkey-patch the 8 auto-generated modules to use custom transports.

    Parameters
    ----------
    sync_factory
        ``factory(url, headers) -> transport`` used for sync WebSocket clients.
    async_factory
        ``factory(url, headers) -> transport`` used for async WebSocket clients.

    Raises
    ------
    RuntimeError
        If a different transport factory is already installed. Re-installing
        the same factory is allowed (idempotent). Call :func:`restore_transport`
        first to switch factories.
    NzA different sync transport factory is already installed. Only one transport factory per process is supported because transport patching is global. Call restore_transport() before creating a new client with a different transport_factory.zA different async transport factory is already installed. Only one transport factory per process is supported because transport patching is global. Call restore_transport() before creating a new client with a different transport_factory.websockets_sync_clientwebsockets_client_connect)r/   r0   )r   RuntimeErrorr   r   r)   _TARGET_MODULESsysmodulesget	importlibimport_moduleImportErrorr   getattrhasattrr/   r0   )r-   r.   	sync_shim
async_shimmod_pathmodr   r   r   r   d   s@   


r   c                  C   sb   t  D ]"\} }tj| }|du rq| D ]\}}|dur%t||| qqt   dadadS )z6Undo all patches applied by :func:`install_transport`.N)	r   itemsr3   r4   r5   setattrclearr   r   )r=   savedr>   attroriginalr   r   r   r      s   r   )r   N)r'   r6   r3   
contextlibr   r   typingr   r   r   r   transport_interfacer	   r
   __all__r2   r   r(   __annotations__r   r   r   r)   r   r   r   r   r   r   <module>   s.    
D