
    g-              	          d dl Z d dlZd dlmZmZmZmZmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZmZmZ d dlmZ d dlmZ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$  G d de%      Z& G d d      Z'	 dde(de(dee"   de'fdZ)y)    N)AnyDictListOptionalUnion)AsyncMemoryStorage)AuthChangeEventSession)Timeout)AsyncPostgrestClientAsyncRequestBuilderAsyncRPCFilterRequestBuilder) DEFAULT_POSTGREST_CLIENT_TIMEOUT)AsyncRealtimeChannelAsyncRealtimeClientRealtimeChannelOptionsAsyncStorageClient)DEFAULT_TIMEOUT)AsyncFunctionsClient   )AsyncClientOptions   )AsyncSupabaseAuthClientc                   $     e Zd Zdef fdZ xZS )SupabaseExceptionmessagec                 F    || _         t        | 	  | j                          y N)r   super__init__)selfr   	__class__s     K/var/www/openai/venv/lib/python3.12/site-packages/supabase/_async/client.pyr!   zSupabaseException.__init__   s    &    )__name__
__module____qualname__strr!   __classcell__)r#   s   @r$   r   r      s    ' ' 'r%   r   c                   `   e Zd ZdZ	 d0dededee   fdZe	 d0dededee   fd       Z	ded	e
fd
Zded	efdZded	e
fdZ	 d0dedeeeef      d	efdZed        Zed        Zed        Zi fdeded	efdZd	ee   fdZded	dfdZd1dZe	 d0dededeeeef      d	efd       Z ee!ddfdedeeef   de"d e#d!ee   d	e$fd"       Z%e	 	 d2d#ed$ed e#d!ee   d	e&f
d%       Z'ee(ddfd&edeeef   ded'e)e"e*e+f   d e#d!ee   d	efd(       Z,d)efd*Z-d0d+ee   d	eeef   fd,Z.d-e/d.ee0   fd/Z1y)3AsyncClientzSupabase client class.Nsupabase_urlsupabase_keyoptionsc                    |st        d      |st        d      t        j                  d|      st        d      t        j                  d|      st        d      |t        t	                     }|| _        || _        || _        |j                  j                  | j                                | d	| _        | d
j                  dd      | _        | d| _        | d| _        | d| _        | j#                  | j                  |      | _        | j'                  | j                  | j                  |r|j(                  nd      | _        d| _        d| _        d| _        | j$                  j1                  | j2                         y)a  Instantiate the client.

        Parameters
        ----------
        supabase_url: str
            The URL to the Supabase instance that should be connected to.
        supabase_key: str
            The API key to the Supabase instance that should be connected to.
        **options
            Any extra settings to be optionally specified - also see the
            `DEFAULT_OPTIONS` dict.
        zsupabase_url is requiredzsupabase_key is requiredz^(https?)://.+zInvalid URLz7^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$zInvalid API keyN)storagez/rest/v1z/realtime/v1httpwsz/auth/v1z/storage/v1z/functions/v1)auth_urlclient_optionsrealtime_urlr.   r/   )r   rematchClientOptionsr   r-   r.   r/   headersupdate_get_auth_headersrest_urlreplacer7   r4   storage_urlfunctions_url_init_supabase_auth_clientauth_init_realtime_clientrealtime
_postgrest_storage
_functionson_auth_state_change_listen_to_auth_events)r"   r-   r.   r/   s       r$   r!   zAsyncClient.__init__!   s   & #$>??#$>?? xx)<8#M22 xxF
 $$566?#,>,@AG((t5578'.1+nL9AA&$O'.1*^;7 ,~]; 33]]" 4 
	 22****(/G$$T 3 

 		&&t'B'BCr%   c                 |  K   |r|j                   j                  d      nd } | |||      }|r	 |j                  j                          d {   }|j	                  |j
                        }|j                  j                   j                  |j                  |             |S 7 U# t        $ r}d }Y d }~Hd }~ww xY ww)NAuthorization)
r;   getrC   get_session_create_auth_headeraccess_token	Exceptionr/   r<   r=   )	clsr-   r.   r/   auth_headerclientsessionsession_access_tokenerrs	            r$   createzAsyncClient.create_   s      ?Fgoo))/:4\<9, & 7 7 99'-'A'A'BVBV'W$ NN""))(()=>  : ,'+$,s@   ,B<B$ B"B$ ,6B<"B$ $	B9-B4/B<4B99B<
table_namereturnc                 $    | j                  |      S )zPerform a table operation.

        Note that the supabase client uses the `from` method, but in Python,
        this is a reserved keyword, so we have elected to use the name `table`.
        Alternatively you can use the `.from_()` method.
        )from_r"   rY   s     r$   tablezAsyncClient.tablev   s     zz*%%r%   schemac                     | j                   j                  |k7  r8|| j                   _        | j                  r| j                  j                  |       | j                  S )zSelect a schema to query or perform an function (rpc) call.

        The schema needs to be on the list of exposed schemas inside Supabase.
        )r/   r_   rF   	postgrest)r"   r_   s     r$   r_   zAsyncClient.schema   sE    
 <<&("(DLL&&v.~~r%   c                 8    | j                   j                  |      S )zDPerform a table operation.

        See the `table` method.
        )ra   r\   r]   s     r$   r\   zAsyncClient.from_   s    
 ~~##J//r%   fnparamsc                 B    |i }| j                   j                  ||      S )a  Performs a stored procedure call.

        Parameters
        ----------
        fn : callable
            The stored procedure call to be executed.
        params : dict of any
            Parameters passed into the stored procedure call.

        Returns
        -------
        SyncFilterRequestBuilder
            Returns a filter builder. This lets you apply filters on the response
            of an RPC.
        )ra   rpc)r"   rc   rd   s      r$   rf   zAsyncClient.rpc   s%    $ >F~~!!"f--r%   c                     | j                   `| j                  | j                  | j                  j                  | j                  j
                  | j                  j                        | _         | j                   S )N)r>   r;   r_   timeout)rF   _init_postgrest_clientr>   r/   r;   r_   postgrest_client_timeoutr"   s    r$   ra   zAsyncClient.postgrest   s]    ??""99,,||**==	 : DO r%   c                     | j                   K| j                  | j                  | j                  j                  | j                  j
                        | _         | j                   S )N)r@   r;   storage_client_timeout)rG   _init_storage_clientr@   r/   r;   rm   rk   s    r$   r1   zAsyncClient.storage   sR    ==  55 ,,,,'+||'J'J 6 DM
 }}r%   c                     | j                   Dt        | j                  | j                  j                  | j                  j
                        | _         | j                   S r   )rH   r   rA   r/   r;   function_client_timeoutrk   s    r$   	functionszAsyncClient.functions   sG    ??"2""$$44DO
 r%   topicc                 :    | j                   j                  ||      S )zJCreates a Realtime channel with Broadcast, Presence, and Postgres Changes.)rE   channel)r"   rr   rd   s      r$   rt   zAsyncClient.channel   s     }}$$UF33r%   c                 6    | j                   j                         S )zReturns all realtime channels.)rE   get_channelsrk   s    r$   rv   zAsyncClient.get_channels   s    }}))++r%   rt   c                 V   K   | j                   j                  |       d{    y7 w)z?Unsubscribes and removes Realtime channel from Realtime client.N)rE   remove_channel)r"   rt   s     r$   rx   zAsyncClient.remove_channel   s     mm**7333s   )')c                 T   K   | j                   j                          d{    y7 w)zDUnsubscribes and removes all Realtime channels from Realtime client.N)rE   remove_all_channelsrk   s    r$   rz   zAsyncClient.remove_all_channels   s     mm//111s   (&(r7   c                 (    |i }	 t        | fd|i|S )Ntoken)r   r6   s      r$   rD   z!AsyncClient._init_realtime_client   s&     ?GP"<O|OwOOr%   Tr@   r;   rm   verifyproxyc                      t        | ||||      S r   r   )r@   r;   rm   r}   r~   s        r$   rn   z AsyncClient._init_storage_client   s     ""8&%
 	
r%   r4   r5   c           
          t        | |j                  |j                  |j                  |j                  |j
                  ||      S )z0Creates a wrapped instance of the GoTrue Client.)urlauto_refresh_tokenpersist_sessionr1   r;   	flow_typer}   r~   )r   r   r   r1   r;   r   )r4   r5   r}   r~   s       r$   rB   z&AsyncClient._init_supabase_auth_client   sG     '-@@*::"**"**$..	
 		
r%   r>   rh   c                 $    t        | |||||      S )z@Private helper for creating an instance of the Postgrest client.)r;   r_   rh   r}   r~   )r   )r>   r;   r_   rh   r}   r~   s         r$   ri   z"AsyncClient._init_postgrest_client  s#     $
 	
r%   r|   c                     d| S )NzBearer  )r"   r|   s     r$   rO   zAsyncClient._create_auth_header  s      r%   authorizationc                     |?| j                   j                  j                  d| j                  | j                              }	 | j                  |dS )NrL   )apiKeyrL   )r/   r;   rM   rO   r.   )r"   r   s     r$   r=   zAsyncClient._get_auth_headers  sS      LL0044!9!9$:K:K!LM 	1''*
 	
r%   eventrU   c                 .   | j                   }|dv r/d | _        d | _        d | _        |r|j                  n| j                   }| j                  |      | j                  j                  d<   t        j                  | j                  j                  |             y )N)	SIGNED_INTOKEN_REFRESHED
SIGNED_OUTrL   )r.   rF   rG   rH   rP   rO   r/   r;   asynciocreate_taskrE   set_auth)r"   r   rU   rP   s       r$   rJ   z"AsyncClient._listen_to_auth_events#  s~     ((BB"DO DM"DO3:7//@Q@QL040H0H0V_-DMM22<@Ar%   r   )rZ   N)TN)2r&   r'   r(   __doc__r)   r   r:   r!   classmethodrX   r   r^   r   r_   r\   r   r   r   rf   propertyra   r1   rq   r   r   rt   r   rv   rx   rz   staticmethodr   rD   DEFAULT_STORAGE_CLIENT_TIMEOUTintboolr   rn   r   rB   r   r   floatr   ri   rO   r=   r	   r
   rJ   r   r%   r$   r,   r,      s"     ,0	<D<D <D -(	<D| 
 ,0	  -(	 ,& &(; &	S 	%9 	0 0(; 0 ;?..'S#X7.	%., 	 	     <>44"84	4,d#78 ,4,@ 4T 42 RVPP),P7?S#X7OP	P P  'E#	
	
c3h	
 !$	
 		

 }	
 
	
 	
  #	

%
 
 }	

 
!
 
$ 
 /O#

c3h
 
 sE7*+	

 
 }
 

 
$! !

x} 

SRUX 

B$B/7/@Br%   r,   r-   r.   r/   rZ   c                 N   K   t         j                  | ||       d{   S 7 w)a  Create client function to instantiate supabase client like JS runtime.

    Parameters
    ----------
    supabase_url: str
        The URL to the Supabase instance that should be connected to.
    supabase_key: str
        The API key to the Supabase instance that should be connected to.
    **options
        Any extra settings to be optionally specified - also see the
        `DEFAULT_OPTIONS` dict.

    Examples
    --------
    Instantiating the client.
    >>> import os
    >>> from supabase import create_client, Client
    >>>
    >>> url: str = os.environ.get("SUPABASE_TEST_URL")
    >>> key: str = os.environ.get("SUPABASE_TEST_KEY")
    >>> supabase: Client = create_client(url, key)

    Returns
    -------
    Client
    r-   r.   r/   N)r,   rX   r   s      r$   create_clientr   2  s1     > ##!g $    s   %#%r   )*r   r8   typingr   r   r   r   r   gotruer   gotrue.typesr	   r
   httpxr   ra   r   r   r   postgrest.constantsr   rE   r   r   r   storage3r   storage3.constantsr   r   supafuncr   lib.client_optionsr   r:   auth_clientr   rQ   r   r,   r)   r   r   r%   r$   <module>r      s     	 3 3 % 1  
 A V V ' P ) D 0'	 'QB QBn (,!!! m$! 	!r%   