o
    d۷il                     @   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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Identity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   V/home/ubuntu/vllm_env/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   s0   t  }| jdd|jid}|  t|jS )Nz/login/device/authorization	client_iddata)r   getpostr   raise_for_statusr   model_validate_jsontext)r   settingsresponser   r   r   _start_device_authorization   s   
r(   r   r   c                 C   s   t  }	 | jd||jddd}|jdvr|  |jdkr.| }|dd	kr.|  |jd
kr4nt| qt	
|j}|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"   r   status_coder#   jsontimesleepr   r$   r%   r   )r   r   r   r&   r'   r    response_datar   r   r   _fetch_access_token+   s*   
	


r3   c                  C   s  t  } |  r)tdd}|d |d W d   dS 1 s"w   Y  dS t }t }|jddd |  |d	/}t| t	|}W d   n1 sUw   Y  |j
}|d
| d| d W d   n1 ssw   Y  |  |d5}t| t| t||j|j}W d   n1 sw   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. 🚀
    T)minimalzYou are already logged in.zKRun [bold]fastapi cloud logout[/bold] first if you want to switch accounts.NzLogin to FastAPI CloudFastAPI)tagzStarting authorizationzOpening [link=]z[/link]z Waiting for user to authorize...)r   u   Now you are logged in! 🚀)r   is_logged_inr	   printr   print_title
print_lineprogressr
   r(   r   logtyperlaunchr3   r   r   r   r   )identitytoolkitr   r<   authorization_dataurlr   r   r   r   loginK   sD   






PrD   )loggingr0   typingr   httpxr>   pydanticr   fastapi_cloud_cli.configr   fastapi_cloud_cli.utils.apir   fastapi_cloud_cli.utils.authr   r   r   fastapi_cloud_cli.utils.clir	   r
   	getLoggerr   loggerr   r   Clientr(   r   r   r3   rD   r   r   r   r   <module>   s(    

 