o
    SDi
                     @   s   d dl Z d dlZd dlmZ d dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZmZ e eZG dd	 d	eZG d
d deZdejdefddZdejdededefddZdefddZdS )    N)Any)	BaseModel)settings)	APIClient)
AuthConfigwrite_auth_config)get_rich_toolkithandle_http_errorsc                   @   s:   e Zd ZU eed< eed< eed< eed< dZeed< dS )AuthorizationData	user_codedevice_codeverification_uriverification_uri_complete   intervalN)__name__
__module____qualname__str__annotations__r   int r   r   Y/var/www/Datamplify/venv/lib/python3.10/site-packages/fastapi_cloud_cli/commands/login.pyr
      s   
 r
   c                   @   s   e Zd ZU eed< dS )TokenResponseaccess_tokenN)r   r   r   r   r   r   r   r   r   r      s   
 r   clientreturnc                 C   s*   | j ddtjid}|  t| S )Nz/login/device/authorization	client_iddata)postr   r   raise_for_statusr
   model_validatejson)r   responser   r   r   _start_device_authorization   s
   
r%   r   r   c                 C   s   	 | j d|tjddd}|jdvr|  |jdkr*| }|dd	kr*|  |jd
kr0nt| qt	
| }|jS )NTz/login/device/tokenz,urn:ietf:params:oauth:grant-type:device_code)r   r   
grant_typer   )     r(   errorauthorization_pendingr'   )r    r   r   status_coder!   r#   gettimesleepr   r"   r   )r   r   r   r$   r   response_datar   r   r   _fetch_access_token)   s(   
	


r0   c                  C   st  t  } t }| jddd |   | d+}t| t|}W d   n1 s,w   Y  |j}|d|  W d   n1 sFw   Y  |   | d5}t	
| t| t||j|j}W d   n1 sqw   Y  tt|d |d	 W d   n1 sw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )
u&   
    Login to FastAPI Cloud. 🚀
    zLogin to FastAPI CloudFastAPI)tagzStarting authorizationNzOpening z Waiting for user to authorize...)r   u   Now you are logged in! 🚀)r   r   print_title
print_lineprogressr	   r%   r   logtyperlaunchr0   r   r   r   r   )toolkitr   r5   authorization_dataurlr   r   r   r   loginG   s.   




Pr<   )loggingr-   typingr   httpxr7   pydanticr   fastapi_cloud_cli.configr   fastapi_cloud_cli.utils.apir   fastapi_cloud_cli.utils.authr   r   fastapi_cloud_cli.utils.clir   r	   	getLoggerr   loggerr
   r   Clientr%   r   r   r0   r<   r   r   r   r   <module>   s(    

