
    iS                   	   U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlZd dlZd dlZd dlZd dlZd dlZd dlmZmZ d dlmZmZ d dlmZmZ d dlmZmZ d dlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d d	l(m)Z) d dl*Z*d dl+Z+d d
l,m-Z-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z? e#rd dl@mAZA d dlBmCZC  ejD        eE          ZFe#rd dlGmHZH e!eIef         ZJe%d         ZKe!eIef         ZLe'e%d         eMf         ZNe%d         ZOde2 dZPde1 dZQde2 dZRdZSdZTejU        V                    dd          ZWdZXdeYd<   ddgZZd eYd!<   d"e6 d#Z[d$Z\ej]        ^                    d%&          Z_d'Z`de`d(d)d*d+d,dZad-eYd.<    ed/0           G d1 d2                      Zb ed/0           G d3 d4                      Zc ed/0           G d5 d6                      Zde G d7 d8                      Ze G d9 d:          Zf G d; d<          Zgd=Zhd>Zid?Zj G d@ dA          Zk G dB dC          Zl G dD dEem          Zne G dF dGen                      Zoe G dH dIenep                      ZqddNZre G dO dP                      Zse G dQ dRes                      Zte G dS dTes                      Zue G dU dVes                      Zv ed/0           G dW dX                      ZwdYdZgZxdd]Zy ed/0           G d^ d_                      Zzd`Z{daZ|dbZ}dddZ~ddhZdiddjddoZdpdqddrZdduZ G dv dwe          ZddzZdd{ZddZddZe G d d                      Ze G d d                      Ze	 ddd            Ze	 ddd            ZddZddZd?ZdZdZg dZg dZ e+j        dd          ZddZg fddZg fddZ	 	 dddZg dddZg dd dZddZddZddZddZddZddÄZddȄZddɄZddʜd	d˄Zddʜd
d̄Zdd̈́Zddʜd	d΄ZddτZddʜd
dЄZddӄZddՄZdd؄ZddڄZ ed/0           G dۄ dew                      Z ed/0           G d܄ dew                      Z ed/0           G d݄ dަ                      Z ed/0           G d߄ d                      ZddZddZdddZdS (      )annotationsN)Path)asynccontextmanagercontextmanager)datetimetimezone)	dataclassfield)cached_propertypartial)
AnyAsyncIterator	AwaitableDictIteratorTYPE_CHECKINGOptionalLiteralCallableUnion)	urlencode)aconnect_sseconnect_sse)AuthCredentialsFAL_QUEUE_RUN_HOSTFAL_RUN_HOSTMissingCredentialsErrorfetch_auth_credentials)__version__)Priorityadd_priority_headeradd_timeout_headeradd_hint_headeradd_fal_app_context_headershandle_response_headersREQUEST_TIMEOUT_TYPE_HEADERREQUEST_TIMEOUT_HEADER)WebSocketClientProtocol)
Connection)Image)fal_v3cdnfal)never	immediate1h1d7d30d1y)hideforbidallowzhttps:///zwss://zhttps://rest.fal.aizhttps://v3.fal.mediaFAL_CDN_HOSTzhttps://fal.mediar+   UploadRepositoryIdDEFAULT_UPLOAD_REPOSITORYr,   r-   list[UploadRepositoryId]"DEFAULT_UPLOAD_FALLBACK_REPOSITORYzfal-client/z	 (python)   FAL_CLIENT_EXECUTOR)thread_name_prefix<   i  iQ i:	 i ' i3zdict[str, int | None]#_UPLOAD_LIFECYCLE_EXPIRATION_VALUEST)frozenc                  $    e Zd ZU ded<   ded<   dS )StorageACLRulestruserStorageACLDecisiondecisionN)__name__
__module____qualname____annotations__     X/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/fal_client/client.pyrE   rE   l   s'         III      rO   rE   c                  ,    e Zd ZU dZded<   dZded<   dS )
StorageACLNzStorageACLDecision | Nonedefaultzlist[StorageACLRule] | Nonerules)rJ   rK   rL   rS   rM   rT   rN   rO   rP   rR   rR   r   s4         )-G----)-E------rO   rR   c                  8    e Zd ZU dZdZded<   dZded<   d
d	ZdS )StorageSettingsa  Lifecycle configuration for uploaded files.

    Use `expires_in="never"` (or omit `expires_in`) to keep the backend's
    default retention policy. `expires_in="immediate"` maps to approximately
    60 seconds because the backend treats `0` as no expiration.
    NzObjectExpiration | None
expires_inzStorageACL | Noneinitial_aclreturnNonec                @    | j         t          | j                    d S d S N)rW   '_get_upload_expiration_duration_secondsselfs    rP   __post_init__zStorageSettings.__post_init__   s(    ?&3DODDDDD '&rO   rY   rZ   )rJ   rK   rL   __doc__rW   rM   rX   r`   rN   rO   rP   rV   rV   x   s^           +/J....%)K))))E E E E E ErO   rV   c                  @    e Zd ZU ded<   ded<   ded<   ded<   dd	Zd
S )CDNTokenrF   token
token_typebase_upload_urlr   
expires_atrY   boolc                P    t          j        t          j                  | j        k    S r\   )r   nowr   utcrh   r^   s    rP   
is_expiredzCDNToken.is_expired   s    |HL))T_<<rO   N)rY   ri   )rJ   rK   rL   rM   rm   rN   rO   rP   rd   rd      sT         JJJOOO= = = = = =rO   rd   c                  &    e Zd Zd
dZddZddZd	S )CDNTokenManagerauthr   rY   rZ   c                   || _         t          dddt          j                            t
          j                            | _        t          j	                    | _
        t           d| _        | j         j        ddd| _        d S N )tzinfore   rf   rg   rh   z+/storage/auth/token?storage_type=fal-cdn-v3application/jsonAuthorizationAcceptContent-Type)_authrd   r   minreplacer   rl   _token	threadingLock_lockREST_URL_urlheader_value_headersr_   rp   s     rP   __init__zCDNTokenManager.__init__   s    
 (|++8<+@@	!
 !
 !
 &/^%5%5
LLL	!Z4(.
 
rO   rd   c           	     f   t          j                    5 }|                    | j        | j        i           }|                                 |                                }d d d            n# 1 swxY w Y   t          |d         |d         |d         t          j	        |d                             S Nheadersjsonre   rf   base_urlrh   ru   )
httpxClientpostr   r   raise_for_statusr   rd   r   fromisoformatr_   clientresponsedatas       rP   _refresh_tokenzCDNTokenManager._refresh_token   s    \^^ 	#v{{49dm"{MMH%%'''==??D	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#
 w-L) ,-d<.@AA	
 
 
 	
s   AA++A/2A/c                    | j         5  | j                                        r|                                 | _        | j        cd d d            S # 1 swxY w Y   d S r\   r   r~   rm   r   r^   s    rP   	get_tokenzCDNTokenManager.get_token   s    Z 	 	{%%'' 4"1133;	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   9AAANrp   r   rY   rZ   rY   rd   rJ   rK   rL   r   r   r   rN   rO   rP   ro   ro      P        
 
 
 
 
 
 
 
     rO   ro   c                  &    e Zd Zd
dZddZddZd	S )AsyncCDNTokenManagerrp   r   rY   rZ   c                   || _         t          dddt          j                            t
          j                            | _        t          j	                    | _
        t           d| _        | j         j        ddd| _        d S rr   )r{   rd   r   r|   r}   r   rl   r~   asyncior   r   r   r   r   r   r   s     rP   r   zAsyncCDNTokenManager.__init__   s    
 (|++8<+@@	!
 !
 !
 $+<>>
LLL	!Z4(.
 
rO   rd   c           	       K   t          j                    4 d {V }|                    | j        | j        i            d {V }|                                 |                                }d d d           d {V  n# 1 d {V swxY w Y   t          |d         |d         |d         t          j	        |d                             S r   )
r   AsyncClientr   r   r   r   r   rd   r   r   r   s       rP   r   z#AsyncCDNTokenManager._refresh_token   sQ     $&& 	# 	# 	# 	# 	# 	# 	#&#[[DMPR[SSSSSSSSH%%'''==??D	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#
 w-L) ,-d<.@AA	
 
 
 	
s   AA??
B	B	c                   K   | j         4 d {V  | j                                        r|                                  d {V | _        | j        cd d d           d {V  S # 1 d {V swxY w Y   d S r\   r   r^   s    rP   r   zAsyncCDNTokenManager.get_token   s     : 	 	 	 	 	 	 	 	{%%'' :$($7$7$9$9999999;	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ?A""
A,/A,Nr   r   r   rN   rO   rP   r   r      r   rO   r   i  @i   
   c                      e Zd Zddddd$dZed%d            Zed%d            Zed&d            Z	 d'd(dZd)dZ	d%dZ
edddddd*d            Zeddddd d+d#            ZdS ),MultipartUploadN
chunk_sizecontent_typemax_concurrency	file_namerF   r   httpx.Clienttoken_managerro   r   
int | Noner   
str | Noner   rY   rZ   c                   || _         || _        || _        |pt          | _        |pd| _        |pt          | _        d | _        d | _	        g | _
        d S Napplication/octet-streamr   _client_token_managerMULTIPART_CHUNK_SIZEr   r   MULTIPART_MAX_CONCURRENCYr   _access_url
_upload_id_partsr_   r   r   r   r   r   r   s          rP   r   zMultipartUpload.__init__   ]     #+$<(<(F,F.K2K'+&*"$rO   c                <    | j         st          d          | j         S NzUpload not initiatedr   
ValueErrorr^   s    rP   
access_urlzMultipartUpload.access_url   %     	53444rO   c                <    | j         st          d          | j         S r   r   r   r^   s    rP   	upload_idzMultipartUpload.upload_id   #     	53444rO   dict[str, str]c                f    | j                                         }|j         d|j         t          dS N rx   z
User-Agentr   r   rf   re   
USER_AGENTr_   re   s     rP   auth_headerszMultipartUpload.auth_headers  s>    #--// % 0@@5;@@$
 
 	
rO   object_lifecycle_preference!LifecyclePreferencePayload | Nonec                *   | j                                         }|j         d}i | j        d| j        | j        d}t          ||           t          | j        d||          }|	                                }|d         | _
        |d         | _        d S Nz/files/upload/multipartrv   )ry   rz   X-Fal-File-NamePOSTr   r   uploadId)r   r   rg   r   r   r   _object_lifecycle_headers_maybe_retry_requestr   r   r   r   )r_   r   re   urlr   r   results          rP   createzMultipartUpload.create  s     #--//&???

( -#~	
 
 
 	"'+FGGG'L	
 
 
 !,/ ,rO   part_numberintr   bytesc           	         | j          d| j         d| }t          | j        d|i | j        | j        dd|d           }|j        d         }| j                            ||d           d S 	N/multipart/r8   PUTidentity)rz   zAccept-Encoding)r   contenttimeoutetag)
partNumberr   )	r   r   _requestr   r   r   r   r   append)r_   r   r   r   r   r   s         rP   upload_partzMultipartUpload.upload_part"  s    KKT^KKkKKL# $ 1#-  
 
 
 
 ') 	
 	
 	
 	
 	
rO   c                ~    | j          d| j         d}t          | j        d|| j        d| j        i           | j         S Nr   z	/completer   partsr   )r   r   r   r   r   r   )r_   r   s     rP   completezMultipartUpload.complete:  sX    FFT^FFFL%4;'	
 	
 	
 	
 rO   r   r   r   r   c          	     r   dd l }	 | ||||||          }
|
                    |           t          j        t	          |          |
j        z            }|	j                            |
j                  5 }g }t          d|dz             D ]P}|dz
  |
j        z  }||||
j        z            }|
                    |                    |
j        ||                     Q|	j                            |          D ]}|                                 	 d d d            n# 1 swxY w Y   |
                                S )Nr   r   r   r   r   r   r   r   max_workersr>   )concurrent.futuresr   mathceillenr   futuresThreadPoolExecutorr   ranger   submitr   as_completedr   r   )clsr   r   r   r   r   r   r   r   
concurrent	multipartr   executorr   r   startfutures                    rP   savezMultipartUpload.saveE  s    	"!!!C'!%+
 
 
	 	5PQQQ	#d))i&::;;22!1 3 
 
 	 G$Q	22  $qI,@@EEI,@$@@AOOI$9;MM    %,99'BB     	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  !!###s   5BDD!Dr   r   r   r   	file_path
str | Pathc                  dd l }t          j                                      }	t          j                                      }
 | |	|||||                              |           t          j        |
j        z            }|j	        
                    j                  5 }g }t          d|dz             D ]2}dfd
}|                    |                    ||                     3|j	                            |          D ]}|                                 	 d d d            n# 1 swxY w Y                                   S )Nr   r   r   r   r>   pnr   rY   rZ   c                    t          d          5 }| dz
  j        z  }|                    |           |                    j                  }                    | |           d d d            d S # 1 swxY w Y   d S Nrbr>   openr   seekreadr   )r  fr  r   r	  r  s       rP   _upload_partz/MultipartUpload.save_file.<locals>._upload_part  s    i.. 8!!#a9+? ?u vvi&:;;!--b$777	8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s   AA22A69A6)r  r   rY   rZ   )r   ospathbasenamegetsizer   r   r   r   r   r   r   r   r   r   r   r   r   )r  r   r   r	  r   r   r   r   r  r   sizer   r  r   r   r  r  r  s      `             @rP   	save_filezMultipartUpload.save_filel  s    	"!!!G$$Y//	wy))C'!%+
 
 
	 	5PQQQ	$!556622!1 3 
 
 	 G$Q	22 	K 	K8 8 8 8 8 8 8 x|[IIJJJJ$,99'BB     	  	  	  	  	  	  	  	  	  	  	  	  	  	  	   !!###s   (A:D//D36D3)r   rF   r   r   r   ro   r   r   r   r   r   r   rY   rZ   rY   rF   rY   r   r\   r   r   r   r   r   r   rY   rZ   )r   r   r   ro   r   rF   r   r   r   r   r   r   r   r   r   r   )r   r   r   ro   r	  r
  r   r   r   r   r   r   r   r   rY   rF   rJ   rK   rL   r   propertyr   r   r   r   r   r   classmethodr  r  rN   rO   rP   r   r      sV        "&#'&*% % % % % %(       X 
    X
 
 
 
 X
 PT- - - - -,
 
 
 
0	 	 	 	  $(!%&*IM$$ $$ $$ $$ $$ [$$L  "&#'&*IM)$ )$ )$ )$ )$ [)$ )$ )$rO   r   c                      e Zd Zddddd$dZed%d            Zed%d            Zed&d            Z	 d'd(dZd)dZ	d%dZ
edddddd*d            Zeddddd d+d#            ZdS ),AsyncMultipartUploadNr   r   rF   r   httpx.AsyncClientr   r   r   r   r   r   r   rY   rZ   c                   || _         || _        || _        |pt          | _        |pd| _        |pt          | _        d | _        d | _	        g | _
        d S r   r   r   s          rP   r   zAsyncMultipartUpload.__init__  r   rO   c                <    | j         st          d          | j         S r   r   r^   s    rP   r   zAsyncMultipartUpload.access_url  r   rO   c                <    | j         st          d          | j         S r   r   r^   s    rP   r   zAsyncMultipartUpload.upload_id  r   rO   r   c                v   K   | j                                          d {V }|j         d|j         t          dS r   r   r   s     rP   r   z!AsyncMultipartUpload.auth_headers  sT      )3355555555 % 0@@5;@@$
 
 	
rO   r   r   c                V  K   | j                                          d {V }|j         d}| j         d {V }i |d| j        | j        d}t          ||           t          | j        d||           d {V }|	                                }|d         | _
        |d         | _        d S r   )r   r   rg   r   r   r   r   _async_maybe_retry_requestr   r   r   r   )r_   r   re   r   r   request_headersr   r   s           rP   r   zAsyncMultipartUpload.create  s      )3355555555&???)))))))

( -#~	
 
 
 	"/3NOOO3L#	
 
 
 
 
 
 
 
 
 !,/ ,rO   r   r   r   r   c           	        K   | j          d| j         d| }| j         d {V }t          | j        d|i || j        dd|d            d {V }|j        d         }| j                            ||d           d S r   )	r   r   r   _async_requestr   r   r   r   r   )r_   r   r   r   r   r   r   s          rP   r   z AsyncMultipartUpload.upload_part  s      KKT^KKkKK)))))))'L $ 1#-  
 
 
 
 
 
 
 
 
 
 ') 	
 	
 	
 	
 	
rO   c                   K   | j          d| j         d}| j         d {V }t          | j        d||d| j        i           d {V  | j         S r   )r   r   r   r+  r   r   )r_   r   r   s      rP   r   zAsyncMultipartUpload.complete  s      FFT^FFF)))))))(L4;'
 
 
 	
 	
 	
 	
 	
 	
 	
 rO   r   c                 K    | ||||||                               |           d {V  t          j        t                    j        z            }	dfdfdt          d	|	d	z             D             }
t          j        j                  fd
t          j	        fd|
D               d {V  
                                 d {V S )Nr   r   r   r   rY   rZ   c                   K   | dz
  j         z  }||j         z            }                    | |           d {V  d S )Nr>   )r   r   )r   r  chunkr   r  s      rP   r   z.AsyncMultipartUpload.save.<locals>.upload_part  s]       1_	(<<E)=!==>E''U;;;;;;;;;;;rO   c                J    g | ]}t          j         |                     S rN   r   create_task.0r   r   s     rP   
<listcomp>z-AsyncMultipartUpload.save.<locals>.<listcomp>  >     
 
 
 K 8 899
 
 
rO   r>   c                v   K   4 d {V  |  d {V  d d d           d {V  d S # 1 d {V swxY w Y   d S r\   rN   tasksems    rP   bounded_uploadz1AsyncMultipartUpload.save.<locals>.bounded_upload#                






                                	(
22c                &    g | ]} |          S rN   rN   r7  r<  r>  s     rP   r8  z-AsyncMultipartUpload.save.<locals>.<listcomp>'  #    FFF~~d33FFFrO   r   r   rY   rZ   )r   r   r   r   r   r   r   	Semaphorer   gatherr   )r  r   r   r   r   r   r   r   r   r   tasksr>  r  r=  r   s       `      @@@@rP   r  zAsyncMultipartUpload.save  s_      C'!%+
 
 
	 ;VWWWWWWWWW	#d))i&::;;	< 	< 	< 	< 	< 	< 	<

 
 
 
$Q	22
 
 
 	 9::	 	 	 	 	 nFFFFFFFGGGGGGGG'')))))))))rO   r  r	  r
  c                 K   t           j                                      }t           j                                      }	 | ||||||                              |           d {V  t          j        |	j        z            }
dfdfdt          d	|
d	z             D             }t          j
        j                  fd
t          j        fd|D               d {V                                   d {V S )Nr   r   r   r   rY   rZ   c                  K   t          d          5 }| dz
  j        z  }|                    |           |                    j                  }                    | |           d {V  d d d            d S # 1 swxY w Y   d S r  r  )r   r  r  r   r	  r  s       rP   r   z3AsyncMultipartUpload.save_file.<locals>.upload_partC  s      i&& ?!$qI,@@uvvi233++K>>>>>>>>>	? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?s   AA::A>A>c                J    g | ]}t          j         |                     S rN   r4  r6  s     rP   r8  z2AsyncMultipartUpload.save_file.<locals>.<listcomp>J  r9  rO   r>   c                v   K   4 d {V  |  d {V  d d d           d {V  d S # 1 d {V swxY w Y   d S r\   rN   r;  s    rP   r>  z6AsyncMultipartUpload.save_file.<locals>.bounded_uploadR  r?  r@  c                &    g | ]} |          S rN   rN   rB  s     rP   r8  z2AsyncMultipartUpload.save_file.<locals>.<listcomp>V  rC  rO   rD  )r  r  r  r  r   r   r   r   r   r   rE  r   rF  r   )r  r   r   r	  r   r   r   r   r   r  r   rG  r>  r  r=  r   s      `        @@@@rP   r  zAsyncMultipartUpload.save_file*  s      G$$Y//	wy))C'!%+
 
 
	 ;VWWWWWWWWW	$!5566	? 	? 	? 	? 	? 	? 	?
 
 
 
$Q	22
 
 
 	 9::	 	 	 	 	 nFFFFFFFGGGGGGGG'')))))))))rO   )r   rF   r   r%  r   r   r   r   r   r   r   r   rY   rZ   r  r  r\   r  r  )r   r%  r   r   r   rF   r   r   r   r   r   r   r   r   r   r   rY   rF   )r   r%  r   r   r	  r
  r   r   r   r   r   r   r   r   rY   rF   r   rN   rO   rP   r$  r$    sV        "&#'&*% % % % % %(       X 
    X
 
 
 
 X
 PT- - - - -.
 
 
 
2
 
 
 
  $(!%&*IM)* )* )* )* )* [)*V  "&#'&*IM,* ,* ,* ,* ,* [,* ,* ,*rO   r$  c                      e Zd ZdS )FalClientErrorNrJ   rK   rL   rN   rO   rP   rN  rN  Z  s        DrO   rN  c                  N    e Zd ZU ded<   ded<   ded<   ded<   d	Zd
ed<   ddZd	S )FalClientHTTPErrorrF   messager   status_coder   response_headershttpx.Responser   Nr   
error_typerY   c                    | j          S r\   )rR  r^   s    rP   __str__zFalClientHTTPError.__str__f  s    ,  rO   r  )rJ   rK   rL   rM   rV  rX  rN   rO   rP   rQ  rQ  ^  sh         LLL$$$$!J!!!!! ! ! ! ! !rO   rQ  c                  0    e Zd ZU ded<   dZded<   d	dZdS )
FalClientTimeoutErrorfloatr   NOptional[str]
request_idrY   rF   c                L    | j         d| j         dS d| j          d| j         dS )NzRequest timed out after z secondszRequest z timed out after )r]  r   r^   s    rP   rX  zFalClientTimeoutError.__str__o  s:    ?"DdlDDDDVdoVVVVVVrO   r  )rJ   rK   rL   rM   r]  rX  rN   rO   rP   rZ  rZ  j  sJ         NNN $J$$$$W W W W W WrO   rZ  r   rU  rY   rZ   c                   	 |                                   d S # t          j        $ r}d }| j        }	 |                                 }n# t
          $ r d }Y nw xY wt          |t                    r0|                    d| j                  }|                    d          }|| j	                            d          }t          || j        t          | j	                  | |          |d }~ww xY w)NdetailrV  zx-fal-error-type)r   rV  )r   r   HTTPStatusErrortextr   r   
isinstancedictgetr   rQ  rS  )r   excrV  msgbodys        rP   _raise_for_statusri  v  s   !!#####    
m	==??DD 	 	 	DDD	 dD!! 	0((8X]33C,//J!)--.@AAJ   !""!
 
 
 	!s8    C*	C%AC%AC%ABC%%C*c                      e Zd ZdS )StatusNrO  rN   rO   rP   rk  rk    s        crO   rk  c                      e Zd ZU dZded<   dS )QueuedzIndicates the request is enqueued and waiting to be processed. The position
    field indicates the relative position in the queue (0-indexed).r   positionN)rJ   rK   rL   rb   rM   rN   rO   rP   rm  rm    s'         G G MMMMMrO   rm  c                  2    e Zd ZU dZ e            Zded<   dS )
InProgresszIndicates the request is currently being processed. If the status operation called
    with the `with_logs` parameter set to True, the logs field will be a list of
    log objects.list[dict[str, Any]] | NonelogsN)rJ   rK   rL   rb   r
   rr  rM   rN   rO   rP   rp  rp    s5          
 ).D//////rO   rp  c                      e Zd ZU dZ e            Zded<    e            Zded<    ed          Zded	<    ed          Z	ded
<   dS )	Completeda2  Indicates the request has been completed and the result can be gathered. The logs field will
    contain the logs if the status operation was called with the `with_logs` parameter set to True. Metrics
    might contain the inference time, and other internal metadata (number of tokens
    processed, etc.).rq  rr  dict[str, Any]metricsN)rS   r   errorrV  )
rJ   rK   rL   rb   r
   rr  rM   rv  rw  rV  rN   rO   rP   rt  rt    s          
 ).D////#eggG%%%%d+++E++++"U4000J000000rO   rt  c                      e Zd ZU ded<    ed          Zded<    ed          Zded<    ed          Zded<   ddZdS )_BaseRequestHandlerF   r]  Freprresponse_url
status_url
cancel_urlr   AnyJSONrY   rk  c                |   |d         dk    rt          |d                   S |d         dk    rt          |d                   S |d         dk    rU|                    d	i           }t          |d         ||                    d
          |                    d                    S t	          d|d                    )NstatusIN_QUEUEqueue_position)rn  IN_PROGRESSrr  )rr  	COMPLETEDrv  rw  rV  )rr  rv  rw  rV  zUnknown status: )rm  rp  re  rt  r   )r_   r   rv  s      rP   _parse_statusz _BaseRequestHandle._parse_status  s    >Z''4(8#9::::(^},,4<0000(^{**hhy"--G&\hhw''88L11	    @X@@AAArO   N)r   r  rY   rk  )	rJ   rK   rL   rM   r
   r|  r}  r~  r  rN   rO   rP   ry  ry    s         OOO5)))L))))e'''J''''e'''J''''B B B B B BrO   ry  	workflowscomfyidrF   c                    dd l }|                     d          }t          |          dk    r| S |                    d|           }|r|                                \  }}| d| S t          d|  d          )Nr   r8   r>   z^([0-9]+)-([a-zA-Z0-9-]+)$zInvalid app id: z*. Must be in the format <appOwner>/<appId>)resplitr   matchgroupsr   )r  r  r   r  	app_ownerapp_ids         rP   _ensure_app_id_formatr    s    IIIHHSMME
5zzA~~	HH2B77E '!LLNN	6&&f&&&
VVVV
W
WWrO   c                  P    e Zd ZU ded<   ded<   ded<   ded<   edd	            Zd
S )AppIdrF   owneraliasr\  r  	namespaceendpoint_idrY   c           	     T   t          |          }|                    d          }|d         t          v r> | |d         |d         d                    |dd                    pd |d                   S  | |d         |d         d                    |dd                    pd d           S )Nr8   r   r>         )r  r  r  r  )r  r  APP_NAMESPACESjoin)r  r  normalized_idr   s       rP   from_endpoint_idzAppId.from_endpoint_id  s    -k::##C((8~%%3AhAhXXeABBi((0D(	    s((%)$$,	
 
 
 	
rO   N)r  rF   rY   r  )rJ   rK   rL   rM   r"  r  rN   rO   rP   r  r    s^         JJJJJJ
 
 
 [
 
 
rO   r  x   g     V@)r>   rA   r  c                x    | j         r
| j          dnd}| j        r
d| j         nd}| | j         d| j         | S )Nr8   rs   )r  r  r  r  )r  prefixsuffixs      rP   _format_app_pathr    s^    '-'7? ####RF"(+52F;fl;;V\;6;;;rO   valuer   r   c                    | d S t           \  }}|| cxk    r|k    sn t          d| d| d          t          |           S )Nzmax_buffering must be between z and z (inclusive))REALTIME_MAX_BUFFERINGr   rF   )r  	min_value	max_values      rP   _serialize_max_bufferingr  	  sk    }t1Iy++++)++++TYTTYTTT
 
 	
 u::rO   rs   r  max_bufferingapplicationre   r  r  c                  t                               |           }t          |          }t           | }|r|d|                    d          z   z  }i }|r||d<   t          |          }|||d<   |r| dt          |           S |S )Nr8   fal_jwt_tokenr  ?)r  r  r  REALTIME_URL_FORMATlstripr  r   )	r  re   r  r  r  app_pathr   queryserialized_bufferings	            rP   _build_runner_ws_urlr    s     ##K00F''H 
,(
,
,C &sT[[%%%%E '!&o3MBB'!5o +**	%((***JrO   realtimer  c               (    t          | |||          S )Nr  )r  )r  re   r  r  s       rP   _build_realtime_urlr  ,  s'      #	   rO   r   r   c                8   t          | t                    r| S t          | t                    r`t          |                     d          t                    r| d         S t          |                     d          t                    r| d         S t	          d          )Nre   r`  z)Unexpected realtime token response format)rc  rF   rd  re  RuntimeError)r   s    rP   _parse_token_responser  ;  s    $ $ "dhhw''-- 	!= dhhx((#.. 	">!
B
C
CCrO   c                  *     e Zd ZdZ	 	 d
d fd	Z xZS )RealtimeErrorz9Raised when the realtime endpoint sends an error payload.Nrw  rF   reasonr   payloadOptional[dict[str, Any]]c                    || _         |pd| _        |pi | _        | j        s|n| d| j         }t                                          |           d S )Nrs   z: )rw  r  r  superr   )r_   rw  r  r  rR  	__class__s        rP   r   zRealtimeError.__init__I  s`     
l}"#{I%%50I0IDK0I0I!!!!!rO   )NN)rw  rF   r  r   r  r  )rJ   rK   rL   rb   r   __classcell__)r  s   @rP   r  r  F  sP        CC
 ",0	
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
"rO   r  rR  r   c                8    dd l }|                    | d          S )Nr   F)raw)msgpackunpackbrR  r  s     rP   msgpack_decode_messager  V  s!    NNN??7?...rO   c                8    dd l }|                    | d          S )Nr   T)use_bin_type)r  packbr  s     rP   msgpack_encode_messager  \  s!    NNN==t=444rO   decode_messageCallable[[bytes], Any] | Nonedict[str, Any] | Nonec                    t          | t                    r|                                 } t          | t          t          f          r|pt
          } ||           S t          | t                    r	 t          j        |           }n# t          j	        $ r d| dcY S w xY w|
                    d          }|dk    r8t          |
                    dd          |
                    d          |          |dk    rd S |S d	| iS )
Nrb  )typer  r  zx-fal-errorrw  UNKNOWN_ERRORr  zx-fal-messager  )rc  
memoryviewtobytesr   	bytearrayr  rF   r   loadsJSONDecodeErrorre  r  )rR  r  decoder  msg_types        rP   _decode_realtime_messager  b  s$    ':&& $//##'E9-.. 9#9vg'3 	8j))GG# 	8 	8 	8"w77777	8 ;;v&&}$$G_55H%%  
 &&4ws   0B BBru  encode_messageCallable[[Any], bytes] | Nonec                *    |pt           } ||           S r\   )r  )rR  r  encodes      rP   _encode_realtime_messager    s     55F6'??rO   c                  b    e Zd ZU dZded<   dZded<   dZded<   ddZddZddZ	ddZ
ddZdS )RealtimeConnectionz(Synchronous realtime connection wrapper.z'Connection'_wsNr  _encode_messager  _decode_message	argumentsru  rY   rZ   c                d    t          || j                  }| j                            |           d S r\   r  r  r  sendr_   r  r  s      rP   r  zRealtimeConnection.send  s.    *9d6JKKgrO   r  c                ,   	 	 | j                                         }n_# t          $ rR}ddlm}m} t          ||          rY d }~d S t          ||          rt          dt          |                    | d }~ww xY wt          || j
                  }||S NTr   )ConnectionClosedConnectionClosedOKCONNECTION_CLOSEDr  recv	Exceptionwebsockets.exceptionsr  r  rc  r  rF   r  r  r_   r   rf  r  r  decodeds         rP   r  zRealtimeConnection.recv  s    	8==?? 
 
 
       
 c#566  44444c#344 P'(;SXXFFCO
 /x9MNNGNs    
A9A4/A44A9c                h    t          | j        dd           }t          |          r |             d S d S Nclosegetattrr  callabler_   r  s     rP   r  zRealtimeConnection.close  s<    '400E?? 	EGGGGG	 	rO   c                    | S r\   rN   r^   s    rP   	__enter__zRealtimeConnection.__enter__  s    rO   c                .    |                                   d S r\   r  r_   exc_typerf  tbs       rP   __exit__zRealtimeConnection.__exit__  s    

rO   r  ru  rY   rZ   rY   r  ra   )rY   r  )rJ   rK   rL   rb   rM   r  r  r  r  r  r   r  rN   rO   rP   r  r    s         2259O999959O9999      (   
        rO   r  c                  b    e Zd ZU dZded<   dZded<   dZded<   ddZddZddZ	ddZ
ddZdS )AsyncRealtimeConnectionz)Asynchronous realtime connection wrapper.z'WebSocketClientProtocol'r  Nr  r  r  r  r  ru  rY   rZ   c                t   K   t          || j                  }| j                            |           d {V  d S r\   r  r  s      rP   r  zAsyncRealtimeConnection.send  sD      *9d6JKKhmmG$$$$$$$$$$$rO   r  c                <  K   	 	 | j                                          d {V }n_# t          $ rR}ddlm}m} t          ||          rY d }~d S t          ||          rt          dt          |                    | d }~ww xY wt          || j
                  }||S r  r  r  s         rP   r  zAsyncRealtimeConnection.recv  s      	!%000000 
 
 
       
 c#566  44444c#344 P'(;SXXFFCO
 /x9MNNGNs   % 
BA</A<<Bc                x   K   t          | j        dd           }t          |          r |             d {V  d S d S r  r  r  s     rP   r  zAsyncRealtimeConnection.close  sL      '400E?? 	%''MMMMMMMMM	 	rO   c                
   K   | S r\   rN   r^   s    rP   
__aenter__z"AsyncRealtimeConnection.__aenter__  s      rO   c                >   K   |                                   d {V  d S r\   r  r  s       rP   	__aexit__z!AsyncRealtimeConnection.__aexit__  s,      jjllrO   r  r  ra   )rY   r
  )rJ   rK   rL   rb   rM   r  r  r  r  r  r  r  rN   rO   rP   r
  r
    s         33""""59O999959O9999% % % %   (   
        rO   r
  r   r   dict[str, str] | NoneIterator['Connection']c              #     K   ddl m} |                    | |t          d           5 }|V  d d d            d S # 1 swxY w Y   d S )Nr   )r   additional_headersopen_timeoutmax_size)websockets.syncr   connectREALTIME_OPEN_TIMEOUT)r   r   r   wss       rP   _connect_sync_wsr    s       '&&&&&	"*	 
 
 
 
 
                 s   9= =(AsyncIterator['WebSocketClientProtocol']c                  K   dd l }|                    | |t          d           4 d {V 	 }|W V  d d d           d {V  d S # 1 d {V swxY w Y   d S )Nr   r  )
websocketsr  r  )r   r   r   r  s       rP   _connect_async_wsr!    s	      !!"*	 "          
 
                             s   A
AAr   r   methodkwargsc                B     | j         ||fi |}t          |           |S r\   requestri  r   r"  r   r#  r   s        rP   r   r     s2     v~fc44V44HhOrO   r%  c                R   K    | j         ||fi | d {V }t          |           |S r\   r%  r'  s        rP   r.  r.    sH       $V^FC::6::::::::HhOrO   皙?   )i  i  i  )i  i          ^@g      >@)r  ri   c                R    | j         t          vrdS d| j        v rdS d| j        v rdS dS )z-Tell apart ingress errors from client errors.Fzx-fal-request-idnginxT)rS  INGRESS_ERROR_CODESr   rb  )r   s    rP   _is_ingress_errorr0    sD     #666uX---u (-t5rO   extra_retry_codes	list[int]c                    | j         dk    r!| j                            t                    rdS t	          |           rdS | j         t
          v s	| j         |v rdS dS )Nr+  FT)rS  r   re  r&   r0  RETRY_CODES)r   r1  s     rP   _should_retry_responser5  -  so    
 s""x'7';';#( (" u"" t{**h.BFW.W.Wt5rO   rf  r  c                h   t          | t          j                  rE	 | j        }t          |t          j                  rt
          |j        v rdS n# t          $ r Y nw xY wdS t          | t          j                  rdS t          | t          j	        t          f          rt          | j        |          S dS )NFT)rc  r   TimeoutExceptionr&  Requestr'   r   r  TransportErrorra  rQ  r5  r   )rf  r1  r&  s      rP   _should_retryr:  @  s    #u-.. 		kG'5=11 !)W_<< 5 	 	 	D	t#u+,, t#-/ABCC G%cl4EFFF5s   /A 
AAexponentialF	num_retryr   
base_delayr[  	max_delaybackoff_typeLiteral['exponential', 'fixed']jitterc                    |dk    rt          |d| dz
  z  z  |          }nt          ||          }|r|t          j        dd          z  }t          ||          S )Nr;  r  r>   g      ?g      ?)r|   randomuniform)r<  r=  r>  r?  rA  delays         rP   _get_retry_delayrF  V  sk     }$$J!	A"67CCJ	** *S)))ui   rO   )r1  c                  t          dt          dz             D ]}	 t          | ||fi |c S # t          j        t
          f$ r}t          ||          rm|t          k     rbt          |t          t          dd          }t                              d| d| dt          |z
   d           t          j        |           Y d }~ d }~ww xY wt          d          )	Nr>   r;  TRetrying request to  due to  ( attempts left)Failed to perform request)r   MAX_ATTEMPTSr   r   	HTTPErrorrQ  r:  rF  
BASE_DELAY	MAX_DELAYloggerdebugtimesleepr  r   r"  r   r1  r#  attemptrf  rE  s           rP   r   r   h  s    L1,--  	FFC::6:::::!34 
	 
	 
	S"344 <9O9O(ZM4  f3ffff|g?Ufff   
5!!!
	 2
3
33s   -CA8C CCc                 K   t          dt          dz             D ]}	 t          | ||fi | d {V c S # t          j        t
          f$ r}t          ||          ri|t          k     r^t          |dddd          }t          	                    d| d| dt          |z
   d	           t          j        |           d {V  Y d }~ d }~ww xY wt          d
          )Nr>   r)  r   r;  TrH  rI  rJ  rK  rL  )r   rM  r.  r   rN  rQ  r:  rF  rQ  rR  r   rT  r  rU  s           rP   r+  r+    s2      L1,--  
	'FFvFFFFFFFFFFF!34 	 	 	S"344 <9O9O(#r=$OOf3ffff|g?Ufff   mE*********	 2
3
33s   5C
A4CCC
rp   r   c                ^    | j                                         dk    r
d| j         S | j        S )NkeyzBearer )schemelowerre   r   )rp   s    rP   _cdn_auth_headerr\    s4    {e##%%%%rO   r   r   r   c                J    | t          j        |          }|| d<   || d<   d S d S )NzX-Fal-Object-Lifecyclez!X-Fal-Object-Lifecycle-Preference)r   dumps)r   r   lifecycle_jsons      rP   r   r     s@     #.$?@@ -;()7E3444 /.rO   rW   ObjectExpirationc                    t          | t                    rt          d          t          | t                    r| dk    rt          d          | S | t          v rt          |          S t          d          )Nz9Boolean values are not valid lifecycle expires_in values.r   zBInteger lifecycle expires_in value must be greater than 0 seconds.zwUnsupported lifecycle expires_in value. Use one of: never, immediate, 1h, 1d, 7d, 30d, 1y, or an integer seconds value.)rc  ri   r   r   rB   )rW   s    rP   r]   r]     s    *d## VTUUU*c"" ??T   8882:>>
	Z  rO   	lifecycleStorageSettings | Nonec                    | d S i }| j         t          | j                   }|||d<   | j        Ii }| j        j        | j        j        |d<   | j        j        rd | j        j        D             |d<   |r||d<   |pd S )Nexpiration_duration_secondsrS   c                ,    g | ]}|j         |j        d S )rG   rI   rg  )r7  rules     rP   r8  z/_normalize_upload_lifecycle.<locals>.<listcomp>  s4     $ $ $ >>$ $ $rO   rT   rX   )rW   r]   rX   rS   rT   )rb  
normalizedre  acl_payloads       rP   _normalize_upload_lifecyclerk    s     t-/J'&M '
 '
# '28SJ45(24 (4%.%:%BK	" & 	$ $%17$ $ $K   	4(3J}%rO   r   r   c                X    |t          |           d}|||d<   t          ||           |S )N)rz   rx   r   )r\  r   )rp   r   r   r   r   s        rP   _cdn_upload_headersrm    sA      ,>Nt>T>TUUG%.!"g'BCCCNrO   c                <    | j         ddd}t          ||           |S )Nrv   rw   )r   r   )rp   r   r   s      rP   _storage_upload_headersro    s4    
 *$* G
 g'BCCCNrO   
repositoryUploadRepositoryId | Nonefallback_repository4UploadRepositoryId | list[UploadRepositoryId] | Nonec                    h d}| t           } |t          }nt          |t                    s|g}| g|}g }|D ]2}||vrt	          d| d          ||vr|                    |           3|S )N>   r,   r-   r+   zUnsupported upload repository '')r;   r=   rc  listr   r   )rp  rr  allowedordereddedupedentrys         rP   _normalize_upload_repositoriesr{    s     '&&G.
"@+T22 4230/0G(*G " "GuGGGHHHNN5!!!NrO   c                    | pd|dS )N
upload.binr   r   rN   r~  s     rP   _storage_upload_payloadr    s    .,$  rO   r   c          
         t          | dt           dt          ||          t          ||                    }|                                }|d         }|d         }	t          | d||d|id            |	S 	Nr   z)/storage/upload/initiate?storage_type=gcs)r   r   
upload_urlfile_urlr   rz   )r   r   r   )r   r   r  ro  r   
r   rp   r   r   r   r   init_responseinit_resultr  r  s
             rP   _upload_via_storager    s     )>>>$Y=='.IJJ  M  $$&&K\*J:&H.    OrO   c          
       K   t          | dt           dt          ||          t          ||                     d {V }|                                }|d         }|d         }	t          | d||d|id            d {V  |	S r  )r+  r   r  ro  r   r  s
             rP   _async_upload_via_storager  0  s       5>>>$Y=='.IJJ        M  $$&&K\*J:&H
$.          OrO   c               n    t          | dt          dz   ||          }|                                d         S Nr   z/files/upload)r   r   r   )r   CDN_URLr   r   r   r   r   s       rP   
_upload_v3r  N  sA     $/!  H ==??<((rO   c                   t          | dt          dz   |t          ||||                    }|                                d         S r  )r   FAL_CDN_FALLBACK_URLrm  r   r   rp   r   r   r   r   r   s          rP   _upload_cdnr  ^  sU     $.#,	+F
 
  H ==??<((rO   c               ~   K   t          | dt          dz   ||           d {V }|                                d         S r  )r+  r  r   r  s       rP   _async_upload_v3r  s  sc       0/!        H ==??<((rO   c                  K   t          | dt          dz   |t          ||||                     d {V }|                                d         S r  )r+  r  rm  r   r  s          rP   _async_upload_cdnr    sw       0.#,	+F
 
        H ==??<((rO   attempts#list[tuple[str, Callable[[], str]]]c           	        t          |           D ]n\  }\  }}	  |            c S # t          $ rM}|t          |           dz
  k    r t                              d|| |dz            d         |           Y d }~gd }~ww xY wt          d          Nr>   z+Upload failed to %s, falling back to %s: %sr   zUpload attempts were exhausted)	enumerater  r   rQ  warningr  )r  idxlabelrV  rf  s        rP   _try_upload_with_fallbackr    s     "+8!4!4  eW
	799 	 	 	c(mma'''NN=q!!$	       	 7
8
88s   	%
A<AA77A<#list[tuple[str, Callable[[], Any]]]c           	     `  K   t          |           D ]\  }\  }}	  |            }t          j        |          r
| d {V c S |c S # t          $ rM}|t	          |           dz
  k    r t
                              d|| |dz            d         |           Y d }~d }~ww xY wt          d          r  )r  r   iscoroutiner  r   rQ  r  r  )r  r  r  rV  r   rf  s         rP   _async_try_upload_with_fallbackr    s       "+8!4!4  eW	WYYF"6** $#||||||###MMM 	 	 	c(mma'''NN=q!!$	       	 7
8
88s   %AA
BABBhandleSyncRequestHandlec                R    	 |                                   d S # t          $ r Y d S w xY wr\   cancelr  r  s    rP   _maybe_cancel_requestr    s:       s    
&&AsyncRequestHandlec                b   K   	 |                                   d {V  d S # t          $ r Y d S w xY wr\   r  r  s    rP   _async_maybe_cancel_requestr    sS      mmoo   s     
..c                  x    e Zd ZU  ed          Zded<   edd	            Zdd
ddZdddddZ	ddZ
ddZdS )r  Frz  r   r   r  rF   r]  rY   c                    t                               |          }|j        r
|j         dnd}t           | |j         d|j         d| } | |||dz   |dz   |          S Nr8   rs   z
/requests/z/statusz/cancelr]  r|  r}  r~  r   r  r  r  QUEUE_URL_FORMATr  r  r  r   r  r]  r  r  r   s          rP   from_request_idz!SyncRequestHandle.from_request_id       ''44+1+;CF$''''&ccccv|ccWaccs!!)+)+
 
 
 	
rO   	with_logsr  ri   rk  c                   t          | j        d| j        d|it                    }t	          |           |                     |                                          S )Returns the status of the request (which can be one of the following:
        Queued, InProgress, Completed). If `with_logs` is True, logs will be included
        for InProgress and Completed statuses.GETrr  paramsr   )r   r   r}  QUEUE_POLL_TIMEOUTri  r  r   r_   r  r   s      rP   r  zSyncRequestHandle.status  s`    
 (KO	 '
 
 
 	(###!!(--//222rO   r)  r  intervalr  r[  Iterator[Status]c             #     K   	 |                      |          }|V  t          |t                    rdS t          j        |           FzContinuously poll for the status of the request and yield it at each interval till
        the request is completed. If `with_logs` is True, logs will be included in the response.
        Tr  N)r  rc  rt  rS  rT  r_   r  r  r  s       rP   iter_eventszSyncRequestHandle.iter_events  sS      	![[9[55FLLL&),, Jx   	!rO   r  c                    |                      d          D ]}t          | j        d| j        t                    }t          |           |                                S )zOWait till the request is completed and return the result of the inference call.Fr  r  r   )r  r   r   r|  r  ri  r   r_   _r   s      rP   re  zSyncRequestHandle.get  sf    !!E!22 	 	A'K 1;M
 
 
 	(###}}rO   rZ   c                h    t          | j        d| j        t                    }t	          |           dS zCancel the request.r   r  N)r   r   r~  r  ri  r_   r   s     rP   r  zSyncRequestHandle.cancel  s<    'KO&	
 
 
 	(#####rO   N)r   r   r  rF   r]  rF   rY   r  r  ri   rY   rk  )r  ri   r  r[  rY   r  rY   r  ra   rJ   rK   rL   r
   r   rM   r"  r  r  r  re  r  rN   rO   rP   r  r    s          5e,,,F,,,,
 
 
 [
" +0 3 3 3 3 3 3& $)C! ! ! ! ! !	 	 	 	$ $ $ $ $ $rO   c                  x    e Zd ZU  ed          Zded<   edd	            Zdd
ddZdddddZ	ddZ
ddZdS )r  Frz  r%  r   r  rF   r]  rY   c                    t                               |          }|j        r
|j         dnd}t           | |j         d|j         d| } | |||dz   |dz   |          S r  r  r  s          rP   r  z"AsyncRequestHandle.from_request_id  r  rO   r  r  ri   rk  c                  K   t          | j        d| j        d|it                     d{V }t	          |           |                     |                                          S )r  r  rr  r  N)r+  r   r}  r  ri  r  r   r  s      rP   r  zAsyncRequestHandle.status0  s      
 4KO	 '
 
 
 
 
 
 
 
 
 	(###!!(--//222rO   r)  r  r  r[  AsyncIterator[Status]c                 K   	 |                      |           d{V }|W V  t          |t                    rdS t          j        |           d{V  Sr  )r  rc  rt  r   rT  r  s       rP   r  zAsyncRequestHandle.iter_eventsB  sy      	*;;;;;;;;;;;FLLLL&),, -)))))))))	*rO   r  c                   K   |                      d          2 3 d{V }6 t          | j        d| j        t                     d{V }t          |           |                                S )z9Wait till the request is completed and return the result.Fr  Nr  r  )r  r+  r   r|  r  ri  r   r  s      rP   re  zAsyncRequestHandle.getQ  s      ''%'88 	 	 	 	 	 	 	! 9 4K&	
 
 
 
 
 
 
 
 
 	(###}}s   !rZ   c                x   K   t          | j        d| j        t                     d{V }t	          |           dS r  )r+  r   r~  r  ri  r  s     rP   r  zAsyncRequestHandle.cancel_  s^      3KO&	
 
 
 
 
 
 
 
 
 	(#####rO   N)r   r%  r  rF   r]  rF   rY   r  r  )r  ri   r  r[  rY   r  r  ra   r  rN   rO   rP   r  r    s          %5 1 1 1F1111
 
 
 [
" 16 3 3 3 3 3 3& $)C* * * * * *   $ $ $ $ $ $rO   c            
         e Zd ZU  edd          Zded<   dZded<   edcd            ZdcdZ	dddZ
eded            Zedfd            ZdfdZeddgdZddddi ddhd$Zddddi dd%did*Zddddddi ddd+	djd3Zdkd5Zdd6dld8Zdmd9Zdnd;Zd<dd=dod@Z	 dpddddAdqdLZddddAdrdNZ	 dsddddAdtdSZedTdUdedddVdud_            ZedTdded`dvdb            ZdS )wr   NFrS   r{  r   rY  r,  r[  default_timeoutrY   r   c                V    | j         t                      S t          d| j                   S NKeyrY  r   r   r^   s    rP   r{   zAsyncClient._autho  (    8)+++udh///rO   c                    | j         S r\   r{   r^   s    rP   	_get_authzAsyncClient._get_authu  
    zrO   rF   c                    |                                  }|j                                        dk    rt          d          |j        S NrY  zZKey credentials are required for this operation. Set FAL_KEY or FAL_KEY_ID/FAL_KEY_SECRET.r  rZ  r[  r   re   r   s     rP   _get_keyzAsyncClient._get_keyx  I    ~~;%'')l   zrO   r   c                D    t          |                                           S r\   )r   r  r^   s    rP   r   zAsyncClient._token_manager  s    #DNN$4$4555rO   r%  c                z    |                                  }t          j        |j        t          d| j                  S )Nr   r   r   )r  r   r   r   r   r  r   s     rP   r   zAsyncClient._client  sF    ~~ !%!2(  (
 
 
 	
rO   c                   K   | j                                          d {V }t          j        |j         d|j         t          d| j                  S Nr   r   r  )r   r   r   r   rf   re   r   r  r   s     rP   _get_cdn_clientzAsyncClient._get_cdn_client  sq      )3355555555 $)$4!D!Du{!D!D(  (
 
 
 	
rO   token_expirationr  r  r   c                  K   t                               |          j        g|d}t          | j        dt
           d|           d {V }t          |                                          S N)allowed_appsr  r   z/tokens/)r   )r  r  r  r+  r   r   r  r   r_   r  r  r  r   s        rP   _get_realtime_tokenzAsyncClient._get_realtime_token  s       #33K@@FG 0
 
 4L!!!	
 
 
 
 
 
 
 
 
 %X]]__555rO   rs   r  r   start_timeouthintr   r  r  r  r   Optional[Union[int, float]]r  r  r   r   c               p  K   t           |z   }|r|d|                    d          z   z  }i |}	|t          ||	           |t          ||	           t	          |	           t          | j        d||||	           d{V }
t          |
           t          |
j	                   |

                                S )ad  Run an application with the given arguments (which will be JSON serialized). The path parameter can be used to
        specify a subpath when applicable. This method will return the result of the inference call directly.

        Args:
            timeout: Client-side HTTP timeout in seconds. Controls how long the HTTP
                client waits for a response. Defaults to the client's default_timeout.
            start_timeout: Server-side request timeout in seconds. Limits total time spent
                waiting before processing starts. Does not apply once the application begins processing.
        r8   Nr   r   r   r   )RUN_URL_FORMATr  r#   r"   r$   r+  r   ri  r%   r   r   r_   r  r  r  r   r  r  r   r   r   r   s              rP   runzAsyncClient.run  s      * {* 	*3S))))C#.g; D(+++$}h777#H---3L
 
 
 
 
 
 
 
 
 	(### 0111}}rO   r  r  webhook_urlpriorityr   r  r   r  Optional[Priority]r  c               2  K   t           |z   }	|r|	d|                    d          z   z  }	||	dt          d|i          z   z  }	i |}
|t          ||
           |t	          ||
           |t          ||
           t          |
           t          | j        d|	|| j	        |
           d{V }t          |           t          |j                   |                                }t          |d         |d         |d	         |d
         | j                  S )aP  Submit an application with the given arguments (which will be JSON serialized). The path parameter can be used to
        specify a subpath when applicable. This method will return a handle to the request that can be used to check the status
        and retrieve the result of the inference call when it is done.

        Args:
            start_timeout: Server-side request timeout in seconds. Limits total time spent
                waiting before processing starts (includes queue wait, retries, and
                routing). Does not apply once the application begins processing.
        r8   Nr  fal_webhookr   r  r]  r|  r}  r~  r  )r  r  r   r#   r!   r"   r$   r+  r   r  ri  r%   r   r   r  r_   r  r  r  r  r   r  r   r  r   r   r   r   s                rP   r   zAsyncClient.submit  se     , , 	*3S))))C"3M;#?@@@@C#.g; D(+++(333$}h777#H---3L(
 
 
 
 
 
 
 
 
 	(### 0111}}!L)n-L)L)<
 
 
 	
rO   	r  r  r  
on_enqueueon_queue_updater  r   r  client_timeoutr  ri   r  1Optional[Callable[[str], None | Awaitable[None]]]r  4Optional[Callable[[Status], None | Awaitable[None]]]r	  c       	           	
K   |.
|
n)
|k    r#t          j        d
 d| dt          d           dgd	 
fd	}| |             d{V S 	 t          j         |            |
           d{V S # t          j        $ r?}d}d         }||j        }t          |           d{V  t          ||          |d}~ww xY w)<  Subscribe to an application and wait for the result.

        Args:
            start_timeout: Server-side request timeout in seconds. Limits total time spent
                waiting before processing starts (includes queue wait, retries, and
                routing). Does not apply once the application begins processing.
            client_timeout: Client-side total timeout in seconds. Limits the total time
                spent waiting for the entire request to complete (including queue wait
                and processing). If not set, waits indefinitely.
        Nstart_timeout ("s) is larger than client_timeout (Ts). The request may timeout on the client before the server-side timeout is reached.r  
stacklevelrY   r  c            	     v  K                        
           d {V } | d<   , | j                  }t          j        |          r| d {V  	F|                               2 3 d {V } 	|          }t          j        |          r| d {V  /6 |                                  d {V S N)r  r  r  r   r  r   r  )r   r]  inspectisawaitabler  re  )r  r   eventr  r  
handle_refr   r  r  r  r  r  r_   r  r  s      rP   _do_subscribez,AsyncClient.subscribe.<locals>._do_subscribeA  s4     ;;!+ '        F #JqM%#F$566&v.. ! LLLLLLL*#)#5#5	#5#J#J % % % % % % %%,_U33F*622 %$ $K
  %%%%%%%s   1B r  r   r   r]  r  )	warningswarnUserWarningr   wait_forTimeoutErrorr]  r  rZ  )r_   r  r  r  r  r  r  r  r  r   r  r	  r  er]  r  r  s   ```````````     @rP   	subscribezAsyncClient.subscribe  s     2 %$ .//gm g gWe g g g 	    ;?
	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&2 !&(((((((	 )--//>RRRRRRRRRR# 
	 
	 
	J]F!#.
1&999999999'&%   
	s   &#B
 
C:CCr]  c                D    t                               | j        ||          S r\   )r  r  r   r_   r  r]  s      rP   
get_handlezAsyncClient.get_handlek  s    !11$,ZXXXrO   r  rk  c               j   K   |                      ||          }|                    |           d {V S Nr  r$  r  r_   r  r]  r  r  s        rP   r  zAsyncClient.statusn  sA       j99]]Y]777777777rO   c                f   K   |                      ||          }|                                 d {V S r\   r$  re  r_   r  r]  r  s       rP   r   zAsyncClient.resultt  s9      j99ZZ\\!!!!!!!rO   rZ   c                j   K   |                      ||          }|                                 d {V  d S r\   r$  r  r+  s       rP   r  zAsyncClient.cancelx  s?      j99mmoorO   /streamr  r   float | NoneAsyncIterator[dict[str, Any]]c              L  K   t           |z   }|r|d|                    d          z   z  }t          | j        d|||          4 d{V 	 }|                                2 3 d{V }|                                W V  6 	 ddd          d{V  dS # 1 d{V swxY w Y   dS ay  Stream the output of an application with the given arguments (which will be JSON serialized). This is only supported
        at a few select applications at the moment, so be sure to first consult with the documentation of individual applications
        to see if this is supported.

        The function will iterate over each event that is streamed from the server.
        r8   r   )r   r   N)r  r  r   r   	aiter_sser   r_   r  r  r  r   r   eventsr  s           rP   streamzAsyncClient.stream|  st      {* 	*3S))))CL
 
 
 	# 	# 	# 	# 	# 	# 	# 	# %//11 # # # # # # #ejjll"""""  21	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   
BA>%B
B Brp  rr  rb  r   str | bytesr   r   rp  rq  rr  rs  rb  rc  c                 K   |                                  }t          |t                    r|                    d          }t	          |          }t          ||          }	t          |          t          k    rU|	d         dk    rI|d}|                                  d{V }
t          
                    |
| j        ||||           d{V S d|i}|||d<   t          ||           g }|	D ]}|dk    rH|                                  d{V }
|                    dt          t          |
||	          f           P|d
k    r6|                    d
t          t           | j        |||||          f           |dk    r5|                    dt          t$          | j        |||||          f           t'          |           d{V S a  Upload the given data blob and return the access URL.

        The content type should be specified as the second argument. Use
        `upload_file()` or `upload_image()` for convenience. Pass `lifecycle` to
        control uploaded object expiration and initial ACL settings.
        utf-8r   r+   Nr}  )r   r   r   r   r   r   rz   r   )r   r   r,   )r   r   r   r   r-   )r  rc  rF   r  rk  r{  r   MULTIPART_THRESHOLDr  r$  r  r   r   r   r   r  r  r   r  r  r_   r   r   r   rp  rr  rb  rp   resolved_lifecyclerepository_chainr   r   r  repos                 rP   uploadzAsyncClient.upload  st     & ~~dC   	(;;w''D8CC9+
 
 t99***/?/Bh/N/N (	//11111111F-22"1#),> 3          "<0 )2G%&!'+=>>>8:$ &	 &	Dx#3355555555  0&tWUUU    - L !%)5&/8J      5 L !%)5&/8J     5X>>>>>>>>>rO   os.PathLikec          	     x  K   t          j        |          \  }}|d}t          ||          }t          j                            |          t          k    rl|d         dk    r`t          |          }|                                  d{V }	t          
                    t          |          |	| j        ||           d{V S t          |d          5 }
|                     |
                                |t          j                            |          |||           d{V cddd           S # 1 swxY w Y   dS zUpload a local file and return the access URL.

        Pass `lifecycle` to control uploaded object expiration and initial ACL
        settings.
        Nr   r   r+   )r	  r   r   r   r   r  )r   rp  rr  rb  )	mimetypes
guess_typer{  r  r  r  r=  rk  r  r$  r  rF   r   r  rB  r  r  r_   r  rp  rr  rb  	mime_typer  r@  r?  r   files              rP   upload_filezAsyncClient.upload_file  s       !+D11	12I9+
 
 GOOD!!$777 #x//!<Y!G!G//11111111F-77d))"1&,> 8          $ 			'**400%$7# %        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AD//D36D3jpegimageImage.Imageformatc                  K   t          j                    5 }|                    ||           |                     |                                d| |||           d{V cddd           S # 1 swxY w Y   dS zUpload a Pillow image object and return the access URL.

        Pass `lifecycle` to control uploaded object expiration and initial ACL
        settings.
        rO  image/r8  NioBytesIOr  rB  getvaluer_   rM  rO  rp  rr  rb  buffers          rP   upload_imagezAsyncClient.upload_image"  s      " Z\\ 	VJJvfJ---!!!!!%$7# %        	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA//A36A3T	/realtimeuse_jwtr  r  r  r  r  r]  r  r   r  r  r  r  &AsyncIterator[AsyncRealtimeConnection]c              `  K   d }d }	|r|                      ||           d {V }	n#|                                 }
|
j        t          d}t	          ||	||          }t          ||          4 d {V }t          |||          W V  d d d           d {V  d S # 1 d {V swxY w Y   d S Nr  r   r  r   )r  r  )r  r  r   r   r  r!  r
  r_   r  r]  r  r  r  r  r  r   re   rp   r   r  s                rP   r  zAsyncClient.realtime=  s      *.  	U22.> 3        EE >>##D(,(9TTG!+um$OOO$S'::: 	 	 	 	 	 	 	b)NN      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   4B
B'*B'r]  r  r  r  r  c              @  K   d }d }|r|                      ||           d {V }n#|                                 }|j        t          d}t	          ||||          }	t          |	|          4 d {V }
|
W V  d d d           d {V  d S # 1 d {V swxY w Y   d S Nr  r   r  r   )r  r  r   r   r  r!  r_   r  r]  r  r  r  r   re   rp   r   r  s              rP   
ws_connectzAsyncClient.ws_connectY  sw      *.  	U22.> 3        EE >>##D(,(9TTG"'	
 
 
 %S'::: 	 	 	 	 	 	 	bHHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   4B
BBrY   r   r  )rY   r   )rY   r%  r  rF   r  r   rY   rF   r  rF   r  r  r  rF   r   r  r  r  r  r   r   r   rY   r  )r  rF   r  r  r  rF   r  r   r   r   r  r  r   r   r  r  rY   r  )r  rF   r  r  r  rF   r  r   r  ri   r  r
  r  r  r  r  r   r   r  r  r	  r  rY   r  )r  rF   r]  rF   rY   r  r  rF   r]  rF   r  ri   rY   rk  r  rF   r]  rF   rY   r  r  rF   r]  rF   rY   rZ   )
r  rF   r  r  r  rF   r   r0  rY   r1  r\   r   r9  r   rF   r   r   rp  rq  rr  rs  rb  rc  rY   rF   
r  rC  rp  rq  rr  rs  rb  rc  rY   rF   rL  rM  rN  rO  rF   rp  rq  rr  rs  rb  rc  rY   rF   )r  rF   r]  ri   r  rF   r  r   r  r   r  r  r  r  rY   r^  )r  rF   r]  ri   r  rF   r  r   r  r   rY   r  )rJ   rK   rL   r
   rY  rM   r  r   r{   r  r  r   r   r  !REALTIME_TOKEN_EXPIRATION_SECONDSr  r  r   r!  r$  r  r   r  r7  rB  rK  rZ  r   r  rf  rN   rO   rP   r   r   j  sL        eDu555C5555"O""""0 0 0 _0
       6 6 6 _6 
 
 
 _

 
 
 
 !B	6 6 6 6 6 6. /359"$/ / / / / /l "&'+"$59=
 =
 =
 =
 =
 =
H HLPT'+"$596:N N N N N N`Y Y Y Y GL8 8 8 8 8 8" " " "     $# # # # # #B !%	X? 15 ,0X? X? X? X? X? X?| 15 ,0- - - - - -d 
 15 ,0     6 
 $( A8<8<     6 
 $( A       rO   r   c            
         e Zd ZU  edd          Zded<   dZded<   eded            ZdedZ	dfdZ
edgd            Zedhd            Zedid            ZdgdZeddjdZddddi ddkd&Zddddi dd'dld,Zddddddi ddd-	dmd5Zdnd7Zdd8dod:Zdpd;Zdqd=Zd>dd?drdBZ	 dsddddCdtdNZddddCdudPZ	 dvddddCdwdUZedVdWdedddXdxda            ZedVddedbdydd            ZdS )z
SyncClientNFr  r   rY  r,  r[  r  rY   r   c                V    | j         t                      S t          d| j                   S r  r  r^   s    rP   r{   zSyncClient._auth|  r  rO   c                    | j         S r\   r  r^   s    rP   r  zSyncClient._get_auth  r  rO   rF   c                    |                                  }|j                                        dk    rt          d          |j        S r  r  r   s     rP   r  zSyncClient._get_key  r  rO   r   c                |    |                                  }t          j        |j        t          d| j        d          S )Nr   T)r   r   follow_redirects)r  r   r   r   r   r  r   s     rP   r   zSyncClient._client  sH    ~~|!%!2(  (!
 
 
 	
rO   ro   c                D    t          |                                           S r\   )ro   r  r^   s    rP   r   zSyncClient._token_manager  s    t~~//000rO   %concurrent.futures.ThreadPoolExecutorc                    t           S r\   )EXECUTORr^   s    rP   	_executorzSyncClient._executor  s    rO   c                    | j                                         }t          j        |j         d|j         t          d| j                  S r  )r   r   r   r   rf   re   r   r  r   s     rP   r  zSyncClient._get_cdn_client  sZ    #--//|$)$4!D!Du{!D!D(  (
 
 
 	
rO   r  r  r  r   c                   t                               |          j        g|d}t          | j        dt
           d|          }t          |                                          S r  )r  r  r  r   r   r   r  r   r  s        rP   r  zSyncClient._get_realtime_token  so     #33K@@FG 0
 
 (L!!!	
 
 
 %X]]__555rO   rs   r  r  r  r  r   r  r  r  r   r   c               `   t           |z   }|r|d|                    d          z   z  }i |}	|t          ||	           |t          ||	           t	          |	           t          | j        d||||	          }
t          |
           t          |
j	                   |

                                S )a  Run an application with the given arguments (which will be JSON serialized).

        Args:
            timeout: Client-side HTTP timeout in seconds. Controls how long the HTTP
                client waits for a response. Defaults to the client's default_timeout.
            start_timeout: Server-side request timeout in seconds. Limits total time spent
                waiting before processing starts. Does not apply once the application begins processing.
        r8   Nr   r  )r  r  r#   r"   r$   r   r   ri  r%   r   r   r  s              rP   r  zSyncClient.run  s    ( {* 	*3S))))C#.g;D(+++$}h777#H---'L
 
 
 	(### 0111}}rO   r  r   r  r  r  c               "   t           |z   }	|r|	d|                    d          z   z  }	||	dt          d|i          z   z  }	i |}
|t          ||
           |t	          ||
           |t          ||
           t          |
           t          | j        d|	|| j	        |
          }t          |           t          |j                   |                                }t          |d         |d         |d	         |d
         | j                  S )ag  Submit an application with the given arguments (which will be JSON serialized).

        Args:
            start_timeout: Server-side request timeout in seconds. Limits total time spent
                waiting before processing starts (includes queue wait, retries, and
                routing). Does not apply once the application begins processing.
        r8   Nr  r  r   r  r]  r|  r}  r~  r  )r  r  r   r#   r!   r"   r$   r   r   r  ri  r%   r   r   r  r  s                rP   r   zSyncClient.submit  sA   ( , 	*3S))))C"3M;#?@@@@C#.g;D(+++(333$}h777#H---'L(
 
 
 	(### 0111}} L)n-L)L)<
 
 
 	
rO   r  r  ri   r  Optional[Callable[[str], None]]r  "Optional[Callable[[Status], None]]r	  c       	           	
 |.
|
n)
|k    r#t          j        d
 d| dt          d           dgd	 
fd	}|
 |            S  j                            |          }	 |                    |
          S # t          j        j        $ r9}d}d         }||j	        }t          |           t          ||          |d}~ww xY w)r  Nr  r  r  r  r  rY   r  c            	                              	
          } | d<    | j                   $|                               D ]} |           |                                 S r  )r   r]  r  re  )r  r  r  r  r  r   r  r  r  r  r  r_   r  r  s     rP   r  z+SyncClient.subscribe.<locals>._do_subscribeM	  s    [[!+ !  F #JqM%
6,---*#//)/DD + +E#OE****::<<rO   r  r   r  r  )r  r  r  r}  r   r   r  r   r  r]  r  rZ  )r_   r  r  r  r  r  r  r  r  r   r  r	  r  r  r   r]  r  r  s   ```````````      @rP   r!  zSyncClient.subscribe'	  s   2 %$ .//gm g gWe g g g 	    :>
	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 * ! =??"&&}55	===888!. 
	 
	 
	J]F!#.
%f---'&%   
	s   8B C"4CCr]  c                D    t                               | j        ||          S r\   )r  r  r   r#  s      rP   r$  zSyncClient.get_handlet	  s     00{JWWWrO   r  rk  c               Z    |                      ||          }|                    |          S r&  r'  r(  s        rP   r  zSyncClient.statusw	  s+     j99}}y}111rO   c                V    |                      ||          }|                                S r\   r*  r+  s       rP   r   zSyncClient.result}	  s#    j99zz||rO   rZ   c                Z    |                      ||          }|                                 d S r\   r-  r+  s       rP   r  zSyncClient.cancel	  s&    j99rO   r.  r/  r0  Iterator[dict[str, Any]]c             #    K   t           |z   }|r|d|                    d          z   z  }t          | j        d|||          5 }|                                D ]}|                                V  	 ddd           dS # 1 swxY w Y   dS r3  )r  r  r   r   iter_sser   r5  s           rP   r7  zSyncClient.stream	  s       {* 	*3S))))CL&#Iw
 
 
 	#** # #jjll""""#	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   .A??BBr8  r   r9  r   r   rp  rq  rr  rs  rb  rc  c               ^   |                                  }t          |t                    r|                    d          }t	          |          }t          ||          }	t          |          t          k    rI|	d         dk    r=|d}|                                 }
t          
                    |
| j        ||||          S d|i}|||d<   t          ||           g }|	D ]}|dk    rB|                                 }
|                    dt          t          |
||	          f           J|d
k    r6|                    d
t          t           | j        |||||          f           |dk    r5|                    dt          t$          | j        |||||          f           t'          |          S r;  )r  rc  rF   r  rk  r{  r   r=  r  r   r  r   r   r   r   r  r  r   r  r  r>  s                 rP   rB  zSyncClient.upload	  s   & ~~dC   	(;;w''D8CC9+
 
 t99***/?/Bh/N/N (	))++F"''"1#),> (    "<0 )2G%&!'+=>>>8:$ &	 &	Dx--// 
FwOOO    ' L !%)5&/8J      / L !%)5&/8J     )222rO   rC  c          	     P   t          j        |          \  }}|d}t          ||          }t          j                            |          t          k    r`|d         dk    rTt          |          }|                                 }	t          
                    t          |          |	| j        ||          S t          |d          5 }
|                     |
                                |t          j                            |          |||          cddd           S # 1 swxY w Y   dS rE  )rF  rG  r{  r  r  r  r=  rk  r  r   r  rF   r   r  rB  r  r  rH  s              rP   rK  zSyncClient.upload_file	  sl     !+D11	12I9+
 
 GOOD!!$777 #x//!<Y!G!G))++F",,d))"1&,> -    $ 	;;		'**400%$7#   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A
DD"DrL  rM  rN  rO  c                   t          j                    5 }|                    ||           |                     |                                d| |||          cddd           S # 1 swxY w Y   dS rQ  rT  rX  s          rP   rZ  zSyncClient.upload_image'
  s    " Z\\ 	VJJvfJ---;;!!!!!%$7#   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA''A+.A+Tr[  r\  r]  r  r   r  r  r  r  Iterator[RealtimeConnection]c             #  .  K   d }d }	|r|                      ||          }	n#|                                 }
|
j        t          d}t	          ||	||          }t          ||          5 }t          |||          V  d d d            d S # 1 swxY w Y   d S r`  )r  r  r   r   r  r  r  ra  s                rP   r  zSyncClient.realtimeB
  s      *.  	U,,.> -  EE >>##D(,(9TTG!+um$OOOc7333 	r$NN     	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   (B

BBrb  r  c             #    K   d }d }|r|                      ||          }n#|                                 }|j        t          d}t	          ||||          }	t          |	|          5 }
|
V  d d d            d S # 1 swxY w Y   d S rd  )r  r  r   r   r  r  re  s              rP   rf  zSyncClient.ws_connect^
  s       *.  	U,,.> -  EE >>##D(,(9TTG"'	
 
 
 c7333 	rHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   (A::A>A>rg  r  )rY   r   )rY   ro   )rY   rz  rh  ri  )r  rF   r  r  r  rF   r  r   r   r   r  r  r   r   r  r  rY   r  )r  rF   r  r  r  rF   r  r   r  ri   r  r  r  r  r  r  r   r   r  r  r	  r  rY   r  )r  rF   r]  rF   rY   r  rj  rk  rl  )
r  rF   r  r  r  rF   r   r0  rY   r  r\   rm  rn  ro  rp  )r  rF   r]  ri   r  rF   r  r   r  r   r  r  r  r  rY   r  )r  rF   r]  ri   r  rF   r  r   r  r   rY   r  ) rJ   rK   rL   r
   rY  rM   r  r   r{   r  r  r   r   r!  r}  r  rq  r  r  r   r!  r$  r  r   r  r7  rB  rK  rZ  r   r  rf  rN   rO   rP   rs  rs  w  si        eDu555C5555"O""""0 0 0 _0
       	
 	
 	
 _	
 1 1 1 _1    X
 
 
 
 !B	6 6 6 6 6 6. /359"$, , , , , ,f "&'+"$59:
 :
 :
 :
 :
 :
B 6:>B'+"$596:K K K K K KZX X X X GL2 2 2 2 2 2        $# # # # # #: !%	X3 15 ,0X3 X3 X3 X3 X3 X3| 15 ,0- - - - - -d 
 15 ,0     6 
 $( A8<8<     ^6 
 $( A     ^  rO   rs  r9  c                    t          | t                    r|                     d          } d| dt          j        |                                            S )zIEncode the given data blob to a data URL with the specified content type.r<  zdata:z;base64,)rc  rF   r  base64	b64encoder  )r   r   s     rP   r  r  |
  sR    $ ${{7##J<JJ)9$)?)?)F)F)H)HJJJrO   rC  c                    t          j        |           \  }}|d}t          | d          5 }t          |                                |          cddd           S # 1 swxY w Y   dS )zUEncode a file from the local filesystem to a data URL with the inferred content type.Nr   r  )rF  rG  r  r  r  )r  rI  r  rJ  s       rP   encode_filer  
  s    '--LIq.		dD		 .Tdiikk9--. . . . . . . . . . . . . . . . . .s   "AA"ArL  rM  rN  rO  c                    t          j                    5 }|                     ||           t          |                                d|           cddd           S # 1 swxY w Y   dS )zEEncode a pillow image object to a data URL with the specified format.rR  rS  N)rU  rV  r  r  rW  )rM  rO  rY  s      rP   encode_imager  
  s    	 <

6&
)))foo''):&):):;;< < < < < < < < < < < < < < < < < <s   <AA!$A!)r   rU  rY   rZ   )r  rF   rY   rF   )r  r  rY   rF   )r  r   rY   r   )
r  rF   re   r   r  rF   r  r   rY   rF   )
r  rF   re   r   r  r   r  rF   rY   rF   )r   r   rY   rF   )rR  r   rY   r   )rR  r   rY   r   )rR  r   r  r  rY   r  )rR  ru  r  r  rY   r   r\   )r   rF   r   r  rY   r  )r   rF   r   r  rY   r  )
r   r   r"  rF   r   rF   r#  r   rY   rU  )
r   r%  r"  rF   r   rF   r#  r   rY   rU  )r   rU  rY   ri   )r   rU  r1  r2  rY   ri   )rf  r  r1  r2  rY   ri   )r;  F)r<  r   r=  r[  r>  r[  r?  r@  rA  ri   rY   r[  )r   r   r"  rF   r   rF   r1  r2  r#  r   rY   rU  )r   r%  r"  rF   r   rF   r1  r2  r#  r   rY   rU  )rp   r   rY   rF   )r   r   r   r   rY   rZ   )rW   r`  rY   r   )rb  rc  rY   r   )
rp   r   r   rF   r   r   r   r   rY   r   )rp   r   r   r   rY   r   )rp  rq  rr  rs  rY   r<   )r   r   r   rF   rY   r   )r   r   rp   r   r   r   r   rF   r   r   r   r   rY   rF   )r   r%  rp   r   r   r   r   rF   r   r   r   r   rY   rF   )r   r   r   r   r   r   rY   rF   )r   r%  r   r   r   r   rY   rF   )r  r  rY   rF   )r  r  rY   rF   )r  r  rY   rZ   )r  r  rY   rZ   )r   r9  r   rF   rY   rF   )r  rC  rY   rF   ro  )rM  rN  rO  rF   rY   rF   )
__future__r   r  rU  r   r   r  rF  r   pathlibr   rC  rS  r  r   loggingr   r  
contextlibr   r   r   r   dataclassesr	   r
   	functoolsr   r   typingr   r   r   r   r   r   r   r   r   r   urllib.parser   r  r   	httpx_sser   r   fal_client.authr   r   r   r   r   fal_client._versionr   fal_client._headersr    r!   r"   r#   r$   r%   r&   r'   websockets.clientr(   websockets.sync.connectionr)   	getLoggerrJ   rQ  PILr*   rF   r  r:   LifecyclePreferencePayloadr   r`  rH   r  r  r  r   r  environre  r  r;   rM   r=   r   MIN_REQUEST_TIMEOUT_SECONDSr   r   r|  _IMMEDIATE_EXPIRATION_SECONDSrB   rE   rR   rV   rd   ro   r   r=  r   r   r   r$  r  rN  rQ  r  rZ  ri  rk  rm  rp  rt  ry  r  r  r  rq  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r!  r   r.  rM  rO  rP  r4  r/  Timeoutr  r0  r5  r:  rF  r   r+  r\  r   r]   rk  rm  ro  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r   rs  r  r  r  rN   rO   rP   <module>r     s6   " " " " " " "  				   				                        : : : : : : : : ' ' ' ' ' ' ' ' ( ( ( ( ( ( ( ( . . . . . . . .                        # " " " " "   / / / / / / / /              , + + + + +	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  6999999555555		8	$	$ 
sCx.34 !#s(^ ?@	  67 +L+++30333 .|...  
 z~~n6IJJ 08  8 8 8 8@Eu~ " M M M M1;111
  00, 1   !#   /




> 
> # 
 
 
 
 $! ! ! ! ! ! ! !
 $. . . . . . . .
 $E E E E E E E E  = = = = = = = =" " " " " " " "J" " " " " " " "J ( '  s$ s$ s$ s$ s$ s$ s$ s$l~* ~* ~* ~* ~* ~* ~* ~*B	 	 	 	 	Y 	 	 	 ! ! ! ! ! ! ! ! W W W W WNL W W W   <               V    0 0 0 0 0 0 0 0 	1 	1 	1 	1 	1 	1 	1 	1 $B B B B B B B B0 w'X X X X $
 
 
 
 
 
 
 
6 %( !   < < < <	 	 	 	   $     8      D D D D" " " " "L " " " / / / /5 5 5 5       >    ( ( ( ( ( ( ( (V ( ( ( ( ( ( ( (V /3     /3           
	oo%oo  #U]5$777    & $&    & BD     4 5B! ! ! ! !. $&4 4 4 4 4 4> $&4 4 4 4 4 40   	F 	F 	F 	F   "   :
 
 
 

 
 
 
   .    FJ     J FJ     <) ) ) ). FJ) ) ) ) ) )*) ) ) ). FJ) ) ) ) ) )*9 9 9 9$9 9 9 9*       $I$ I$ I$ I$ I$* I$ I$ I$X $L$ L$ L$ L$ L$+ L$ L$ L$^ $I I I I I I I IX $A A A A A A A AHK K K K. . . .< < < < < < <rO   