o
    ˳i95                  	   @   s@  d dl Z 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mZ d dl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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* ddl+m,Z, G dd de-Z.G dd dZ/	dde0de0dee' de/fddZ1dS )    N)AnyDictListOptionalUnion)Timeout)AsyncPostgrestClientAsyncRequestBuilderAsyncRPCFilterRequestBuilder) DEFAULT_POSTGREST_CLIENT_TIMEOUT)CountMethod)AsyncRealtimeChannelAsyncRealtimeClientRealtimeChannelOptionsAsyncStorageClient)DEFAULT_TIMEOUT)AsyncMemoryStorage)AuthChangeEventSession)AsyncFunctionsClient)URL   )AsyncClientOptions)AsyncHttpxClient)RealtimeClientOptions   )AsyncSupabaseAuthClientc                       s&   e Zd Zdeddf fddZ  ZS )SupabaseExceptionmessagereturnNc                    s   || _ t | j  d S N)r   super__init__)selfr   	__class__ J/home/ubuntu/.local/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	dIdededee ddfddZe	dIdededee dd 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				dJdedeeeef  dee dededefddZedefddZedefddZedefdd Z	dId!edee defd"d#Zdee fd$d%Zd&eddfd'd(Z dKd)d*Z!e"	dId+e#dedee$ de%fd,d-Z&e"e'd.ddfd/ed0eeef d1e(d2ed3ee d4e)e*df defd5d6Z+e"	.	dLd7ed8ed2ed3ee de,f
d9d:Z-e"e.d.ddfd;ed0eeef ded<e)e(e/e0f d2ed3ee d4e)e*df defd=d>Z1d?edefd@dAZ2dIdBee deeef fdCdDZ3dEe4dFee5 ddfdGdHZ6dS )MAsyncClientzSupabase client class.Nsupabase_urlsupabase_keyoptionsr    c                 C   sP  |st d|st dtd|st d|du r tt d}|dr)t|nt|d | _|| _t		|| _
i |j|  | j
_| jdd	| _| jd
d	| jjdkr[dnd| _| jdd	| _| jdd	d| _| jdd	| _| jt| j| j
d| _| j| j| j| j
r| j
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 URLN)storage/restv1realtimehttpswsswsauthr2    	functions)auth_urlclient_options)realtime_urlr0   r1   )r   rematchClientOptionsr   endswithr   r/   r0   copyr1   headers_get_auth_headersjoinpathrest_urlwith_schemeschemer?   r=   storage_urlfunctions_url_init_supabase_auth_clientr,   r:   _init_realtime_clientr6   
_postgrest_storage
_functionson_auth_state_change_listen_to_auth_events)r$   r/   r0   r1   r'   r'   r(   r#   &   sJ   zAsyncClient.__init__c                    s   |r	|j dnd }| |||}|d u r?z|j I d H }|r&||jnd }W n ty4   d }Y nw |jj |	| |S )NAuthorization)
rE   getr:   get_session_create_auth_headeraccess_token	Exceptionr1   updaterF   )clsr/   r0   r1   auth_headerclientsessionsession_access_tokenr'   r'   r(   createf   s"   zAsyncClient.create
table_namec                 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$   ra   r'   r'   r(   table   s   
zAsyncClient.tableschemac                 C      | 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.
        )	postgrestre   )r$   re   r'   r'   r(   re         zAsyncClient.schemac                 C   rf   )zDPerform a table operation.

        See the `table` method.
        )rg   rb   rc   r'   r'   r(   rb      rh   zAsyncClient.from_FfnparamscountheadrU   c                 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.
        count: The method to use to get the count of rows returned.
        head: When set to `true`, `data` will not be returned. Useful if you only need the count.
        get: When set to `true`, the function will be called with read-only access mode.

        Returns
        -------
        AsyncFilterRequestBuilder
            Returns a filter builder. This lets you apply filters on the response
            of an RPC.
        N)rg   rpc)r$   ri   rj   rk   rl   rU   r'   r'   r(   rm      s   zAsyncClient.rpcc                 C   s<   | j d u r| jt| j| jj| jj| jj| jjd| _ | j S )N)rH   rE   re   timeouthttp_client)	rO   _init_postgrest_clientr,   rH   r1   rE   re   postgrest_client_timeouthttpx_clientr$   r'   r'   r(   rg      s   
zAsyncClient.postgrestc                 C   s6   | j d u r| jt| j| jj| jj| jjd| _ | j S )N)rK   rE   storage_client_timeoutro   )rP   _init_storage_clientr,   rK   r1   rE   rt   rr   rs   r'   r'   r(   r2      s   
zAsyncClient.storagec                 C   sD   | j d u rtt| j| jj| jjd u r| jjnd | jjd| _ | j S )N)urlrE   rn   ro   )rQ   r   r,   rL   r1   rE   rr   function_client_timeoutrs   r'   r'   r(   r<      s   

zAsyncClient.functionstopicc                 C   s   | j ||pi S )zJCreates a Realtime channel with Broadcast, Presence, and Postgres Changes.)r6   channel)r$   rx   rj   r'   r'   r(   ry      s   zAsyncClient.channelc                 C   s
   | j  S )zReturns all realtime channels.)r6   get_channelsrs   r'   r'   r(   rz      s   
zAsyncClient.get_channelsry   c                    s   | j |I dH  dS )z?Unsubscribes and removes Realtime channel from Realtime client.N)r6   remove_channel)r$   ry   r'   r'   r(   r{      s   zAsyncClient.remove_channelc                    s   | j  I dH  dS )zDUnsubscribes and removes all Realtime channels from Realtime client.N)r6   remove_all_channelsrs   r'   r'   r(   r|      s   zAsyncClient.remove_all_channelsr?   c                 C   s"   |pi }	 t t| fd|i|S )Ntoken)r   r,   )r?   r0   r1   realtime_optionsr'   r'   r(   rN      s   z!AsyncClient._init_realtime_clientTrK   rE   rt   verifyproxyro   c                 C   s*   |d urt | ||dS t | ||||d dS )N)rv   rE   ro   )rv   rE   rn   r   r   ro   r   )rK   rE   rt   r   r   ro   r'   r'   r(   ru      s   	z AsyncClient._init_storage_clientr=   r>   c                 C   s&   t | |j|j|j|j|j|||jd	S )z0Creates a wrapped instance of the GoTrue Client.)	rv   auto_refresh_tokenpersist_sessionr2   rE   	flow_typer   r   ro   )r   r   r   r2   rE   r   rr   )r=   r>   r   r   r'   r'   r(   rM     s   z&AsyncClient._init_supabase_auth_clientrH   rn   c              	   C   s.   |durt | |||dS t | |||||ddS )z@Private helper for creating an instance of the Postgrest client.N)rE   re   ro   )rE   re   rn   r   r   ro   )r   )rH   rE   re   rn   r   r   ro   r'   r'   r(   rp   &  s   z"AsyncClient._init_postgrest_clientr}   c                 C   s
   d| S )NzBearer r'   )r$   r}   r'   r'   r(   rW   @  s   
zAsyncClient._create_auth_headerauthorizationc                 C   s.   |d u r| j jd| | j}	 | j|dS )NrT   )apiKeyrT   )r1   rE   rU   rW   r0   )r$   r   r'   r'   r(   rF   C  s   zAsyncClient._get_auth_headerseventr^   c                 C   sh   | j }|dv rd | _d | _d | _|r|jn| j }| |}|| jjd< || jj	d< t
| j| d S )N)	SIGNED_INTOKEN_REFRESHED
SIGNED_OUTrT   )r0   rO   rP   rQ   rX   rW   r1   rE   r:   _headersasynciocreate_taskr6   set_auth)r$   r   r^   rX   r\   r'   r'   r(   rS   O  s   
z"AsyncClient._listen_to_auth_eventsr!   )NNFF)r    N)TN)7r)   r*   r+   __doc__r,   r   rB   r#   classmethodr`   r	   rd   r   re   rb   r   r   r   boolr
   rm   propertyrg   r   r2   r   r<   r   r   ry   r   rz   r{   r|   staticmethodr   r   r   rN   DEFAULT_STORAGE_CLIENT_TIMEOUTintr   r   ru   r   rM   r   floatr   rp   rW   rF   r   r   rS   r'   r'   r'   r(   r.   #   s   
@	








 r.   r/   r0   r1   r    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/   r0   r1   N)r.   r`   r   r'   r'   r(   create_client_  s   r   r!   )2r   rD   r@   typingr   r   r   r   r   httpxr   rg   r   r	   r
   postgrest.constantsr   postgrest.typesr   r6   r   r   r   storage3r   storage3.constantsr   r   supabase_authr   supabase_auth.typesr   r   supabase_functionsr   yarlr   lib.client_optionsr   rB   r   typesr   auth_clientr   rY   r   r.   r,   r   r'   r'   r'   r(   <module>   sB      A