o
    /h,                  	   @   s   d dl Z d dl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 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OptionalUnion)AsyncMemoryStorage)AuthChangeEventSession)Timeout)AsyncPostgrestClientAsyncRequestBuilderAsyncRPCFilterRequestBuilder) DEFAULT_POSTGREST_CLIENT_TIMEOUT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__ O/var/www/html/govbot/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	d6dededeedf fddZe	d6d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	d6d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eedfdedeeef d ed!edef
d"d#Zed$ed%edefd&d'Zee dfd(edeeef ded)eee!e"f d!edefd*d+Z#d,efd-d.Z$	d6d/eedf deeef fd0d1Z%d2e&d3ee'df fd4d5Z(dS )7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| _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)r   rematchr   r   r&   r'   r(   headersupdate_get_auth_headersrest_urlreplacerealtime_urlr-   storage_urlfunctions_url_init_supabase_auth_clientauthrealtime
_postgrest_storage
_functionson_auth_state_change_listen_to_auth_events)r   r&   r'   r(   r   r   r   r      s<   	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)
r1   getr:   get_session_create_auth_headeraccess_token	Exceptionr(   r2   r3   )	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   rN   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.
        r4   r1   rS   timeout)_init_postgrest_clientr4   r(   r1   postgrest_client_timeoutr<   )r   rS   r   r   r   rS   ~   s   zAsyncClient.schemac                 C   s   | j |S )zDPerform a table operation.

        See the `table` method.
        )	postgrestrP   rQ   r   r   r   rP      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)rX   rpc)r   rY   rZ   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 )NrT   )r<   rV   r4   r(   r1   rS   rW   r   r   r   r   rX      s   
zAsyncClient.postgrestc                 C   s,   | j d u r| j| j| jj| jjd| _ | j S )N)r7   r1   storage_client_timeout)r=   _init_storage_clientr7   r(   r1   r]   r\   r   r   r   r)      s   
zAsyncClient.storagec                 C   s(   | j d u rt| j| jj| jj| _ | j S r   )r>   r   r8   r(   r1   function_client_timeoutr\   r   r   r   	functions   s   
zAsyncClient.functionsTr7   r1   r]   verifyc                 C   s   t | |||S r   r   )r7   r1   r]   ra   r   r   r   r^      s   z AsyncClient._init_storage_clientr-   r.   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)   r1   	flow_type)r   rc   rd   r)   r1   re   r,   r   r   r   r9      s   z&AsyncClient._init_supabase_auth_clientr4   rU   c                 C   s   t | ||||dS )z@Private helper for creating an instance of the Postgrest client.)r1   rS   rU   ra   )r
   )r4   r1   rS   rU   ra   r   r   r   rV     s   	z"AsyncClient._init_postgrest_clienttokenc                 C   s
   d| S )NzBearer r   )r   rf   r   r   r   rD     s   
zAsyncClient._create_auth_headerauthorizationc                 C   s.   |d u r| j jd| | j}	 | j|dS )NrA   )apiKeyrA   )r(   r1   rB   rD   r'   )r   rg   r   r   r   r3     s   zAsyncClient._get_auth_headerseventrJ   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_OUTrA   )r'   r<   r=   r>   rE   rD   r(   r1   )r   ri   rJ   rE   r   r   r   r@   %  s   z"AsyncClient._listen_to_auth_eventsr   ))r    r!   r"   __doc__r#   r   r   r   classmethodrM   r   rR   r
   rS   rP   r   r   r   r   r[   propertyrX   r)   r`   staticmethodDEFAULT_STORAGE_CLIENT_TIMEOUTintboolr   r^   r   r9   r   floatr	   rV   rD   r3   r   r   r@   r   r   r   r   r%      s    

?
	


	
,





r%   r&   r'   r(   rO   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%   rM   ru   r   r   r   create_client3  s   rv   r   )#r/   typingr   r   r   r   gotruer   gotrue.typesr   r   httpxr	   rX   r
   r   r   postgrest.constantsr   storage3r   storage3.constantsr   rq   supafuncr   lib.client_optionsr   auth_clientr   rF   r   r%   r#   rv   r   r   r   r   <module>   s4      
