o
    #1 i                     @   s  d dl 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mZ d dlZd dlmZmZmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ e e!Z"dZ#eddZ$G dd de%Z&G dd de%Z'G dd deZ(G dd deZ)ee)e(f Z*ee* ee*eddf Z+ede,ded fddZ-edZ.edZ/deddfd e,d!ede
e
e.ee/ddf f ge
e.ee/ddf f f fd"d#Z0G d$d% d%ej1Z2dS )&    N)contextmanager)	timedelta)wraps)Callable	GeneratorLiteralOptionalTypeVarUnion)	BaseModelFieldValidationError)	Annotated	ParamSpec)__version__)Settings)get_auth_token)TypeAdapter      )minutesc                   @      e Zd ZdS )BuildLogErrorN__name__
__module____qualname__ r   r   g/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/fastapi_cloud_cli/utils/api.pyr          r   c                   @   r   )TooManyRetriesErrorNr   r   r   r   r   r    #   r   r    c                   @   s*   e Zd ZU ed ed< dZee ed< dS )BuildLogLineGeneric)completefailedtimeout	heartbeattypeNid)r   r   r   r   __annotations__r'   r   strr   r   r   r   r!   '   s   
 r!   c                   @   s6   e Zd ZU dZed ed< eed< dZee ed< dS )BuildLogLineMessagemessager&   Nr'   )	r   r   r   r&   r   r(   r)   r'   r   r   r   r   r   r*   ,   s   
 r*   r&   )Zdiscriminatorattempt_numberreturn)NNNc                 #   s    d
 fdd}zd V  W d S  t jt jt jfy0 } ztd| |  W Y d }~d S d }~w t jyu } z9|jjdkrKtd|jj| |  nz|jj	}W n t
y\   d}Y nw td|jj d	| |W Y d }~d S d }~ww )Nr-   c                     s*   t d  d} td|   t|  d S )N      zRetrying in %ds (attempt %d))minloggerdebugtimesleep)Zbackoff_secondsr,   r   r   _backoff:   s   zattempt.<locals>._backoffzNetwork error (will retry): %si  z Server error %d (will retry): %sz(response body unavailable)zHTTP z: r-   N)httpxZTimeoutExceptionNetworkErrorZRemoteProtocolErrorr1   r2   ZHTTPStatusErrorresponsestatus_codetext	Exceptionr   )r,   r6   errorZerror_detailr   r5   r   attempt8   sB   	r?   PTtotal_attemptsr$   c              	      s@   dt tttd d f f dt tttd d f f f fdd}|S )Nfuncr-   c              
      s8   t  dtjdtjdttd d f f fdd}|S )Nargskwargsr-   c               	   ?   s    t  }tD ]4}t  |  krtd t|  | i |E d H  	 W d     d S 1 s8w   Y  q	td d)Nz'Build log streaming timed out after %dszFailed after z	 attempts)r3   	monotonicrangetotal_secondsTimeoutErrorr?   r    )rD   rE   startr,   )rC   r$   rB   r   r   wrappern   s   
$z,attempts.<locals>.decorator.<locals>.wrapper)r   r@   rD   rE   r   rA   )rC   rK   r$   rB   )rC   r   	decoratork   s   .zattempts.<locals>.decorator)r   r@   r   rA   )rB   r$   rM   r   rL   r   attemptsf   s   rN   c                       sZ   e Zd Zd fddZeeededee	ddf fddZ
dedee	 fd	d
Z  ZS )	APIClientr-   Nc                    s>   t  }t }t j|jtdd| dt dd d S )N   zBearer zfastapi-cloud-cli/)Authorizationz
User-Agent)base_urlr$   headers)	r   getr   super__init__Zbase_api_urlr8   Timeoutr   )selfsettingstoken	__class__r   r   rV      s   
zAPIClient.__init__deployment_idc                 c   s    d }	 |r
d|ind }| j dd| dd|dV}|  | D ];}|r*| s+q"| | }r]|jr8|j}|jdkr@|V  |jd	v rQ|V   W d    d S |jd
kr]td  nq"td t	
dW d    n1 srw   Y  td q)NTlast_idGETz/deployments/z/build-logs<   )r$   paramsr+   )r"   r#   r$   zReceived timeout; reconnectingz4Connection closed by server unexpectedly; will retryz(Connection closed without terminal stateg      ?)streamraise_for_status
iter_linesstrip_parse_log_liner'   r&   r1   r2   r8   r9   r3   r4   )rX   r]   r^   ra   r:   lineZlog_liner   r   r   stream_build_logs   sD   







zAPIClient.stream_build_logsrg   c              
   C   sP   zt |W S  ttjfy' } ztd|d d | W Y d }~d S d }~ww )Nz&Skipping malformed log: %s (error: %s)d   )BuildLogAdapterZvalidate_jsonr   jsonJSONDecodeErrorr1   r2   )rX   rg   er   r   r   rf      s   zAPIClient._parse_log_liner7   )r   r   r   rV   rN   BUILD_LOG_MAX_RETRIESBUILD_LOG_TIMEOUTr)   r   BuildLogLinerh   r   rf   __classcell__r   r   r[   r   rO      s    )rO   )3rk   loggingr3   
contextlibr   datetimer   	functoolsr   typingr   r   r   r   r	   r
   r8   Zpydanticr   r   r   Ztyping_extensionsr   r   Zfastapi_cloud_clir   Zfastapi_cloud_cli.configr   Zfastapi_cloud_cli.utils.authr   Z'fastapi_cloud_cli.utils.pydantic_compatr   	getLoggerr   r1   rn   ro   r=   r   r    r!   r*   rp   rj   intr?   r@   rA   rN   ZClientrO   r   r   r   r   <module>   sP     	

)
,
