o
    ~jh,                  	   @   s  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 de#Z$G dd dZ%	dde&de&dee df de%fddZ'dS )    N)AnyDictListOptionalUnion)AsyncMemoryStorage)AuthChangeEventSession)Timeout)AsyncPostgrestClientAsyncRequestBuilderAsyncRPCFilterRequestBuilder) DEFAULT_POSTGREST_CLIENT_TIMEOUT)AsyncRealtimeChannelAsyncRealtimeClientRealtimeChannelOptionsAsyncStorageClient)DEFAULT_TIMEOUT)AsyncFunctionsClient   )ClientOptions   )AsyncSupabaseAuthClientc                       s"   e Zd Zdef fddZ  ZS )SupabaseExceptionmessagec                    s   || _ t | j  d S N)r   super__init__)selfr   	__class__ L/var/www/html/bot/env/lib/python3.10/site-packages/supabase/_async/client.pyr      s   zSupabaseException.__init__)__name__
__module____qualname__strr   __classcell__r"   r"   r    r#   r      s    r   c                   @   s(  e Zd ZdZ	dCdededeedf fddZe	dCdededeedf fdd	Z	d
ede
fddZdedefddZd
ede
fddZ	dCdedeeeef  defddZedd Zedd Zedd Zi fdededefddZdee fd d!Zd"eddfd#d$ZdDd%d&Zed'ededeeeef  de fd(d)Z!ee"d*fd+ed,eeef d-e#d.e$de%f
d/d0Z&e	*dEd1ed2ed.e$de'fd3d4Z(ee)d*fd5ed,eeef ded6ee#e*e+f d.e$defd7d8Z,d9efd:d;Z-	dCd<eedf deeef fd=d>Z.d?e/d@ee0df fdAdBZ1dS )FAsyncClientzSupabase client class.Nsupabase_urlsupabase_keyoptionsc                 C   s  |st d|st dtd|st dtd|s t d|du r*tt d}|| _|| _|| _|j	| 
  | d	| _| d
dd| _| d| _| d| _| d| _| j| j|d| _| j| j| j|rq|jndd| _d| _d| _d| _| j| j dS )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matchr   r   r*   r+   r,   headersupdate_get_auth_headersrest_urlreplacer3   r0   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,   r"   r"   r#   r       sD   zAsyncClient.__init__c           	   
      s   |r	|j dnd }| |||}|d u rDz|j I d H }||j}W n ty9 } zd }W Y d }~nd }~ww |jj |	| |S )NAuthorization)
r6   getr>   get_session_create_auth_headeraccess_token	Exceptionr,   r7   r8   )	clsr*   r+   r,   auth_headerclientsessionsession_access_tokenerrr"   r"   r#   create^   s   zAsyncClient.create
table_namereturnc                 C   s
   |  |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   rS   r"   r"   r#   tableu   s   
zAsyncClient.tableschemac                 C   s$   | j | j| jj|| jjd| _| 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.
        r9   r6   rX   timeout)_init_postgrest_clientr9   r,   r6   postgrest_client_timeoutrA   )r   rX   r"   r"   r#   rX   ~   s   zAsyncClient.schemac                 C   s   | j |S )zDPerform a table operation.

        See the `table` method.
        )	postgrestrU   rV   r"   r"   r#   rU      s   zAsyncClient.from_fnparamsc                 C   s   |du ri }| 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.
        N)r]   rpc)r   r^   r_   r"   r"   r#   r`      s   zAsyncClient.rpcc                 C   s2   | j d u r| j| j| jj| jj| jjd| _ | j S )NrY   )rA   r[   r9   r,   r6   rX   r\   r   r"   r"   r#   r]      s   
zAsyncClient.postgrestc                 C   s,   | j d u r| j| j| jj| jjd| _ | j S )N)r;   r6   storage_client_timeout)rB   _init_storage_clientr;   r,   r6   rb   ra   r"   r"   r#   r-      s   
zAsyncClient.storagec                 C   s(   | j d u rt| j| jj| jj| _ | j S r   )rC   r   r<   r,   r6   function_client_timeoutra   r"   r"   r#   	functions   s   
zAsyncClient.functionstopicc                 C   s   | j ||S )zJCreates a Realtime channel with Broadcast, Presence, and Postgres Changes.)r@   channel)r   rf   r_   r"   r"   r#   rg      s   zAsyncClient.channelc                 C   s
   | j  S )zReturns all realtime channels.)r@   get_channelsra   r"   r"   r#   rh      s   
zAsyncClient.get_channelsrg   c                    s   | j |I dH  dS )z?Unsubscribes and removes Realtime channel from Realtime client.N)r@   remove_channel)r   rg   r"   r"   r#   ri      s   zAsyncClient.remove_channelc                    s   | j  I dH  dS )zDUnsubscribes and removes all Realtime channels from Realtime client.N)r@   remove_all_channelsra   r"   r"   r#   rj      s   zAsyncClient.remove_all_channelsr3   c                 C   s   t | ||pi dS )zBPrivate method for creating an instance of the realtime-py client.)tokenr_   )r   r2   r"   r"   r#   r?      s   
z!AsyncClient._init_realtime_clientTr;   r6   rb   verifyc                 C   s   t | |||S r   r   )r;   r6   rb   rl   r"   r"   r#   rc      s   z AsyncClient._init_storage_clientr0   r1   c              	   C   s    t | |j|j|j|j|j|dS )z0Creates a wrapped instance of the GoTrue Client.)urlauto_refresh_tokenpersist_sessionr-   r6   	flow_typerl   )r   rn   ro   r-   r6   rp   )r0   r1   rl   r"   r"   r#   r=      s   z&AsyncClient._init_supabase_auth_clientr9   rZ   c                 C   s   t | ||||dS )z@Private helper for creating an instance of the Postgrest client.)r6   rX   rZ   rl   )r   )r9   r6   rX   rZ   rl   r"   r"   r#   r[      s   	z"AsyncClient._init_postgrest_clientrk   c                 C   s
   d| S )NzBearer r"   )r   rk   r"   r"   r#   rI     s   
zAsyncClient._create_auth_headerauthorizationc                 C   s.   |d u r| j jd| | j}	 | j|dS )NrF   )apiKeyrF   )r,   r6   rG   rI   r+   )r   rq   r"   r"   r#   r8     s   zAsyncClient._get_auth_headerseventrO   c                 C   sR   | j }|dv rd | _d | _d | _|r|jn| j }| || jjd< | j	| d S )N)	SIGNED_INTOKEN_REFRESHED
SIGNED_OUTrF   )
r+   rA   rB   rC   rJ   rI   r,   r6   r@   set_auth)r   rs   rO   rJ   r"   r"   r#   rE     s   z"AsyncClient._listen_to_auth_eventsr   )rT   N)T)2r$   r%   r&   __doc__r'   r   r   r   classmethodrR   r   rW   r   rX   rU   r   r   r   r   r`   propertyr]   r-   re   r   r   rg   r   rh   ri   rj   staticmethodr   r?   DEFAULT_STORAGE_CLIENT_TIMEOUTintboolr   rc   r   r=   r   floatr
   r[   rI   r8   r   r	   rE   r"   r"   r"   r#   r)      s    

>
	


	









r)   r*   r+   r,   rT   c                    s   t j| ||dI dH S )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)   rR   r   r"   r"   r#   create_client0  s   r   r   )(r4   typingr   r   r   r   r   gotruer   gotrue.typesr   r	   httpxr
   r]   r   r   r   postgrest.constantsr   r@   r   r   r   storage3r   storage3.constantsr   r|   supafuncr   lib.client_optionsr   auth_clientr   rK   r   r)   r'   r   r"   r"   r"   r#   <module>   s6      
