o
    i                     @   s   d Z ddlZddlZddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ded	ed
dfddZG dd de
ZG dd de	ZdS )a  
Custom client entrypoints that extend the generated BaseClient/AsyncBaseClient.

Adds support for:
- `access_token` as an alternative to `api_key` with the following rules:
  - If `access_token` is provided, it takes precedence and sets `Authorization: bearer <token>`
  - When `access_token` is used, `api_key` is forced to "token" to satisfy the generator,
    but the Authorization header is overridden for all HTTP and WebSocket requests.
- `session_id` as a header sent with every request and websocket connection:
  - If `session_id` is provided, it will be used; otherwise, a UUID is auto-generated
  - The session_id is sent as the `x-deepgram-session-id` header
- `transport_factory` to replace the default `websockets` transport with a custom one:
  - A callable: ``factory(url, headers) -> transport`` returning an object with
    ``send()``, ``recv()``, iteration, and ``close()`` support.
    N)AnyCallableDictOptional   )AsyncBaseClient
BaseClient)install_transport)BaseClientWrapperclient_wrapperbearer_tokenreturnc                    s`   | j dtdtttf f fdd}t|| | _ t| dr,t| jdr.| j | j_dS dS dS )zOverride header providers to always use a Bearer authorization token.

    This updates both:
    - client_wrapper.get_headers() used by WebSocket clients
    - client_wrapper.httpx_client.base_headers used by HTTP clients
    _selfr   c                    s    }d  |d< |S )Nzbearer Authorization )r   headersr   original_get_headersr   C/home/ubuntu/.local/lib/python3.10/site-packages/deepgram/client.py_get_headers_with_bearer$   s   zF_apply_bearer_authorization_override.<locals>._get_headers_with_bearerhttpx_clientbase_headersN)	get_headersr   r   strtypes
MethodTypehasattrr   r   )r   r   r   r   r   r   $_apply_bearer_authorization_override   s    r   c                       "   e Zd ZdZd fddZ  ZS )DeepgramClienta9  
    Custom Deepgram client that extends the generated BaseClient.

    Supports:
    - `session_id`: Optional session identifier. If not provided, a UUID is auto-generated.
                     Sent as `x-deepgram-session-id` header in all requests and websocket connections.
    - `access_token`: Alternative to `api_key`. If provided, uses Bearer token authentication.
    - `transport_factory`: Custom sync WebSocket transport factory. A callable
                           ``factory(url, headers) -> transport`` whose return value must support
                           ``send()``, ``recv()``, iteration, and ``close()``.
    - `telemetry_opt_out`: Telemetry opt-out flag (maintained for backwards compatibility, no-op).
    - `telemetry_handler`: Telemetry handler (maintained for backwards compatibility, no-op).
    r   Nc           
            | dd }| dd }| dd }t| dd}| dd }|d ur&|ntt }|d}	|	d u r;i }	|	|d< ||	d< |d urN|d	d u rNd
|d	< t j|i | || _|d urdt	| j
| |d urmt|d d | _d S )Naccess_token
session_idtransport_factorytelemetry_opt_outTtelemetry_handlerr   x-deepgram-session-idapi_keytoken)sync_factorypopboolr   uuiduuid4getsuper__init__r"   r   _client_wrapperr	   _telemetry_handler
selfargskwargsr!   r"   r#   r$   r%   final_session_idr   	__class__r   r   r1   @   *   


zDeepgramClient.__init__r   N__name__
__module____qualname____doc__r1   __classcell__r   r   r9   r   r   1       r   c                       r   )AsyncDeepgramClientaK  
    Custom async Deepgram client that extends the generated AsyncBaseClient.

    Supports:
    - `session_id`: Optional session identifier. If not provided, a UUID is auto-generated.
                     Sent as `x-deepgram-session-id` header in all requests and websocket connections.
    - `access_token`: Alternative to `api_key`. If provided, uses Bearer token authentication.
    - `transport_factory`: Custom async WebSocket transport factory. A callable
                           ``factory(url, headers) -> transport`` whose return value must support
                           ``send()``, ``recv()``, async iteration, and ``close()``.
    - `telemetry_opt_out`: Telemetry opt-out flag (maintained for backwards compatibility, no-op).
    - `telemetry_handler`: Telemetry handler (maintained for backwards compatibility, no-op).
    r   Nc           
         r    )Nr!   r"   r#   r$   Tr%   r   r&   r'   r(   )async_factoryr*   r4   r9   r   r   r1   y   r;   zAsyncDeepgramClient.__init__r<   r=   r   r   r9   r   rD   j   rC   rD   )rA   r   r-   typingr   r   r   r   base_clientr   r   	transportr	   deepgram.core.client_wrapperr
   r   r   r   rD   r   r   r   r   <module>   s    9