Ë
    kQêjG  ã                   ó‚   — d Z ddlmZmZ ddlmZmZmZmZ ddl	Z	ddl
Z
 G d„ de«      Z G d„ de«      Z G d	„ d
e«      Zy)a�  Header providers for LanceDB remote connections.

This module provides a flexible header management framework for LanceDB remote
connections, allowing users to implement custom header strategies for
authentication, request tracking, custom metadata, or any other header-based
requirements.

The module includes the HeaderProvider abstract base class and example implementations
(StaticHeaderProvider and OAuthProvider) that demonstrate common patterns.

The HeaderProvider interface is designed to be called before each request to the remote
server, enabling dynamic header scenarios where values may need to be
refreshed, rotated, or computed on-demand.
é    )ÚABCÚabstractmethod)ÚDictÚOptionalÚCallableÚAnyNc                   ó0   — e Zd ZdZedeeef   fd„«       Zy)ÚHeaderProvidera�  Abstract base class for providing custom headers for each request.

    Users can implement this interface to provide dynamic headers for various purposes
    such as authentication (OAuth tokens, API keys), request tracking (correlation IDs),
    custom metadata, or any other header-based requirements. The provider is called
    before each request to ensure fresh header values are always used.

    Error Handling
    --------------
    If get_headers() raises an exception, the request will fail. Implementations
    should handle recoverable errors internally (e.g., retry token refresh) and
    only raise exceptions for unrecoverable errors.
    Úreturnc                  ó   — y)a  Get the latest headers to be added to requests.

        This method is called before each request to the remote LanceDB server.
        Implementations should return headers that will be merged with existing headers.

        Returns
        -------
        Dict[str, str]
            Dictionary of header names to values to add to the request.

        Raises
        ------
        Exception
            If unable to fetch headers, the exception will be propagated
            and the request will fail.
        N© ©Úselfs    úZ/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/lancedb/remote/header.pyÚget_headerszHeaderProvider.get_headers(   s   € ð$ 	ó    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ústrr   r   r   r   r
   r
      s*   „ ñð ð˜T # s (™^ò ó ñr   r
   c                   ó<   — e Zd ZdZdeeef   fd„Zdeeef   fd„Zy)ÚStaticHeaderProvidera™  Example implementation: A simple header provider that returns static headers.

    This is an example implementation showing how to create a HeaderProvider
    for cases where headers don't change during the session. Users can use this
    as a reference for implementing their own providers.

    Parameters
    ----------
    headers : Dict[str, str]
        Static headers to return for every request.
    Úheadersc                 ó.   — |j                  «       | _        y)z¡Initialize with static headers.

        Parameters
        ----------
        headers : Dict[str, str]
            Headers to return for every request.
        N)ÚcopyÚ_headers)r   r   s     r   Ú__init__zStaticHeaderProvider.__init__J   s   € ð  Ÿ™›ˆ�r   r   c                 ó6   — | j                   j                  «       S )zƒReturn the static headers.

        Returns
        -------
        Dict[str, str]
            Copy of the static headers.
        )r   r   r   s    r   r   z StaticHeaderProvider.get_headersT   s   € ð �}‰}×!Ñ!Ó#Ð#r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   =   s/   „ ñ
ð'  S¨# X¡ó 'ð$˜T # s (™^ô $r   r   c                   óX   — e Zd ZdZ	 d
deg ef   defd„Zdd„Zde	fd„Z
deeef   fd	„Zy)ÚOAuthProvidera£  Example implementation: OAuth token provider with automatic refresh.

    This is an example implementation showing how to manage OAuth tokens
    with automatic refresh when they expire. Users can use this as a reference
    for implementing their own OAuth or token-based authentication providers.

    Parameters
    ----------
    token_fetcher : Callable[[], Dict[str, Any]]
        Function that fetches a new token. Should return a dict with
        'access_token' and optionally 'expires_in' (seconds until expiration).
    refresh_buffer_seconds : int, optional
        Number of seconds before expiration to trigger refresh. Default is 300
        (5 minutes).
    Útoken_fetcherÚrefresh_buffer_secondsc                 ón   — || _         || _        d| _        d| _        t	        j
                  «       | _        y)ab  Initialize the OAuth provider.

        Parameters
        ----------
        token_fetcher : Callable[[], Any]
            Function to fetch new tokens. Should return dict with
            'access_token' and optionally 'expires_in'.
        refresh_buffer_seconds : int, optional
            Seconds before expiry to refresh token. Default 300.
        N)Ú_token_fetcherÚ_refresh_bufferÚ_current_tokenÚ_token_expires_atÚ	threadingÚLockÚ_refresh_lock)r   r"   r#   s      r   r   zOAuthProvider.__init__p   s3   € ð ,ˆÔØ5ˆÔØ-1ˆÔØ26ˆÔÜ&Ÿ^™^Ó-ˆÕr   r   Nc                 óN  — | j                   5  | j                  «       rt| j                  «       }|j                  d«      | _        | j                  st        d«      ‚|j                  d«      }|�t        j                  «       |z   | _        nd| _        ddd«       y# 1 sw Y   yxY w)z7Refresh the token if it's expired or close to expiring.Úaccess_tokenz+Token fetcher did not return 'access_token'Ú
expires_inN)r+   Ú_needs_refreshr%   Úgetr'   Ú
ValueErrorÚtimer(   )r   Ú
token_datar.   s      r   Ú_refresh_token_if_neededz&OAuthProvider._refresh_token_if_neededƒ   s“   € à×Ññ 	2à×"Ñ"Ô$Ø!×0Ñ0Ó2�
à&0§n¡n°^Ó&D�Ô#Ø×*Ò*Ü$Ð%RÓSÐSð (Ÿ^™^¨LÓ9�
ØÐ)Ü-1¯Y©Y«[¸:Ñ-E�DÕ*ð .2�DÔ*÷	2÷ 	2ñ 	2ús   �BBÂB$c                 ó’   — | j                   €y| j                  €yt        j                  «       | j                  | j                  z
  k\  S )zCheck if token needs refresh.TF)r'   r(   r2   r&   r   s    r   r/   zOAuthProvider._needs_refresh–   sD   € à×ÑÐ&Øà×!Ñ!Ð)àô �y‰y‹{˜t×5Ñ5¸×8LÑ8LÑLÑMÐMr   c                 ót   — | j                  «        | j                  st        d«      ‚d| j                  › �ddœS )a  Get OAuth headers, refreshing token if needed.

        Returns
        -------
        Dict[str, str]
            Headers with Bearer token authorization.

        Raises
        ------
        Exception
            If unable to fetch or refresh token.
        zFailed to obtain OAuth tokenzBearer Úoidc)ÚAuthorizationzx-lancedb-credential-type)r4   r'   ÚRuntimeErrorr   s    r   r   zOAuthProvider.get_headers¢   sE   € ð 	×%Ñ%Ô'à×"Ò"ÜÐ=Ó>Ð>ð  ' t×':Ñ':Ð&;Ð<Ø)/ñ
ð 	
r   )i,  )r   N)r   r   r   r   r   r   Úintr   r4   Úboolr/   r   r   r   r   r   r   r!   r!   _   sS   „ ñð" ORñ.Ø% b¨# gÑ.ð.ØHKó.ó&2ð&
N ó 
Nð
˜T # s (™^ô 
r   r!   )r   Úabcr   r   Útypingr   r   r   r   r2   r)   r
   r   r!   r   r   r   ú<module>r>      sC   ðñ÷ $ß 0Ó 0Û Û ô!�Sô !ôH$˜>ô $ôDX
�Nõ X
r   