o
    `gK                     @  s   d dl mZ d dlmZmZmZmZ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mZmZmZmZmZmZ G d	d
 d
ZdS )    )annotations)AnyDictListOptionalUnion)parse_obj_as   )APIError)check_responseencode_uri_component)
SyncClient)CookieOptionsLinkTypeProviderSessionUserUserAttributes-determine_session_or_user_model_from_responsec                   @  sR  e Zd ZdddddbddZdcddZddddZddddZdeddZdfd d!Zddd"dgd)d*Z	dd+dhd-d.Z
dd/did1d2Zdjd3d4Zdd+dkd6d7Zdld8d9Zdd+dmd;d<Zddd"dnd=d>Zdd+dod?d@ZdpdBdCZdqdDdEZdddFdrdJdKZdsdLdMZdtdNdOZdPdQdudTdUZdvdWdXZddddYdwd\d]Zd^d_ Zd`da ZdS )xSyncGoTrueAPINT)http_clientverifyproxyurlstrheadersDict[str, str]cookie_optionsr   r   Optional[SyncClient]r   boolr   Optional[str]returnNonec                C  s0   || _ || _|| _|ptt||ddd| _dS )zInitialise API class.T)r   r   follow_redirectshttp2N)r   r   r   r   r   r   )selfr   r   r   r   r   r    r&   K/var/www/html/propose/venv/lib/python3.10/site-packages/gotrue/_sync/api.py__init__   s   zSyncGoTrueAPI.__init__c                 C  s   | S Nr&   r%   r&   r&   r'   	__enter__+   s   zSyncGoTrueAPI.__enter__c                 C  s   |    d S r)   )close)r%   exc_texc_vexc_tbr&   r&   r'   __exit__.   s   zSyncGoTrueAPI.__exit__c                 C  s   | j   d S r)   )r   acloser*   r&   r&   r'   r,   1   s   zSyncGoTrueAPI.close
attributesr   r   c                C  s6   | j }| }| j d}| jj|||d}t|S )a  Creates a new user.

        This function should only be called on a server.
        Never expose your `service_role` key in the browser.

        Parameters
        ----------
        attributes: UserAttributes
            The data you want to create the user with.

        Returns
        -------
        response : User
            The created user

        Raises
        ------
        APIError
            If an error occurs.
        /admin/usersjsonr   )r   dictr   r   postr   parse_response)r%   r2   r   datar   responser&   r&   r'   create_user4   s
   
zSyncGoTrueAPI.create_user
List[User]c                 C  sl   | j }| j d}| jj||d}t| | d}|du r%tddt|ts/tddt	t
t |S )a?  Get a list of users.

        This function should only be called on a server.
        Never expose your `service_role` key in the browser.

        Returns
        -------
        response : List[User]
            A list of users

        Raises
        ------
        APIError
            If an error occurs.
        r3   r   usersNzNo users found in responsei  zExpected a list of users)r   r   r   getr   r5   r
   
isinstancelistr   r   r   )r%   r   r   r:   r>   r&   r&   r'   
list_usersO   s   


zSyncGoTrueAPI.list_users)redirect_tor9   emailpasswordrC   r9   Optional[Dict[str, Any]]Union[Session, User]c                C  s`   | j }d}|rt|}d| }|||d}| j d| }| jj|||d}	t|	}
|
|	S )a  Creates a new user using their email address.

        Parameters
        ----------
        email : str
            The email address of the user.
        password : str
            The password of the user.
        redirect_to : Optional[str]
            A URL or mobile address to send the user to after they are confirmed.
        data : Optional[Dict[str, Any]]
            Optional user metadata.

        Returns
        -------
        response : Union[Session, User]
            A logged-in session if the server has "autoconfirm" ON
            A user if the server has "autoconfirm" OFF

        Raises
        ------
        APIError
            If an error occurs.
         ?redirect_to=)rD   rE   r9   /signupr4   r   r   r   r   r7   r   r8   )r%   rD   rE   rC   r9   r   query_stringredirect_to_encodedr   r:   session_or_user_modelr&   r&   r'   sign_up_with_emailj   s    

z SyncGoTrueAPI.sign_up_with_email)rC   r   c          
      C  sZ   | j }d}|rt|}|d| 7 }||d}| j d| }| jj|||d}	t|	S )a  Logs in an existing user using their email address.

        Parameters
        ----------
        email : str
            The email address of the user.
        password : str
            The password of the user.
        redirect_to : Optional[str]
            A URL or mobile address to send the user to after they are confirmed.

        Returns
        -------
        response : Session
            A logged-in session

        Raises
        ------
        APIError
            If an error occurs.
        z?grant_type=passwordz&redirect_to=)rD   rE   z/tokenr4   )r   r   r   r   r7   r   r8   )
r%   rD   rE   rC   r   rL   rM   r9   r   r:   r&   r&   r'   sign_in_with_email   s   

z SyncGoTrueAPI.sign_in_with_email)r9   phonec                C  sB   | j }|||d}| j d}| jj|||d}t|}||S )aF  Signs up a new user using their phone number and a password.

        Parameters
        ----------
        phone : str
            The phone number of the user.
        password : str
            The password of the user.
        data : Optional[Dict[str, Any]]
            Optional user metadata.

        Returns
        -------
        response : Union[Session, User]
            A logged-in session if the server has "autoconfirm" ON
            A user if the server has "autoconfirm" OFF

        Raises
        ------
        APIError
            If an error occurs.
        )rQ   rE   r9   rJ   r4   )r   r   r   r7   r   r8   )r%   rQ   rE   r9   r   r   r:   rN   r&   r&   r'   sign_up_with_phone   s   
z SyncGoTrueAPI.sign_up_with_phonec                C  s8   ||d}| j  d}| j}| jj|||d}t|S )a  Logs in an existing user using their phone number and password.

        Parameters
        ----------
        phone : str
            The phone number of the user.
        password : str
            The password of the user.

        Returns
        -------
        response : Session
            A logged-in session

        Raises
        ------
        APIError
            If an error occurs.
        )rQ   rE   z/token?grant_type=passwordr4   r   r   r   r7   r   r8   )r%   rQ   rE   r9   r   r   r:   r&   r&   r'   sign_in_with_phone   s
   

z SyncGoTrueAPI.sign_in_with_phoner;   c          
      C  sT   | j }d}|rt|}d| }||d}| j d| }| jj|||d}	t|	S )ab  Sends a magic login link to an email address.

        Parameters
        ----------
        email : str
            The email address of the user.
        redirect_to : Optional[str]
            A URL or mobile address to send the user to after they are confirmed.

        Raises
        ------
        APIError
            If an error occurs.
        rH   rI   )rD   r;   z
/magiclinkr4   r   r   r   r   r7   r   )
r%   rD   r;   rC   r   rL   rM   r9   r   r:   r&   r&   r'   send_magic_link_email   s   

z#SyncGoTrueAPI.send_magic_link_emailc                C  s6   | j }||d}| j d}| jj|||d}t|S )a#  Sends a mobile OTP via SMS. Will register the account if it doesn't already exist

        Parameters
        ----------
        phone : str
            The user's phone number WITH international prefix

        Raises
        ------
        APIError
            If an error occurs.
        )rQ   r;   z/otpr4   )r   r   r   r7   r   )r%   rQ   r;   r   r9   r   r:   r&   r&   r'   send_mobile_otp  s
   
zSyncGoTrueAPI.send_mobile_otptokenc          
      C  sV   | j }||dd}|rt|}||d< | j d}| jj|||d}t|}	|	|S )a  Send User supplied Mobile OTP to be verified

        Parameters
        ----------
        phone : str
            The user's phone number WITH international prefix
        token : str
            Token that user was sent to their mobile phone
        redirect_to : Optional[str]
            A URL or mobile address to send the user to after they are confirmed.

        Returns
        -------
        response : Union[Session, User]
            A logged-in session if the server has "autoconfirm" ON
            A user if the server has "autoconfirm" OFF

        Raises
        ------
        APIError
            If an error occurs.
        sms)rQ   rX   typerC   z/verifyr4   rK   )
r%   rQ   rX   rC   r   r9   rM   r   r:   rN   r&   r&   r'   verify_mobile_otp1  s   
zSyncGoTrueAPI.verify_mobile_otpc          	      C  sV   | j }d}|rt|}d| }||d}| j d| }| jj|||d}t|S )a  Sends an invite link to an email address.

        Parameters
        ----------
        email : str
            The email address of the user.
        redirect_to : Optional[str]
            A URL or mobile address to send the user to after they are confirmed.
        data : Optional[Dict[str, Any]]
            Optional user metadata.

        Returns
        -------
        response : User
            A user

        Raises
        ------
        APIError
            If an error occurs.
        rH   rI   )rD   r9   z/inviter4   )r   r   r   r   r7   r   r8   )	r%   rD   rC   r9   r   rL   rM   r   r:   r&   r&   r'   invite_user_by_email\  s   


z"SyncGoTrueAPI.invite_user_by_emailc          	      C  sR   | j }d}|rt|}d| }d|i}| j d| }| jj|||d}t|S )a_  Sends a reset request to an email address.

        Parameters
        ----------
        email : str
            The email address of the user.
        redirect_to : Optional[str]
            A URL or mobile address to send the user to after they are confirmed.

        Raises
        ------
        APIError
            If an error occurs.
        rH   rI   rD   z/recoverr4   rU   )	r%   rD   rC   r   rL   rM   r9   r   r:   r&   r&   r'   reset_password_for_email  s   
z&SyncGoTrueAPI.reset_password_for_emailjwtc                C  s   i | j dd| i}|S )a  Create temporary object.

        Create a temporary object with all configured headers and adds the
        Authorization token to be used on request methods.

        Parameters
        ----------
        jwt : str
            A valid, logged-in JWT.

        Returns
        -------
        headers : dict of str
            The headers required for a successful request statement with the
            supabase backend.
        AuthorizationzBearer r=   )r%   r^   r   r&   r&   r'   _create_request_headers  s   z%SyncGoTrueAPI._create_request_headersc                C  s,   | j |d}| j d}| jj||d dS )zRemoves a logged-in session.

        Parameters
        ----------
        jwt : str
            A valid, logged-in JWT.
        r^   z/logoutr=   N)r`   r   r   r7   )r%   r^   r   r   r&   r&   r'   sign_out  s   zSyncGoTrueAPI.sign_out)rC   scopesproviderr   rc   c                C  sZ   dt | g}|rt |}|d|  |r"|dt |  | j dd| S )aR  Generates the relevant login URL for a third-party provider.

        Parameters
        ----------
        provider : Provider
            One of the providers supported by GoTrue.
        redirect_to : Optional[str]
            A URL or mobile address to send the user to after they are confirmed.
        scopes : Optional[str]
            A space-separated list of scopes granted to the OAuth application.

        Returns
        -------
        url : str
            The URL to redirect the user to.

        Raises
        ------
        APIError
            If an error occurs.
        z	provider=zredirect_to=zscopes=z/authorize?&)r   appendr   join)r%   rd   rC   rc   
url_paramsrM   r&   r&   r'   get_url_for_provider  s   z"SyncGoTrueAPI.get_url_for_providerc                C  s2   | j |d}| j d}| jj||d}t|S )a  Gets the user details.

        Parameters
        ----------
        jwt : str
            A valid, logged-in JWT.

        Returns
        -------
        response : User
            A user

        Raises
        ------
        APIError
            If an error occurs.
        ra   /userr=   )r`   r   r   r?   r   r8   )r%   r^   r   r   r:   r&   r&   r'   get_user  s   
zSyncGoTrueAPI.get_userc                C  s<   | j |d}| }| j d}| jj|||d}t|S )an  
        Updates the user data.

        Parameters
        ----------
        jwt : str
            A valid, logged-in JWT.
        attributes : UserAttributes
            The data you want to update.

        Returns
        -------
        response : User
            A user

        Raises
        ------
        APIError
            If an error occurs.
        ra   rj   r4   )r`   r6   r   r   putr   r8   )r%   r^   r2   r   r9   r   r:   r&   r&   r'   update_user  s
   
zSyncGoTrueAPI.update_userF)should_soft_deleteuidrn   c                C  s>   | j |d}| j d| }d|i}| jj|||d}t|S )aU  Delete a user. Requires a `service_role` key.

        This function should only be called on a server.
        Never expose your `service_role` key in the browser.

        Parameters
        ----------
        uid : str
            The user uid you want to remove.
        jwt : str
            A valid, logged-in JWT.
        should_soft_delete : bool
            If true, then the user will be soft-deleted from the auth schema.

        Returns
        -------
        response : User
            A user

        Raises
        ------
        APIError
            If an error occurs.
        ra   z/admin/users/rn   r4   )r`   r   r   deleter   )r%   ro   r^   rn   r   r   bodyr:   r&   r&   r'   delete_user  s   zSyncGoTrueAPI.delete_userrefresh_tokenc                C  s6   d|i}| j  d}| j}| jj|||d}t|S )a@  Generates a new JWT.

        Parameters
        ----------
        refresh_token : str
            A valid refresh token that was returned on login.

        Returns
        -------
        response : Session
            A session

        Raises
        ------
        APIError
            If an error occurs.
        rs   z/token?grant_type=refresh_tokenr4   rS   )r%   rs   r9   r   r   r:   r&   r&   r'   refresh_access_token>  s
   
z"SyncGoTrueAPI.refresh_access_token)rE   rC   r9   rZ   r   c                C  sb   | j }|||d}|r||d< |rt|}||d< | j d}| jj|||d}	t|	}
|
|	S )a0  
        Generates links to be sent via email or other.

        Parameters
        ----------
        type : LinkType
            The link type ("signup" or "magiclink" or "recovery" or "invite").
        email : str
            The user's email.
        password : Optional[str]
            User password. For signup only.
        redirect_to : Optional[str]
            The link type ("signup" or "magiclink" or "recovery" or "invite").
        data : Optional[Dict[str, Any]]
            Optional user metadata. For signup only.

        Returns
        -------
        response : Union[Session, User]
            A logged-in session if the server has "autoconfirm" ON
            A user if the server has "autoconfirm" OFF

        Raises
        ------
        APIError
            If an error occurs.
        )rZ   rD   r9   rE   rC   z/admin/generate_linkr4   rK   )r%   rZ   rD   rE   rC   r9   r   rM   r   r:   rN   r&   r&   r'   generate_linkV  s   $
zSyncGoTrueAPI.generate_linkc                C     t d)Stub for parity with JS api.z set_auth_cookie not implemented.NotImplementedError)r%   reqresr&   r&   r'   set_auth_cookie     zSyncGoTrueAPI.set_auth_cookiec                C  rv   )rw   z#get_user_by_cookie not implemented.rx   )r%   rz   r&   r&   r'   get_user_by_cookie  r}   z SyncGoTrueAPI.get_user_by_cookie)r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   )r!   r   )r!   r"   )r2   r   r!   r   )r!   r<   )
rD   r   rE   r   rC   r    r9   rF   r!   rG   )rD   r   rE   r   rC   r    r!   r   )rQ   r   rE   r   r9   rF   r!   rG   )rQ   r   rE   r   r!   r   )rD   r   r;   r   rC   r    r!   r"   )rQ   r   r;   r   r!   r"   )rQ   r   rX   r   rC   r    r!   rG   )rD   r   rC   r    r9   rF   r!   r   )rD   r   rC   r    r!   r"   )r^   r   r!   r   )r^   r   r!   r"   )rd   r   rC   r    rc   r    r!   r   )r^   r   r!   r   )r^   r   r2   r   r!   r   )ro   r   r^   r   rn   r   r!   r"   )rs   r   r!   r   )rZ   r   rD   r   rE   r    rC   r    r9   rF   r!   rG   )__name__
__module____qualname__r(   r+   r0   r,   r;   rB   rO   rP   rR   rT   rV   rW   r[   r\   r]   r`   rb   ri   rk   rm   rr   rt   ru   r|   r~   r&   r&   r&   r'   r      sX    




 0,
$$
/*


$
!
#4r   N)
__future__r   typingr   r   r   r   r   pydanticr   
exceptionsr
   helpersr   r   http_clientsr   typesr   r   r   r   r   r   r   r   r&   r&   r&   r'   <module>   s    $