U
    Wi,                     @   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e!e!eedf e d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ed fddZ  ZS )SupabaseException)messagec                    s   || _ t | j  d S N)r   super__init__)selfr   	__class__ \/home/afrizal/public_html/fastapi/venv/lib/python3.8/site-packages/supabase/_async/client.pyr      s    zSupabaseException.__init__)__name__
__module____qualname__strr   __classcell__r   r   r   r   r      s   r   c                	   @   sz  e Zd ZdZd,eeeedf dddZed-eeeedf dddZ	ee
dd	d
ZeedddZee
dddZd.eeeeef  edddZedd Zedd Zedd Zeedfeeeef eeedddZeeeedddZee dfeeeef eeee!e"f eed d!d"Z#ed#d$d%Z$d/eedf eeef d&d'd(Z%e&ee'df d)d*d+Z(dS )0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krTt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replaceZrealtime_urlr.   storage_urlfunctions_url_init_supabase_auth_clientauthZrealtime
_postgrest_storage
_functionsZ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 krz |j I d H }||j}W n& tk
rn } zd }W 5 d }~X Y nX |jj |	| |S )NAuthorization)
r2   getr:   Zget_session_create_auth_headeraccess_token	Exceptionr)   r3   r4   )	clsr'   r(   r)   auth_headerclientsessionZ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   rJ   r   r   r   tableu   s    zAsyncClient.table)schemarK   c                 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.
        r5   r2   rO   timeout)_init_postgrest_clientr5   r)   r2   postgrest_client_timeoutr;   )r   rO   r   r   r   rO   ~   s    zAsyncClient.schemac                 C   s   | j |S )zDPerform a table operation.

        See the `table` method.
        )	postgrestrL   rM   r   r   r   rL      s    zAsyncClient.from_)fnparamsrK   c                 C   s   |dk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)rT   rpc)r   rU   rV   r   r   r   rW      s    zAsyncClient.rpcc                 C   s2   | j d kr,| j| j| jj| jj| jjd| _ | j S )NrP   )r;   rR   r5   r)   r2   rO   rS   r   r   r   r   rT      s    
zAsyncClient.postgrestc                 C   s,   | j d kr&| j| j| jj| jjd| _ | j S )N)r7   r2   storage_client_timeout)r<   _init_storage_clientr7   r)   r2   rY   rX   r   r   r   r*      s    
zAsyncClient.storagec                 C   s(   | j d kr"t| j| jj| jj| _ | j S r   )r=   r   r8   r)   r2   Zfunction_client_timeoutrX   r   r   r   	functions   s    
zAsyncClient.functionsT)r7   r2   rY   verifyrK   c                 C   s   t | |||S r   r   )r7   r2   rY   r\   r   r   r   rZ      s    z AsyncClient._init_storage_client)r.   r/   rK   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*   r2   	flow_type)r   r^   r_   r*   r2   r`   r-   r   r   r   r9      s    z&AsyncClient._init_supabase_auth_client)r5   r2   rO   rQ   r\   rK   c                 C   s   t | ||||dS )z@Private helper for creating an instance of the Postgrest client.)r2   rO   rQ   r\   )r
   )r5   r2   rO   rQ   r\   r   r   r   rR     s    	z"AsyncClient._init_postgrest_client)tokenc                 C   s
   d| S )NzBearer r   )r   ra   r   r   r   rA     s    zAsyncClient._create_auth_header)authorizationrK   c                 C   s,   |d kr | j jd| | j}| j|dS )Nr?   )ZapiKeyr?   )r)   r2   r@   rA   r(   )r   rb   r   r   r   r4     s     
zAsyncClient._get_auth_headers)eventrG   c                 C   sF   | j }|dkr0d | _d | _d | _|r*|jn| j }| || jjd< d S )N)Z	SIGNED_INZTOKEN_REFRESHEDZ
SIGNED_OUTr?   )r(   r;   r<   r=   rB   rA   r)   r2   )r   rc   rG   rB   r   r   r   r>   %  s    z"AsyncClient._listen_to_auth_events)N)N)N)N))r    r!   r"   __doc__r#   r   r   r   classmethodrI   r   rN   r
   rO   rL   r   r   r   r   rW   propertyrT   r*   r[   staticmethodDEFAULT_STORAGE_CLIENT_TIMEOUTintboolr   rZ   r   r9   r   floatr	   rR   rA   r4   r   r   r>   r   r   r   r   r%      s    
? 
	  

	
,

 

 
r%   )r'   r(   r)   rK   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&   N)r%   rI   r&   r   r   r   create_client3  s
      rl   )N)#r0   typingr   r   r   r   Zgotruer   Zgotrue.typesr   r   Zhttpxr	   rT   r
   r   r   Zpostgrest.constantsr   Zstorage3r   Zstorage3.constantsr   rh   Zsupafuncr   Zlib.client_optionsr   Zauth_clientr   rC   r   r%   r#   rl   r   r   r   r   <module>   s,      
