o
    `gy,                  	   @   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e&fddZ(dS )    N)AnyDictListOptionalUnion)SyncMemoryStorage)AuthChangeEventSession)Timeout)SyncPostgrestClientSyncRequestBuilderSyncRPCFilterRequestBuilder) DEFAULT_POSTGREST_CLIENT_TIMEOUT)RealtimeChannelOptionsSyncRealtimeChannelSyncRealtimeClientSyncStorageClient)DEFAULT_TIMEOUT)SyncFunctionsClient   )SyncClientOptions   )SyncSupabaseAuthClientc                       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__ P/var/www/html/propose/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                   @   s8  e Zd ZdZ	dDdededee fddZe	dDdededee 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	dDd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dEd%d&Ze	dDd'ededeeeef  defd(d)Z ee!d*dfd+ed,eeef d-e"d.e#d/ee de$fd0d1Z%e	*	dFd2ed3ed.e#d/ee de&f
d4d5Z'ee(d*dfd6ed,eeef ded7e)e"e*e+f d.e#d/ee defd8d9Z,d:efd;d<Z-dDd=ee deeef fd>d?Z.d@e/dAee0 fdBdCZ1dS )G
SyncClientz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matchClientOptionsr   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SyncClient.__init__c           	   
   C   s   |r|j dnd }| |||}|d u r@z|j }||j}W n ty5 } zd }W Y d }~nd }~ww |jj |	| |S )NAuthorization)
r7   getr?   get_session_create_auth_headeraccess_token	Exceptionr,   r8   r9   )	clsr*   r+   r,   auth_headerclientsessionsession_access_tokenerrr"   r"   r#   create^   s   
zSyncClient.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   rT   r"   r"   r#   tableu   s   
zSyncClient.tableschemac                 C   s,   | j j|kr|| j _| jr| 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,   rY   rB   	postgrest)r   rY   r"   r"   r#   rY   ~   s
   zSyncClient.schemac                 C   s   | j |S )zDPerform a table operation.

        See the `table` method.
        )rZ   rV   rW   r"   r"   r#   rV      s   zSyncClient.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)rZ   rpc)r   r[   r\   r"   r"   r#   r]      s   zSyncClient.rpcc                 C   s2   | j d u r| j| j| jj| jj| jjd| _ | j S )N)r:   r7   rY   timeout)rB   _init_postgrest_clientr:   r,   r7   rY   postgrest_client_timeoutr   r"   r"   r#   rZ      s   
zSyncClient.postgrestc                 C   s,   | j d u r| j| j| jj| jjd| _ | j S )N)r<   r7   storage_client_timeout)rC   _init_storage_clientr<   r,   r7   rb   ra   r"   r"   r#   r-      s   
zSyncClient.storagec                 C   s(   | j d u rt| j| jj| jj| _ | j S r   )rD   r   r=   r,   r7   function_client_timeoutra   r"   r"   r#   	functions   s   
zSyncClient.functionstopicc                 C   s   | j ||S )zJCreates a Realtime channel with Broadcast, Presence, and Postgres Changes.)rA   channel)r   rf   r\   r"   r"   r#   rg      s   zSyncClient.channelc                 C   s
   | j  S )zReturns all realtime channels.)rA   get_channelsra   r"   r"   r#   rh      s   
zSyncClient.get_channelsrg   c                 C   s   | j | dS )z?Unsubscribes and removes Realtime channel from Realtime client.N)rA   remove_channel)r   rg   r"   r"   r#   ri      s   zSyncClient.remove_channelc                 C   s   | j   dS )zDUnsubscribes and removes all Realtime channels from Realtime client.N)rA   remove_all_channelsra   r"   r"   r#   rj      s   zSyncClient.remove_all_channelsr3   c                 C   s"   |d u ri }	 t | fd|i|S )Ntoken)r   r2   r"   r"   r#   r@      s   z SyncClient._init_realtime_clientTr<   r7   rb   verifyproxyc                 C   s   t | ||||S r   r   )r<   r7   rb   rl   rm   r"   r"   r#   rc      s   
zSyncClient._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-   r7   	flow_typerl   rm   )r   ro   rp   r-   r7   rq   )r0   r1   rl   rm   r"   r"   r#   r>      s   z%SyncClient._init_supabase_auth_clientr:   r^   c                 C   s   t | |||||dS )z@Private helper for creating an instance of the Postgrest client.)r7   rY   r^   rl   rm   )r   )r:   r7   rY   r^   rl   rm   r"   r"   r#   r_      s   
z!SyncClient._init_postgrest_clientrk   c                 C   s
   d| S )NzBearer r"   )r   rk   r"   r"   r#   rJ     s   
zSyncClient._create_auth_headerauthorizationc                 C   s.   |d u r| j jd| | j}	 | j|dS )NrG   )apiKeyrG   )r,   r7   rH   rJ   r+   )r   rr   r"   r"   r#   r9     s   zSyncClient._get_auth_headerseventrP   c                 C   sF   | j }|dv rd | _d | _d | _|r|jn| j }| || jjd< d S )N)	SIGNED_INTOKEN_REFRESHED
SIGNED_OUTrG   )r+   rB   rC   rD   rK   rJ   r,   r7   )r   rt   rP   rK   r"   r"   r#   rF   "  s   z!SyncClient._listen_to_auth_eventsr   )rU   N)TN)2r$   r%   r&   __doc__r'   r   r6   r   classmethodrS   r   rX   r   rY   rV   r   r   r   r]   propertyrZ   r-   re   r   r   rg   r   rh   ri   rj   staticmethodr   r@   DEFAULT_STORAGE_CLIENT_TIMEOUTintboolr   rc   r   r>   r   r   floatr
   r_   rJ   r9   r   r	   rF   r"   r"   r"   r#   r)      s    
>	


	





 r)   r*   r+   r,   rU   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*   r+   r,   )r)   rS   r   r"   r"   r#   create_client0  s   r   r   ))r4   typingr   r   r   r   r   gotruer   gotrue.typesr   r	   httpxr
   rZ   r   r   r   postgrest.constantsr   rA   r   r   r   storage3r   storage3.constantsr   r|   supafuncr   lib.client_optionsr   r6   auth_clientr   rL   r   r)   r'   r   r"   r"   r"   r#   <module>   s6      