
    gK                        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
      Zy)    )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                     e Zd Zdddd	 	 	 	 	 	 	 	 	 	 	 	 	 d$dZd%dZd&dZd&dZd'dZd(d	Zddd
	 	 	 	 	 	 	 	 	 d)dZ	dd	 	 	 	 	 	 	 d*dZ
dd	 	 	 	 	 	 	 d+dZ	 	 	 	 	 	 d,dZdd	 	 	 	 	 	 	 d-dZd.dZdd	 	 	 	 	 	 	 d/dZddd
	 	 	 	 	 	 	 d0dZdd	 	 	 	 	 d1dZd2dZd3dZddd	 	 	 	 	 	 	 d4dZd5dZ	 	 	 	 	 	 d6dZdd	 	 	 	 	 	 	 d7dZd8dZdddd 	 	 	 	 	 	 	 	 	 	 	 d9d!Zd" Zd# Zy):SyncGoTrueAPINT)http_clientverifyproxyc               p    || _         || _        || _        |xs t        t	        |      |dd      | _        y)zInitialise API class.T)r   r   follow_redirectshttp2N)urlheaderscookie_optionsr   boolr   )selfr   r   r    r   r   r   s          E/var/www/openai/venv/lib/python3.12/site-packages/gotrue/_sync/api.py__init__zSyncGoTrueAPI.__init__   s>     ,& 
*<!	+
    c                    | S N r"   s    r#   	__enter__zSyncGoTrueAPI.__enter__+   s    r%   c                $    | j                          y r'   )close)r"   exc_texc_vexc_tbs       r#   __exit__zSyncGoTrueAPI.__exit__.   s    

r%   c                8    | j                   j                          y r'   )r   acloser)   s    r#   r,   zSyncGoTrueAPI.close1   s    !r%   c                   | j                   }|j                         }| j                   d}| j                  j	                  |||      }t        j                  |      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"   
attributesr   datar   responses         r#   create_userzSyncGoTrueAPI.create_user4   sW    * ,, 
,'##((4(I""8,,r%   c                H   | j                   }| j                   d}| j                  j                  ||      }t	        |       |j                         j                  d      }|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.
        r4   r   userszNo users found in responsei  zExpected a list of users)r   r   r   getr   r6   r   
isinstancelistr	   r   r   )r"   r   r   r<   r@   s        r#   
list_userszSyncGoTrueAPI.list_usersO   s      ,,
,'##''W'=x ##G,=7==%&5s;;DJ..r%   )redirect_tor;   c                   | j                   }d}|rt        |      }d| }|||d}| j                   d| }| j                  j	                  |||      }	t        |	      }
|
j                  |	      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=)emailpasswordr;   /signupr5   r   r   r   r   r8   r   r9   )r"   rI   rJ   rE   r;   r   query_stringredirect_to_encodedr   r<   session_or_user_models              r#   sign_up_with_emailz SyncGoTrueAPI.sign_up_with_emailj   s    @ ,,"6{"C*+>*?@LHdC
',0##((4(I Mh W$33H==r%   )rE   c                   | j                   }d}|rt        |      }|d| z  }||d}| j                   d| }| j                  j	                  |||      }	t        j                  |	      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=)rI   rJ   z/tokenr5   )r   r   r   r   r8   r   r9   )
r"   rI   rJ   rE   r   rM   rN   r;   r   r<   s
             r#   sign_in_with_emailz SyncGoTrueAPI.sign_in_with_email   s    : ,,-"6{"Cm,?+@AALH5
&/##((4(I%%h//r%   )r;   c                   | j                   }|||d}| j                   d}| j                  j                  |||      }t	        |      }|j                  |      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.
        )phonerJ   r;   rK   r5   )r   r   r   r8   r   r9   )r"   rT   rJ   r;   r   r   r<   rO   s           r#   sign_up_with_phonez SyncGoTrueAPI.sign_up_with_phone   sb    : ,,HdC
'"##((4(I Mh W$33H==r%   c                   ||d}| j                    d}| j                  }| j                  j                  |||      }t	        j
                  |      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.
        )rT   rJ   z/token?grant_type=passwordr5   r   r   r   r8   r   r9   )r"   rT   rJ   r;   r   r   r<   s          r#   sign_in_with_phonez SyncGoTrueAPI.sign_in_with_phone   sV    2 H5
45,,##((4(I%%h//r%   c                   | j                   }d}|rt        |      }d| }||d}| j                   d| }| j                  j	                  |||      }	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.
        rG   rH   )rI   r=   z
/magiclinkr5   r   r   r   r   r8   r   )
r"   rI   r=   rE   r   rM   rN   r;   r   r<   s
             r#   send_magic_link_emailz#SyncGoTrueAPI.send_magic_link_email   st    * ,,"6{"C*+>*?@L{;
*\N3##((4(Ih''r%   c                   | j                   }||d}| j                   d}| j                  j                  |||      }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.
        )rT   r=   z/otpr5   )r   r   r   r8   r   )r"   rT   r=   r   r;   r   r<   s          r#   send_mobile_otpzSyncGoTrueAPI.send_mobile_otp  sO     ,,{;
$##((4(Ih''r%   c                   | j                   }||dd}|rt        |      }||d<   | j                   d}| j                  j	                  |||      }t        |      }	|	j                  |      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)rT   tokentyperE   z/verifyr5   rL   )
r"   rT   r`   rE   r   r;   rN   r   r<   rO   s
             r#   verify_mobile_otpzSyncGoTrueAPI.verify_mobile_otp1  s    : ,,

 "6{"C"5D
'"##((4(I Mh W$33H==r%   c                   | j                   }d}|rt        |      }d| }||d}| j                   d| }| j                  j	                  |||      }t        j                  |      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.
        rG   rH   )rI   r;   z/inviter5   )r   r   r   r   r8   r   r9   )	r"   rI   rE   r;   r   rM   rN   r   r<   s	            r#   invite_user_by_emailz"SyncGoTrueAPI.invite_user_by_email\  sz    8 ,,"6{"C*+>*?@L-
',0##((4(I""8,,r%   c                   | j                   }d}|rt        |      }d| }d|i}| j                   d| }| j                  j	                  |||      }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.
        rG   rH   rI   z/recoverr5   rZ   )	r"   rI   rE   r   rM   rN   r;   r   r<   s	            r#   reset_password_for_emailz&SyncGoTrueAPI.reset_password_for_email  st    ( ,,"6{"C*+>*?@L
(<.1##((4(Ih''r%   c               0    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"   jwtr   s      r#   _create_request_headersz%SyncGoTrueAPI._create_request_headers  s%    " ET\\D?gcUODr%   c                   | j                  |      }| j                   d}| j                  j                  ||       y)zRemoves a logged-in session.

        Parameters
        ----------
        jwt : str
            A valid, logged-in JWT.
        ri   z/logoutr?   N)rj   r   r   r8   )r"   ri   r   r   s       r#   sign_outzSyncGoTrueAPI.sign_out  s@     ..3.7
'"c73r%   )rE   scopesc                   dt        |       g}|rt        |      }|j                  d|        |r|j                  dt        |              | j                   ddj                  |       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"   providerrE   rn   
url_paramsrN   s         r#   get_url_for_providerz"SyncGoTrueAPI.get_url_for_provider  s}    8 ""6x"@!ABC
"6{"C-@,ABC(<V(D'EFG((;sxx
';&<==r%   c                   | j                  |      }| j                   d}| j                  j                  ||      }t	        j
                  |      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.
        rl   /userr?   )rj   r   r   rA   r   r9   )r"   ri   r   r   r<   s        r#   get_userzSyncGoTrueAPI.get_user  sS    $ ..3.7
% ##''W'=""8,,r%   c                   | j                  |      }|j                         }| j                   d}| j                  j	                  |||      }t        j                  |      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.
        rl   rw   r5   )rj   r7   r   r   putr   r9   )r"   ri   r:   r   r;   r   r<   s          r#   update_userzSyncGoTrueAPI.update_user  sa    4 ..3.7 
% ##''$'H""8,,r%   F)should_soft_deletec                   | j                  |      }| j                   d| }d|i}| j                  j                  |||      }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.
        rl   z/admin/users/r|   r5   )rj   r   r   deleter   )r"   uidri   r|   r   r   bodyr<   s           r#   delete_userzSyncGoTrueAPI.delete_user  sb    6 ..3.7
-u- "4
 ##**3T7*Kh''r%   c                   d|i}| j                    d}| j                  }| j                  j                  |||      }t	        j
                  |      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.
        refresh_tokenz/token?grant_type=refresh_tokenr5   rW   )r"   r   r;   r   r   r<   s         r#   refresh_access_tokenz"SyncGoTrueAPI.refresh_access_token>  sV    $  /
9:,,##((4(I%%h//r%   )rJ   rE   r;   c                   | j                   }|||d}|r||d<   |rt        |      }||d<   | j                   d}| j                  j	                  |||      }	t        |	      }
|
j                  |	      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.
        )ra   rI   r;   rJ   rE   z/admin/generate_linkr5   rL   )r"   ra   rI   rJ   rE   r;   r   rN   r   r<   rO   s              r#   generate_linkzSyncGoTrueAPI.generate_linkV  s    H ,,

 'D"6{"C"5D
./##((4(I Mh W$33H==r%   c                   t        d      )Stub for parity with JS api.z set_auth_cookie not implemented.NotImplementedError)r"   reqress      r#   set_auth_cookiezSyncGoTrueAPI.set_auth_cookie  s    !"DEEr%   c                   t        d      )r   z#get_user_by_cookie not implemented.r   )r"   r   s     r#   get_user_by_cookiez SyncGoTrueAPI.get_user_by_cookie  s    !"GHHr%   )r   strr   Dict[str, str]r    r   r   zOptional[SyncClient]r   r!   r   Optional[str]returnNone)r   r   )r   r   )r:   r   r   r   )r   z
List[User])
rI   r   rJ   r   rE   r   r;   Optional[Dict[str, Any]]r   Union[Session, User])rI   r   rJ   r   rE   r   r   r   )rT   r   rJ   r   r;   r   r   r   )rT   r   rJ   r   r   r   )rI   r   r=   r!   rE   r   r   r   )rT   r   r=   r!   r   r   )rT   r   r`   r   rE   r   r   r   )rI   r   rE   r   r;   r   r   r   )rI   r   rE   r   r   r   )ri   r   r   r   )ri   r   r   r   )rs   r   rE   r   rn   r   r   r   )ri   r   r   r   )ri   r   r:   r   r   r   )r   r   ri   r   r|   r!   r   r   )r   r   r   r   )ra   r   rI   r   rJ   r   rE   r   r;   r   r   r   )__name__
__module____qualname__r$   r*   r0   r,   r=   rD   rP   rR   rU   rX   r[   r]   rb   rd   rf   rj   rm   ru   rx   r{   r   r   r   r   r   r(   r%   r#   r   r      s    -1#
 
  	

 &
 *
 
 
 

*"-6/@ &*)-)> )> 	)>
 #)> ')> 
)>` &*%0 %0 	%0
 #%0 
%0X *."> "> 	">
 '"> 
">H0 0 	0
 
0H &*( ( 	(
 #( 
(>(0 &*)> )> 	)>
 #)> 
)>^ &*)-$- $- #	$-
 '$- 
$-T &*	( ( #	(
 
(<(
4  &* $"> "> #	">
 "> 
">H-.- - #	-
 
-B AF!(!( #!(9=!(	!(F0: #'%))-2> 2> 	2>
  2> #2> '2> 
2>hFIr%   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#   <module>r      s7    " 3 3 ! ! : %  {	I {	Ir%   