o
    =i}4                  	   @   s8  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e/de/dee& de.fddZ0dS )    N)AnyDictListOptionalUnion)Timeout)SyncPostgrestClientSyncRequestBuilderSyncRPCFilterRequestBuilder) DEFAULT_POSTGREST_CLIENT_TIMEOUT)CountMethod)RealtimeChannelOptionsSyncRealtimeChannelSyncRealtimeClientSyncStorageClient)DEFAULT_TIMEOUT)SyncMemoryStorage)AuthChangeEventSession)SyncFunctionsClient)URL   )SyncClientOptions)SyncHttpxClient)RealtimeClientOptions   )SyncSupabaseAuthClientc                       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__ R/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/supabase/_sync/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Clientz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Client.__init__c                 C   sz   |r|j dnd }| |||}|d u r;z|j }|r"||jnd }W n ty0   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(   createe   s    
zClient.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Client.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Client.schemac                 C   rf   )zDPerform a table operation.

        See the `table` method.
        )rg   rb   rc   r'   r'   r(   rb      rh   zClient.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
        -------
        SyncFilterRequestBuilder
            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
Client.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Client.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Client.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Client.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Client.channelc                 C   s
   | j  S )zReturns all realtime channels.)r6   get_channelsrs   r'   r'   r(   rz      s   
zClient.get_channelsry   c                 C   s   | j | dS )z?Unsubscribes and removes Realtime channel from Realtime client.N)r6   remove_channel)r$   ry   r'   r'   r(   r{      s   zClient.remove_channelc                 C   s   | j   dS )zDUnsubscribes and removes all Realtime channels from Realtime client.N)r6   remove_all_channelsrs   r'   r'   r(   r|      s   zClient.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Client._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Client._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!Client._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Client._init_postgrest_clientr}   c                 C   s
   d| S )NzBearer r'   )r$   r}   r'   r'   r(   rW   ?  s   
zClient._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   B  s   zClient._get_auth_headerseventr^   c                 C   sV   | j }|dv rd | _d | _d | _|r|jn| j }| |}|| jjd< || jj	d< d S )N)	SIGNED_INTOKEN_REFRESHED
SIGNED_OUTrT   )
r0   rO   rP   rQ   rX   rW   r1   rE   r:   _headers)r$   r   r^   rX   r\   r'   r'   r(   rS   N  s   
zClient._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                 C   s   t j| ||d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   )r.   r`   r   r'   r'   r(   create_client]  s   r   r!   )1rD   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>   s@      @