o
    #1 i                     @   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mZmZ d dlmZmZ d dl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get_auth_tokenis_logged_inis_token_expiredwrite_auth_config)get_rich_toolkithandle_http_errors)model_validate_jsonc                   @   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 )AuthorizationDataZ	user_codedevice_codeZverification_uriverification_uri_complete   intervalN)__name__
__module____qualname__str__annotations__r   int r   r   l/home/app/PaddleOCR-VL-test/.venv_paddleocr/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t|jS )Nz/login/device/authorization	client_iddata)r   getpostr   raise_for_statusr   r   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	t
|j}|jS )NTz/login/device/tokenz,urn:ietf:params:oauth:grant-type:device_code)r   r   Z
grant_typer    )     r*   errorZauthorization_pendingr)   )r   r"   r#   r   status_coder$   jsontimesleepr   r   r%   r   )r   r   r   r&   r'   r!   Zresponse_datar   r   r   _fetch_access_token2   s*   
	


r0   c                  C   s  t  } | dur)t| r)tdd}|d |  W d   n1 s$w   Y  t rNtdd}|d |d W d   dS 1 sGw   Y  dS t }t }|jddd	 |  |d
/}t	| t
|}W d   n1 szw   Y  |j}|d| d| d W d   n1 sw   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 sw   Y  dS )u&   
    Login to FastAPI Cloud. 🚀
    NT)Zminimalz-Your session has expired. Logging in again...zYou are already logged in.zKRun [bold]fastapi cloud logout[/bold] first if you want to switch accounts.zLogin to FastAPI CloudZFastAPI)tagzStarting authorizationzOpening [link=]z[/link]z Waiting for user to authorize...)r   u   Now you are logged in! 🚀)r   r	   r   printZ
print_liner   r   Zprint_titleprogressr   r(   r   logtyperZlaunchr0   r   r   r
   r   )tokenZtoolkitr   r4   Zauthorization_dataurlr   r   r   r   loginR   sN   








Rr9   )"loggingr.   typingr   Zhttpxr6   Zpydanticr   Zfastapi_cloud_cli.configr   Zfastapi_cloud_cli.utils.apir   Zfastapi_cloud_cli.utils.authr   r   r   r	   r
   Zfastapi_cloud_cli.utils.clir   r   Z'fastapi_cloud_cli.utils.pydantic_compatr   	getLoggerr   loggerr   r   ZClientr(   r   r   r0   r9   r   r   r   r   <module>   s*    

 