
    Ojl7                       S r SSKrSSKJr  SSKrSSKJr  SSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJrJrJrJrJrJrJr  SSKJr  SSKJr  SSKrSSKrSSKrSSK r!SSK"r!SSK"J#r#  SS	K$J%r%  SS
K&J'r'  SSK J(r)  SSK*r*SSK+J,r,  SSK+J-r-  SSK.r.SSK/J0r0  SSK1r1SSK2J3r3  SSK2J4r4  SSK2J5r5  SSK6J7r7  SSK6J8r8  SSK6J9r:  SSK6J;r;  SSK6J<r<   SSK=J>r?  SrB SSKCrCSrB\(       a  SSKEJFrF  SSKGJHrH  \
R                  " S5      rJSrKSrLS rMSrNS!rOS"S#1rP " S$ S%\Q5      rRS&\\S   4S' jrTS(\U\S\S4   S&S4S) jrVS*\7S+\7S&\74S, jrWS(\U\S\S4   S-\\\X\Y4      S&S4S. jrZS/\SS0\SS&\S4S1 jr[S2\\SS4   S&\\#\S4   4S3 jr\S4\#S&\#4S5 jr]S6\\\X\Y4      S&\\X   4S7 jr^\ " S8 S95      5       r_ " S: S;5      r9S<r`S=raS>rbS!rcSrdS?reS*\\;   S&\3R                  4S@ jrg " SA SB\*R                  5      ri " SC SD\*R                  5      rk " SE SF5      rlSGSFS4\!R                  R                  RF                  S&\S4SH jroSGSFS4\!R                  R                  RF                  S&\S4SI jrpg! \@ a
    SSKAJ>r?   GNf = f! \D a     GNf = f)JzBase client for calling HTTP APIs sending and receiving JSON.

The BaseApiClient is intended to be a private module and is subject to change.
    N)	Generator)	dataclass)AnyAsyncIteratorIteratorOptionalTYPE_CHECKINGTupleUnion)urlparse)
urlunparseCredentials)mtls)AuthorizedSession)
exceptions)	BaseModel)ValidationError)CaseInsensitiveDict   )_common)errors)version)HttpOptions)HttpOptionsOrDict)HttpResponse)HttpRetryOptions)ResourceScope)connectFT)CIMultiDictProxyAsyncAuthorizedSessionzgoogle_genai._api_clienti   i  @       useuc                       \ rS rSrSrSrg)EphemeralTokenAPIKeyErrorb   z)Error raised when the API key is invalid. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r*       Z/var/www/html/leaf-doctor-v1/venv/lib/python3.13/site-packages/google/genai/_api_client.pyr(   r(   b   s    1r1   r(   returnc                      [         R                  R                  SS5      n [         R                  R                  SS5      nU (       a  U(       a  [        R	                  S5        U =(       d    U=(       d    S$ )zGets the API key from environment variables, prioritizing GOOGLE_API_KEY.

Returns:
    The API key string if found, otherwise None. Empty string is considered
    invalid.
GOOGLE_API_KEYNGEMINI_API_KEYzEBoth GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY.)osenvirongetloggerwarning)env_google_api_keyenv_gemini_api_keys     r2   get_env_api_keyr>   h   sX     zz~~&6=zz~~&6=.
NNO 
	91	9T9r1   headersc                    S[         R                   3nS[        R                   R                  5       S   -   nU SU 3nSU ;   a  XS   ;  a  U S3U S   -   U S'   O&SU ;   a  X S   ;  a  U S    SU 3U S'   O
SU ;  a  X0S'   SU ;   a  XS   ;  a  U S3U S   -   U S'   gSU ;   a  X S   ;  a  U S    SU 3U S'   gSU ;  a  X0S'   gg)z1Appends the telemetry header to the headers dict.zgoogle-genai-sdk/z
gl-python/r    z
user-agentzx-goog-api-clientN)r   __version__syssplit)r?   library_labellanguage_labelversion_header_values       r2   append_library_version_headersrH   y   s8   %g&9&9%:;-#++"3"3"5a"88.)?!N+;<g
|4
434A69NNGLw>9N#N&|45Q~6FGGL7"0LW$
':;
;  
 "W-@%AA   W$
(; <
< &'
(.)9:   ')#7  *r1   optionspatch_optionsc           	         U R                  5       nUR                  =(       d    0 nUR                  =(       d    0 n0 UEUEUl        [        R                  R	                  5       nU H=  nUS:X  a  M  [        XS 5      nUb  [        X&U5        M(  [        X&[        X5      5        M?     UR                  b  [        UR                  5        U$ )Nr?   )
model_copyr?   r   model_fieldskeysgetattrsetattrrH   )rI   rJ   copy_optionoptions_headerspatch_options_headershttp_options_keyskeypatch_values           r2   patch_http_optionsrW      s     ""$+''-2/'//52+
 "..335c
i-d3Kk,k 56  $";#6#67	r1   timeout_in_secondsc                 f    U(       a*  SU ;  a#  [        [        R                  " U5      5      U S'   ggg)z8Populates the server timeout header in the headers dict.zX-Server-TimeoutN)strmathceil)r?   rX   s     r2   populate_server_timeout_headerr]      s1     .g="%dii0B&C"DG >r1   base_urlpathc                    [        U 5      nUR                  R                  S5      (       a  UR                  S S OUR                  nUR                  S5      (       a  USS  OUn[	        UR                  US-   U-   S95      $ )N/r   )r_   )r   r_   endswith
startswithr   _replace)r^   r_   parsed_base	base_paths       r2   join_url_pathrh      s    "+ 
			"	"3	'	' s 
 __S))abt$	K((i#o.D(E	FFr1   projectc                 ~    [         R                  R                  S/S9u  pU (       d  Un U (       d  [        S5      eX4$ )z-Loads google auth credentials and project id.z.https://www.googleapis.com/auth/cloud-platform)scopesz@Could not resolve project using application default credentials.)googleauthdefault
ValueError)ri   credentialsloaded_project_ids      r2   	load_authrr      sL    #);;#6#6>? $7 $ + 
G	
J  
	r1   rp   c                 >    SSK Jn  U R                  U" 5       5        U $ )Nr   )Request)google.auth.transport.requestsrt   refresh)rp   rt   s     r2   refresh_authrw      s    4gi 	r1   timeoutc                 &    U (       a  U S-  nU$ SnU$ )z Converts the timeout to seconds.g     @@Nr*   )rx   rX   s     r2   get_timeout_in_secondsrz      s*      !6) 
 	r1   c                   p    \ rS rSr% \\\4   \S'   \\S'   \\S'   \\\\4   \	4   \S'   Sr
\\   \S'   Srg)	HttpRequest   r?   urlmethoddataNrx   r*   )r+   r,   r-   r.   dictrZ   __annotations__r   objectbytesrx   r   floatr0   r*   r1   r2   r|   r|      sA    S>
(
+d3;&''!'8E?!r1   r|   c            	       T   \ rS rSr  SS\\\\4   \R                  S\	4   S\\
\4   S\\
\4   4S jjrSS	 jrS\
4S
 jr\S\
4S j5       rS\\
SS4   4S jrS\\
   4S jrS\\\\
4   SS4   4S jrS\\\4   SS4S jrS\\   4S jrS\\   4S jr\S\
S\
4S j5       rSrg)r      Nr?   zCIMultiDictProxy[str]response_streambyte_streamc           	         [        U[        5      (       a  Xl        GO[        U[        R                  5      (       aE  UR                  5        Vs0 s H#  oDSR                  UR                  U5      5      _M%     snU l        O[        U[        5      (       a+  UR                  5        VVs0 s H  u  pEXE_M	     snnU l        O][        U5      R                  S:X  aD  UR                  5        Vs0 s H#  oDSR                  UR                  U5      5      _M%     snU l        SU l        X l        X0l        g s  snf s  snnf s  snf )Nz, r       )
isinstancer   r?   httpxHeadersrN   joinget_listr   itemstyper+   getallstatus_coder   r   )selfr?   r   r   rU   values         r2   __init__HttpResponse.__init__   s     '4  l	GU]]	+	+;B<<>;ICtyy))#./
/>dl 
G0	1	13:==?C?ZScj?Cdl	g		#5	59@9G#tyy,-
-dl  D*" Ds   *D3*D8-*D>r3   c                 0    U R                  5       U l        U $ N)async_segmentssegment_iteratorr   s    r2   	__aiter__HttpResponse.__aiter__  s     //1DKr1   c                 |   #     U R                   R                  5       I S h  vN $  N! [         a    [        ef = f7fr   )r   	__anext__StopIterationStopAsyncIterationr   s    r2   r   HttpResponse.__anext__  s9     ((224444 s    <( &( <( 9<c                     [        U R                  [        5      (       d  g U R                  (       a  U R                  S   (       d  gU R                  U R                  S   5      $ )Nr    )r   r   list_load_json_from_responser   s    r2   jsonHttpResponse.json  sO     d**D11t';';A'>(()=)=a)@AAr1   c              #   2  #    [        U R                  [        5      (       a0  U R                   H  nU(       a  U R                  U5      O0 v   M!     g U R                  c  /  S h  vN   g U R	                  5        H  nU R                  U5      v   M     g  N07fr   )r   r   r   r   _iter_response_stream)r   chunks     r2   segmentsHttpResponse.segments*  s{     $&&--''%6;d++E2C (				%mm --/%++E22 0 s   A"B$B%1Bc                V  #    [        U R                  [        5      (       a1  U R                   H   nU(       a  U R                  U5      O0 7v   M"     g U R                  c  /   S h  vN nU7v   M  U R	                  5         S h  vN nU R                  U5      7v   M   N:
 g  N
 g 7fr   )r   r   r   r   _aiter_response_stream)r   r   cs      r2   r   HttpResponse.async_segments6  s     $&&--''%6;d++E2C (				%  446 3++E22R36sN   A B)"B#&B!'B#*B)B'B%B'
B)!B##B)%B''B)c              #      #    [        U R                  [        5      (       a  U R                   S h  vN   g U R                  c  /  S h  vN   g [        S5      e N( N7f)Nz8Byte segments are not supported for streaming responses.)r   r   r   ro   r   s    r2   byte_segmentsHttpResponse.byte_segmentsC  sR     $""D))!!!!				!mm
D 	 "s!   /AAA	A
AAresponse_payloadc                     [        U S5        [        U 5       H$  n[        R                  " [	        X5      5      X'   M&     g )Nr   )delattrdircopydeepcopyrO   )r   r   	attributes      r2   _copy_to_dictHttpResponse._copy_to_dictN  s2    D$%Y	$(MM'$2J$K! r1   c              #   J  #    [        U R                  [        R                  5      (       dU  [        U R                  [        R                  5      (       d,  [        S[        U R                  5      R                   S35      eSnSn/ n[        U R                  [        R                  5      (       a  U R                  R                  5       nOU R                  R                  SS9nU H  nU(       d  U(       a  SR                  U5      v   / nM(  UR                  S5      (       a  UR                  U[        S5      S	 5        M]  U H  nUS
:X  a  US-  nM  US:X  d  M  US-  nM     X-  nUS:X  d  M  Uv   SnM     U(       a  Uv   U(       a  SR                  U5      v   g	g	7f)z,Iterates over chunks retrieved from the API.zFExpected self.response_stream to be an httpx.Response object, but got .r   r   T)decode_unicode
data: N{r   })r   r   r   Responserequests	TypeErrorr   r+   
iter_linesr   rd   appendlen)r   r   balancedata_bufferr   liner   s          r2   r   "HttpResponse._iter_response_streamT  sw     	4''88d**H,=,=>>$../889< 
 EGK$&&77,,779o,,77t7Lo		+&
&+ 
	"	"4H/0
 !8
Q,'#X
Q,'	  me	A3  8 kIIk"" s   EF#F#21F#c                j  #    [        U R                  [        R                  5      =(       d1    [        =(       a$    [        U R                  [
        R                  5      nU(       d,  [        S[        U R                  5      R                   S35      eSnSn/ n[        U R                  [        R                  5      (       a   U R                  R                  5         Sh  vN nU(       d  U(       a  SR                  U5      7v   / nM/  UR                  S5      (       a  UR                  U[        S5      S 5        Md  U H  nUS:X  a  US	-  nM  US
:X  d  M  US	-  nM     X%-  nUS:X  d  M  U7v   SnM  [        (       GaW  [        U R                  [
        R                  5      (       Ga,     U R                  R                   R#                  [$        S9I Sh  vN nU(       d  OUR'                  S5      R)                  5       nU(       d  U(       a  SR                  U5      7v   / nM~  UR                  S5      (       a  UR                  U[        S5      S 5        M  U H  nUS:X  a  US	-  nM  US
:X  d  M  US	-  nM     X%-  nUS:X  a  U7v   SnM  U(       a  U7v   U(       a  SR                  U5      7v   U R                  R+                  5         ggg GN
 U(       a  U7v   U(       a  SR                  U5      7v   U R                  R                  5       I Sh  vN    g! U R                  R                  5       I Sh  vN    f = f GNx! [         a1    U R                  R                   R#                  5       I Sh  vN  n GNf = f! U R                  R+                  5         f = f7f)z;Asynchronously iterates over chunks retrieved from the API.z`Expected self.response_stream to be an httpx.Response or aiohttp.ClientResponse object, but got r   r   r   Nr   r   r   r   r   )max_line_lengthutf-8)r   r   r   r   has_aiohttpaiohttpClientResponser   r   r+   aiter_linesr   rd   r   r   aclosecontentreadlineREAD_BUFFER_SIZEdecoderstriprelease)r   is_valid_responser   r   r   r   r   
line_bytess           r2   r   #HttpResponse._aiter_response_stream  s<    "4#7#7H P
4#7#79O9OP  4''(112!5  EGK$&&77!,..::< 	$IIk**k __X&&tCMN34
 aCxlgclg	  -%\KE 
g44 /'G#33;;DD 0  E   J ""7+224$IIk**k __X&&tCMN34
 aCxlgclg	  -%\KEM P +		+&
& 	$$&eG	<4 +		+&
& ""))+++d""))+++  G  $33;;DDFFFJGP 	$$&s   B?N3L+ K K!K$A2L+ L+ /	L+ 86N3/N 1*M MM  BN <AN ?N3K(L+ N3$L'%N3+M	M
MN3M 1NN
NN NN N00N3responsec                      [         R                  " U5      $ ! [         R                   a  n[        R                  " SU 35      UeSnAff = f)zFLoads JSON from the response, or raises an error if the parsing fails.z0Failed to parse response as JSON. Raw response: N)r   loadsJSONDecodeErrorr   UnknownApiResponseError)clsr   es      r2   r   %HttpResponse._load_json_from_response  sL    ZZ!! **<XJ
Gs    AAA)r   r?   r   r   r   )NN)r3   r   )r+   r,   r-   r.   r   r   rZ   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   r   r   r   classmethodr   r0   r*   r1   r2   r   r      s<    *.'+#
sCx.
--
!
# S#X# e$#:s  	BC 	B 	B
3	#tT/2 
33M#$6 3	YuUCZ'8$'DE 	LDf,= L$ L1#Xc] 1#fg'M#,> g'R c c  r1   r      g      ?g      N@)i  i  i  i  i  i  c                 n  ^ U c  [         R                  " S5      SS.$ U R                  S:X  a  SU l        [         R                  " U R                  =(       d    [        5      nU R                  =(       d    [
        m[         R                  " U4S j5      n[         R                  " U R                  =(       d    [        U R                  =(       d    [        U R                  =(       d    [        U R                  =(       d    [        S9nUUSU[         R                   " ["        [$        R&                  5      S.$ )a  Returns the retry args for the given http retry options.

Args:
  options: The http retry options to use for the retry configuration. If None,
    the 'never retry' stop strategy will be used.

Returns:
  The arguments passed to the tenacity.(Async)Retrying constructor.
r   T)stopreraiser   c                    > [        U [        R                  5      =(       a    U R                  T;   =(       d*    [        U [        R
                  [        R                  45      $ r   )r   r   APIErrorcoder   TimeoutExceptionConnectError)r   retriable_codess    r2   <lambda>retry_args.<locals>.<lambda>  sE    Av/MAFFo4M E	A..0B0BC	DEr1   )initialmaxexp_basejitter)r   retryr   waitbefore_sleep)tenacitystop_after_attemptattempts_RETRY_ATTEMPTShttp_status_codes_RETRY_HTTP_STATUS_CODESretry_if_exceptionwait_exponential_jitterinitial_delay_RETRY_INITIAL_DELAY	max_delay_RETRY_MAX_DELAYr   _RETRY_EXP_BASEr   _RETRY_JITTERbefore_sleep_logr:   loggingINFO)rI   r   r   r   r   s       @r2   
retry_argsr    s     _//2tDDG		$	$W%5%5%H	I$--I1I/

%
%E% 
	)	)##;';



//2?^^,}	
$ //E
 r1   c                   B   ^  \ rS rSrSrS\SS4U 4S jjrS	S jrSrU =r	$ )
SyncHttpxClienti1  zSync httpx client.kwargsr3   Nc                 J   > UR                  SS5        [        TU ]  " S0 UD6  gzInitializes the httpx client.follow_redirectsTNr*   
setdefaultsuperr   r   r  	__class__s     r2   r   SyncHttpxClient.__init__4  $    
($/	Gvr1   c                      U R                   (       a  g  U R                  5         g! [         a     Nf = f! [         a     gf = f)zCloses the httpx client.N)	is_closed	Exceptioncloser   s    r2   __del__SyncHttpxClient.__del__9  sK    	 

jjl  
  
s   ' 7 
44
AAr*   r3   N
r+   r,   r-   r.   r/   r   r   r"  r0   __classcell__r  s   @r2   r  r  1  s#    s t 

 
r1   r  c                   B   ^  \ rS rSrSrS\SS4U 4S jjrS	S jrSrU =r	$ )
AsyncHttpxClientiF  zAsync httpx client.r  r3   Nc                 J   > UR                  SS5        [        TU ]  " S0 UD6  gr  r  r  s     r2   r   AsyncHttpxClient.__init__I  r  r1   c                      U R                   (       a  g   [        R                  " 5       R	                  U R                  5       5        g ! [         a     N@f = f! [         a     g f = fr   )r  r   asyncioget_running_loopcreate_taskr   r   s    r2   r"  AsyncHttpxClient.__del__N  s_    	 
 ,,T[[];  
  
s"   A	 2A 	
AA
A&%A&r*   r$  r%  r'  s   @r2   r)  r)  F  s#    s t 
	 	r1   r)  c                   ~   \ rS rSrSr      S9S\\   S\\   S\\R                  R                  R                     S\\   S\\   S	\\   4S
 jjrS\4S jrS\4S jrS\S   4S jr\S\S\\R,                  \R,                  4   4S j5       r\S\S\R,                  4S j5       r\S\S\R,                  4S j5       rS\4S jrS\4S jrS\4S jrS\R<                  4S jrS\\\ 4   4S jr! S:S\S\S\"\\#4   S	\\   S\$4
S jjr% S;S\$S\S\&4S jjr'  S<S\$S	\\   S\S\&4S  jjr( S;S\$S\S\&4S! jjr)  S<S\$S	\\   S\S\&4S" jjr*S\R,                  4S# jr+ S:S\S\S\"\\#4   S	\\   S\,4
S$ jjr- S:S\S\S\"\\#4   S	\\   S\.\,SS4   4
S% jjr/ S:S\S\S\"\\#4   S	\\   S\,4
S& jjr0 S:S\S\S\"\\#4   S	\\   S\ 4
S' jjr1SS(.S)\\\2Rf                  4   S*\S+\4S	\\   S\&4
S, jjr5SS(.S-\2Rf                  S*\S+\4S	\\   S\&4
S. jjr6SS(.S\S	\\   S\\ \74   4S/ jjr8SS(.S)\\\2Rf                  4   S*\S+\4S	\\   S\&4
S0 jjr9SS(.S-\\2Rf                  \:Rv                  \    4   S*\S+\4S	\\   S\&4
S1 jjr<SS(.S\S	\\   S\\ \74   4S2 jjr=S3\R|                  SS4S4 jr?S=S5 jr@S=S6 jrAS=S7 jrBS8rCg)>BaseApiClientiZ  z8Client for calling HTTP APIs sending and receiving JSON.Nvertexaiapi_keyrp   ri   locationhttp_optionsc                    Xl         S U l        U R                   c  [        R                  R	                  SS 5      n[        R                  R	                  SS 5      nS n	Ub  UR                  5       S;   n	S n
Ub  UR                  5       S;   n
U	b  U
b  X:w  a  [        R                  " S5        U	b  Xl         O	U
b  Xl         U(       d  U(       a  U(       a  [        S5      eU(       a  U(       a  [        S5      e[        5       n[        U[        5      (       a   [        R                  " U5      nO)U(       a"  [        R                  " U[        5      (       a  UnUR                   (       a  UR"                  (       d  [        S5      e[        R                  R	                  S	S 5      n[        R                  R	                  S
S 5      n[%        5       nU=(       d    UU l        U=(       d    UU l        U=(       d    UU l        X0l        [        5       U l        [0        R2                  " 5       U l        S U l        S U l        U R                   (       Ga&  U(       a$  U(       a  [:        R=                  S5        S U l        OU(       d  U(       a+  U(       a$  [:        R=                  S5        S U l        S U l        OcU(       d  U(       a$  U(       a  [:        R=                  S5        S U l        O1U(       d  U(       a#  U(       a  [:        R=                  S5        S U l        UR"                  (       a  UR"                  OS U l        U R(                  (       dQ  U R*                  (       d@  U R                  (       d  SU l        O'U R                  R?                  S5      (       a  SU l        U R&                  (       dI  U R*                  (       d8  U R                  (       d'  [A        S S9u  o0l        U R,                  (       d  X0l        U R&                  =(       a    U R(                  =(       d    U R*                  nU(       d  U R                  (       d  [        S5      eU R*                  (       d  U R(                  S:X  a#  U R                  (       d  SU R.                  l        OU R(                  [B        ;   a1  U R                  (       d   SU R(                   S3U R.                  l        OU R                  (       a_  U R                  R?                  S5      (       d?  U(       a  U(       d1  U(       d*  U R                  U R.                  l        S U l        S U l        OSU R(                   S3U R.                  l        SU R.                  l"        O>U R*                  (       d  [        S5      eSU R.                  l        SU R.                  l"        SS0U R.                  l#        U R*                  (       aY  U R*                  RI                  5       U l        U R.                  RF                  b#  U R*                  U R.                  RF                  S'   U(       a  [K        U R.                  U5      U l        O6U R.                  RF                  b  [M        U R.                  RF                  5        U RO                  U R.                  5      u  nnUU l(        S U l)        U RU                  5       (       a  S U l+        OGU R.                  RX                  (       a  U R.                  RX                  U l+        O[[        S&0 UD6U l+        U R]                  5       (       a  S U l/        OGU R.                  R`                  (       a  U R.                  R`                  U l/        O[c        S&0 UD6U l/        U R.                  R`                  (       a  U R.                  R`                  U l/        O[c        S&0 UD6U l/        S U l2        U Rg                  5       (       a   SS K4nU R.                  Rj                  (       a  U R.                  Rj                  U l2        U Rm                  U R.                  5      U l7        U R]                  5       (       al  S U Rn                  S!'   U R.                  Rp                  c  [s        S"5      O[s        U R.                  Rp                  5      U Rn                  S#'   S$U Rn                  S%'   [w        U R.                  Rx                  5      nU R{                  U R.                  5      U l>        [~        R                  " S&0 UD6U lA        [~        R                  " S&0 UD6U lC        g ! [         a  n[        S5      UeS nAff = f! [t         a     Nf = f)'NGOOGLE_GENAI_USE_ENTERPRISEGOOGLE_GENAI_USE_VERTEXAI)true1zWarning: Both GOOGLE_GENAI_USE_ENTERPRISE and GOOGLE_GENAI_USE_VERTEXAI are set with conflicting values. The value of GOOGLE_GENAI_USE_ENTERPRISE will be used.zNProject/location and API key are mutually exclusive in the client initializer.zICredentials and API key are mutually exclusive in the client initializer.zInvalid http_optionsz9base_url must be set when base_url_resource_scope is set.GOOGLE_CLOUD_PROJECTGOOGLE_CLOUD_LOCATIONzoThe user provided Google Cloud credentials will take precedence over the API key from the environment variable.zrThe user provided Vertex AI API key will take precedence over the project/location from the environment variables.zqThe user provided project/location will take precedence over the Vertex AI API key from the environment variable.zyThe project/location from the environment variables will take precedence over the API key from the environment variables.globalz.googleapis.comri   z<Project or API key must be set when using the Vertex AI API.z"https://aiplatform.googleapis.com/zhttps://aiplatform.z.rep.googleapis.com/zhttps://z-aiplatform.googleapis.com/v1beta1zNo API key was provided. Please pass a valid API key. Learn how to create an API key at https://ai.google.dev/gemini-api/docs/api-key.z*https://generativelanguage.googleapis.com/v1betazContent-Typezapplication/jsonzx-goog-api-keyr   Tsslinfmax_allowed_timer   total_attemptsr*   )Dr3  custom_base_urlr7   r8   r9   lowerwarningswarnro   r   r   r   model_validater   r   is_duck_type_ofbase_url_resource_scoper^   r>   ri   r5  r4  _credentials_http_options	threadingLock_sync_auth_lock_async_auth_lock_async_auth_lock_creation_lockr:   inforc   rr   _MULTI_REGIONAL_LOCATIONSapi_versionr?   striprW   rH   _ensure_httpx_ssl_ctx_async_httpx_client_args_authorized_session_use_google_auth_sync_httpx_clienthttpx_clientr  _use_google_auth_async_async_httpx_clienthttpx_async_clientr)  _aiohttp_session_use_aiohttpr   aiohttp_client_ensure_aiohttp_ssl_ctx"_async_client_session_request_argsrx   r   ImportErrorr  retry_options_ensure_websocket_ssl_ctx_websocket_ssl_ctxr   Retrying_retryAsyncRetrying_async_retry)r   r3  r4  rp   ri   r5  r6  env_enterprise_strenv_vertexai_strenv_enterpriseenv_vertexaivalidated_http_optionsr   env_projectenv_locationenv_api_keyhas_sufficient_authclient_argsasync_client_argsr   retry_kwargss                        r2   r   BaseApiClient.__init__]  s~    MD}}::>>*GN(CTJn		'+113}Dl		%'--/=@ 
$&,B	
 
	#&#$ 	8  
  )],%%8!,!;!;L!I 
'11,LL+ 	66&// 
E 
 **..!7>K::>>"94@L!#K)kDL,DM)kDL#$D %>>+D48DBFD' }}}	A	
 KWB	
 x[B	
 K[M	
  $,, !
)
)  mm,,##"$-!!**+<=="$- ll,,&&$-d$;!\  )
!\\;dmmL )=)=J
 	
 ,,$--83$$(J#
--4
4&& "$--0DE 	# 

&&//0ABBh7 '+&:&:# t}}o%@A 	# (1d$\\>
 	

 %Qd!'/d$"02D!ED||\\'')dl				#	#	/7;||""#34-


4d 
			#	#	/&t'9'9'A'AB%)%?%?&"K" %6D!<@D!!##d				(	(--::d*9[9d""$$!%d				.	.!%!3!3!F!Fd!1!F4E!Fd,,!%!3!3!F!Fd!1!F4E!Fd aeD,,"&"4"4"C"C$
 ((););< 	/ &&((;?$
1
1%
8 !!))1 INI7<  ((8 
1
12D
E
 GH$
1
12B
C d00>>?L"<<T=O=OPD##3l3DK ..>>DM  8/0a78@  s+   d1  Ce 1
e;ee
eer3   c                 F   [        [        S5      (       d  g[        U R                  =(       ap    [        R                  " 5       =(       aT    [        R
                  " 5       =(       a8    U R                  R                  =(       d    U R                  R                  (       + 5      $ )Nshould_use_client_certF)	hasattrr   boolr3  r|  has_default_client_cert_sourcerN  r]  rw  r   s    r2   r[  #BaseApiClient._use_google_auth_sync_  su    4122 	
'')	
//1	
 ++Mt/A/A/M/M
	 r1   c                 (   [        [        =(       a}    U R                  =(       aj    [        [        S5      =(       aS    [        R
                  " 5       =(       a7    [        R                  " 5       =(       a    U R                  R                  (       + 5      $ )Nr|  )	r~  r   r3  r}  r   r|  r  rN  r`  r   s    r2   r^  $BaseApiClient._use_google_auth_asynck  sm     	6MM	6D23	6 '')	6 //1		6
 ""555 r1   )zaiohttp.ClientSessionr"   c                 N  #    U R                   cL  U R                  5       (       a7   SSKJn  SSKJn   " S SU5      nU" U" U 5      5      U l         U R                   $ U R                  5       (       d  U R                   bD  U R                   R                  (       d)  U R                   R                  R                  5       (       aG   " S S[        R                  5      n " S S	[        R                  5      nU" U" SS
9S[        S9U l         U R                   $ ! [         a     Nf = f7f)z#Returns the aiohttp client session.r   r   r!   c            
       p   ^  \ rS rSrSrSU 4S jjrS\S\S\S\\\4   SS	4
S
 jr	\
S\4S j5       rSrU =r$ )HBaseApiClient._get_aiohttp_session.<locals>._RefreshableAsyncCredentialsi  zJAdapter to use the client's sync credentials in an AsyncAuthorizedSession.c                 .   > [         TU ]  5         Xl        g r   )r  r   _client)r   clientr  s     r2   r   QBaseApiClient._get_aiohttp_session.<locals>._RefreshableAsyncCredentials.__init__  s    G!Lr1   requestr   r~   r?   r3   Nc                 .  #    U R                   R                  5       I S h  vN nSU 3US'   U R                   R                  (       aJ  U R                   R                  R                  (       a$  U R                   R                  R                  US'   g g g  Nr7f)NBearer Authorizationx-goog-user-project)r  _async_access_tokenrM  quota_project_id)r   r  r   r~   r?   tokens         r2   before_requestWBaseApiClient._get_aiohttp_session.<locals>._RefreshableAsyncCredentials.before_request  s|      ,,::<<E)0'8GO$))LL-->> ,,++<< +, ? * =s   BBA3Bc                     U R                   R                  (       d  gU R                   R                  R                  (       + $ NF)r  rM  expiredr   s    r2   validNBaseApiClient._get_aiohttp_session.<locals>._RefreshableAsyncCredentials.valid  s+    <<,,||008888r1   )r  )r  r2  )r+   r,   r-   r.   r/   r   r   rZ   r   r  r   r~  r  r0   r&  r'  s   @r2   _RefreshableAsyncCredentialsr    s[    
Z" *-47BFsCx. 9T 9 9r1   r  c                   ,    \ rS rSr\4S\SS4S jjrSrg)@BaseApiClient._get_aiohttp_session.<locals>.AiohttpClientSessioni  	_warningsr3   Nc                 l    U R                   (       d#  U SS.nU R                  b  U R                  US'   g g g )NzUnclosed client session)client_sessionmessagesource_traceback)closed_source_traceback)r   r  contexts      r2   r"  HBaseApiClient._get_aiohttp_session.<locals>.AiohttpClientSession.__del__  s?    "&4G %%1,0,B,Bg() 2 r1   r*   r+   r,   r-   r.   rH  r   r"  r0   r*   r1   r2   AiohttpClientSessionr    s!    +3 	CS 	C 	C 	Cr1   r  c                   ,    \ rS rSr\4S\SS4S jjrSrg)?BaseApiClient._get_aiohttp_session.<locals>.AiohttpTCPConnectori  r  r3   Nc                 "   U R                   (       a  g U R                  (       d  g U R                  R                  5        Vs/ s H  n[        U5      PM     nnU R	                  5         U USS.nU R
                  b  U R
                  US'   g g s  snf )NzUnclosed connector)	connectorconnectionsr  r  )_closed_connsvaluesrepr_closer  )r   r  r   connsr  s        r2   r"  GBaseApiClient._get_aiohttp_session.<locals>.AiohttpTCPConnector.__del__  s    \\$(KK$6$6$89$8q47$8%9
++-"-'
 ##/*.*@*@G&' 0 :s   Br*   r  r*   r1   r2   AiohttpTCPConnectorr    s!    +3 	AS 	A 	A 	Ar1   r  )limitT)r  	trust_envread_bufsize)ra  r^  google.auth.aio.credentialsr   "google.auth.aio.transport.sessionsr"   rf  r  _loopr  r   ClientSessionTCPConnectorr   )r   AsyncCredentialsr"   r  r  r  s         r2   _get_aiohttp_session"BaseApiClient._get_aiohttp_sessionu  s	    
 $)D)D)F)F!OM	9+; 	94 !77STX7Y Z$$$ &&((%  ''  &&0022	C!6!6 	CA 4 4 A$ 3'a0'd    [  s)   #D%5D B:D%
D"D%!D""D%rI   c                   ^ SmU R                   nU R                  nU(       a  UR                  T5      OU(       a  UR                  T5      OSnU(       dc  [        R                  " [
        R                  R                  S[        R                  " 5       5      [
        R                  R                  S5      S9nS[        [        R                     S[        R                  S[        R                  4U4S	 jjnU" X5      U" X#5      4$ )
zEnsures the SSL context is present in the HTTPX client args.

Creates a default SSL context if one is not provided.

Args:
  options: The http options to check for SSL context.

Returns:
  A tuple of sync/async httpx client args.
verifyNSSL_CERT_FILESSL_CERT_DIRcafilecapathargsctxr3   c                 R  > U =(       d    0 R                  5       n U R                  T5      (       d  XT'   SU ;  a  SU S'   U R                  5       nUR                  5        HC  nU[        R                  " [        R
                  R                  5      R                  ;  d  MA  X#	 ME     U$ )  Sets the SSL context in the client args if not set.

Does not override the SSL context if it is already set.

Args:
  args: The client args to to check for SSL context.
  ctx: The SSL context to set.

Returns:
  The client args with the SSL context included.
rx   N)r   r9   inspect	signaturer   Clientr   
parametersr  r  copied_argsrU   r  s       r2   
_maybe_set7BaseApiClient._ensure_httpx_ssl_ctx.<locals>._maybe_set  s     jb dXXfV	$	YIIKk!!##g''(=(=>III $ r1   )rw  rx  r9   rB  create_default_contextr7   r8   certifiwherer   r   
StringDict
SSLContext)rI   r  
async_argsr  r  r  s        @r2   rX  #BaseApiClient._ensure_httpx_ssl_ctx  s     FD**J  	   ^^F+  
 &&A/c
w))*^^ 
		8 	4:# r1   c                   ^ SmU R                   nU(       a  UR                  T5      OSnU(       dc  [        R                  " [        R
                  R                  S[        R                  " 5       5      [        R
                  R                  S5      S9nS[        [        R                     S[        R                  S[        R                  4U4S	 jjnU" X5      $ )
Ensures the SSL context is present in the async client args.

Creates a default SSL context if one is not provided.

Args:
  options: The http options to check for SSL context.

Returns:
  An async aiohttp ClientSession._request args.
rB  Nr  r  r  r  r  r3   c                 J  > U (       a  U R                  T5      (       d  U =(       d    0 R                  5       n XT'   U R                  5       nUR                  5        HC  nU[        R                  " [        R
                  R                  5      R                  ;  d  MA  X#	 ME     U$ )r  )r9   r   r  r  r   r  _requestr  r  s       r2   r  9BaseApiClient._ensure_aiohttp_ssl_ctx.<locals>._maybe_set$  s     &))
  "VIIKk!!##$$W%:%:%C%CDOOP  $ r1   rx  r9   rB  r  r7   r8   r  r  r   r   r  r  rI   r  r  r  r  s       @r2   rd  %BaseApiClient._ensure_aiohttp_ssl_ctx  s     F**J$.*..
 DC&&A/c
w))*^^ 
		8 j&&r1   c                   ^ SmU R                   nU(       a  UR                  T5      OSnU(       dc  [        R                  " [        R
                  R                  S[        R                  " 5       5      [        R
                  R                  S5      S9nS[        [        R                     S[        R                  S[        R                  4U4S	 jjnU" X5      $ )
r  rB  Nr  r  r  r  r  r3   c                 2  > U (       a  U R                  T5      (       d  U =(       d    0 R                  5       n XT'   U R                  5       nUR                  5        H7  nU[        R                  " [        5      R
                  ;  d  M-  US:w  d  M5  X#	 M9     U$ )r  rB  )r9   r   r  r  
ws_connectr  r  s       r2   r  ;BaseApiClient._ensure_websocket_ssl_ctx.<locals>._maybe_set^  sz     &))
  "VIIKk!!##g''
3>>>3%< $ r1   r  r  s       @r2   rh  'BaseApiClient._ensure_websocket_ssl_ctxB  s     F**J$.*..
 DC &&A/c
w))*^^ 
		2 j&&r1   c                     [         =(       aO    U R                  R                  =(       d    0 R                  S5      S L =(       a    U R                  R                  S L $ )N	transport)r   rN  rx  r9   r`  r   s    r2   rb  BaseApiClient._use_aiohttpy  sS     	 	<117R<<[I	< 22d:	r1   c                 &   U R                   =(       a    U R                  =(       d    U R                  nU R                  (       a  U(       d  U R                  $ [	        U R
                  R                  5      nUR                  SS9R                  5       $ )Nwss)scheme)	ri   r5  r4  rF  r   rN  r^   re   geturl)r   rv  	url_partss      r2   _websocket_base_url!BaseApiClient._websocket_base_url  sk    <<9DMMJdll$7 !!!++445IU+2244r1   c                 8   U R                      U R                  (       d2  [        U R                  S9u  U l        nU R                  (       d  Xl        U R                  (       a  [	        X R                  5      sSSS5        $ [        S5      e! , (       d  f       g= f)z/Retrieves the access token for the credentials.r?  N0Could not resolve API token from the environment)rQ  rM  rr   ri   get_token_from_credentialsRuntimeError)r   ri   s     r2   _access_tokenBaseApiClient._access_token  sl    			%.t||%D"7|| ,			)$0A0AB 
	 MNN 
	s   A)B B
Bc                   #    U R                   c  U R                  c  [        R                  " 5       U l        U R                   ISh  vN   U R                   c  [        R                  " 5       U l         SSS5      ISh  vN   U R                   $ U R                   $  NS N! , ISh  vN  (       d  f       U R                   $ = f7f)a7  Lazily initializes and returns an asyncio.Lock for async authentication.

This method ensures that a single `asyncio.Lock` instance is created and
shared among all asynchronous operations that require authentication,
preventing race conditions when accessing or refreshing credentials.

The lock is created on the first call to this method. An internal async lock
is used to protect the creation of the main authentication lock to ensure
it's a singleton within the client instance.

Returns:
    The asyncio.Lock instance for asynchronous authentication operations.
N)rR  rS  r-  rP  r   s    r2   _get_async_auth_lock"BaseApiClient._get_async_auth_lock  s      $		,	,	4.5lln+444  ("),,.$
 54    4   	 5444    sH   ACBC(B3C>B?CCC%B(&C2Cc                   #    U R                   (       d  U R                  5       I Sh  vN nU ISh  vN   U R                   (       dJ  [        R                  " [        U R
                  S9I Sh  vN u  U l         nU R
                  (       d  X l        SSS5      ISh  vN   U R                   (       a  [        U U R                   5      I Sh  vN $ [        S5      e N N Nq ND! , ISh  vN  (       d  f       NY= f N17f)z>Retrieves the access token for the credentials asynchronously.Nr?  r  )rM  r  r-  	to_threadrr   ri    async_get_token_from_credentialsr  )r   async_auth_lockri   s      r2   r  !BaseApiClient._async_access_token  s     "7799o    -4->->. (
$$
W "L ! 3



  
 KLL% : 
( !sr   %D C
D CD 8C$.C /#C$D C".D C>D D  C$"D $C;*C-+C;7D http_methodr_   request_dictc                    UR                  5        Vs/ s H  oUR                  S5      (       d  M  UPM     nnU H  nX5	 M     U(       aW  [        U[        5      (       a  [	        U R
                  U5      nO7[	        U R
                  [        R                  " U5      5      nOU R
                  nSnU R                  (       a  US:X  a  UR                  S5      (       a  SnU R                  (       a  UR                  S5      (       dx  U(       dq  U R                  (       d  U R                  (       aO  U R                  (       a  UR                  [        R                  :X  d   SU R                   SU R                   S3U-   nUR                  c  SU 3n	OUR                   SU 3n	UR                  b  UR                  (       d  [!        S	5      eUR                  n
[#        US
5      (       a1  UR$                  (       a   [&        R(                  " X7R$                  5        U
nU R                  (       a3  U R                  (       a  U R                  (       d  U R*                  (       a7  UR                  [        R                  :X  a  [-        X5      nOG[-        U
U	5      nO:U R                  (       a)  UR                  [        R                  :X  a  [-        X5      nU R*                  (       a+  U R*                  R                  S5      (       a  [/        S5      e[1        UR2                  5      nUR4                  c  [!        S5      e[7        UR4                  U5        [9        UUUR4                  UUS9$ s  snf )N_Fr9   zpublishers/Tz	projects/z/locations/ra   zBase URL must be set.
extra_bodyzauth_tokens/z4Ephemeral tokens can only be used with the live API.zRequest headers must be set.r   r~   r?   r   rx   )rN   rd   r   r   rW   rN  rJ  r3  ri   r5  rF  rL  r   
COLLECTIONrV  r^   ro   r}  r  r   recursive_dict_updater4  rh   r(   rz   rx   r?   r]   r|   )r   r  r_   r  r6  rU   keys_to_deletepatched_http_optionsquery_vertex_base_modelsversioned_pathr^   r~   rX   s                r2   _build_requestBaseApiClient._build_request  s    &2%6%6%8P%8cNN3<Oc%8NP

  	L+	.	.1 

  2 : :< H 
 "//$5 OOM**!%,,(\\T]]  $<<''( k$--BTId''/4&zn.::;1TFCn 	%%-#,,.//%..h 	$l33 ++##
77 C  LLT]]<< 
6
6%%& H+

 	 88M<T<TT()c||//??%
@  00D0L0LM##+566"$$&8 $,," w Qs
   M%M%http_requeststreamc           	         S nU R                   (       a  U R                  (       d  U R                  (       a  SU R                  5        3UR                  S'   U R
                  (       a>  U R
                  R                  (       a#  U R
                  R                  UR                  S'   UR                  (       a   [        R                  " UR                  5      OS nOpUR                  (       a_  [        UR                  [        5      (       d4  UR                  (       a   [        R                  " UR                  5      OS nOUR                  nU R                  5       (       Ga  [        UR                  5      nU R                  cI  [!        U R
                  SS9U l        ["        R$                  " 5       nU R                  R'                  U5        U R                  R(                  (       a1  SU;   a+  SU;   a  UR+                  SS	5      nOUR+                  SS
5      nU R                  R-                  UR.                  R1                  5       UUUR                  UR2                  US9nO^U R4                  R7                  UR.                  UR                  UUR                  UR2                  S9nU R4                  R9                  XrS9n[:        R<                  R?                  U5        [A        UR                  U(       a  U5      $ URB                  /5      $ )Nr  r  r  r   )max_refresh_attemptsgoogleapis.comsandboxsandbox.googleapis.commtls.sandbox.googleapis.commtls.googleapis.com)r   r~   r   r?   rx   r  r   r~   r   r?   rx   r  )"r3  ri   r5  r  r?   rM  r  r   r   dumpsr   r   r[  rZ   r~   rZ  r   r   default_client_cert_sourceconfigure_mtls_channel_is_mtlsreplacer  r   upperrx   r\  build_requestsendr   r   raise_for_responser   text)r   r  r  r   r~   client_cert_sourcer   httpx_requests           r2   _request_onceBaseApiClient._request_once4  st   
 )-D}}$,,$--078J8J8L7M.Nl?+			t00AA.. 	23 /;.?.?TZZ))*Td			,++U332>2C2CL--.$""$!!##  !c		!	!	)#4!"$
  "<<>  77	
 
	!	!	*	*/?3/F&(E# ,.CD#))11$$**,&&&& 2 h ((66$$&&&& 7 m ##(((Fh
OO&&x0f( 3;==/ r1   c                 *   U(       aq  [        U[        5      (       a  [        S0 UD6OUnUR                  (       a>  [	        UR                  5      n[
        R                  " S0 UD6nU" U R                  X5      $ U R                  U R                  X5      $ Nr*   )	r   r   r   rg  r  r   rj  r#  rk  r   r  r6  r  parameter_modelry  r   s          r2   r  BaseApiClient._requestq  s      d++ 
%
%  
	&	&!/"?"?@!!1L1T''>>;;t))<@@r1   c                   #    S nU R                   (       a  U R                  (       d  U R                  (       aw  SU R                  5       I S h  vN  3UR                  S'   U R
                  (       a>  U R
                  R                  (       a#  U R
                  R                  UR                  S'   UR                  (       a[  [        UR                  [        5      (       d0  [        R                  " UR                  5      R                  S5      nOUR                  nU(       Ga;  U R                  5       (       Ga  U R                  5       I S h  vN U l        UR                   nU R#                  5       (       a  [$        R&                  " 5       nU R                  R)                  U5      I S h  vN   U R                  R*                  (       a1  SU;   a+  SU;   a  UR-                  SS5      nOUR-                  SS	5      n U R                  R.                  " SUR0                  UUR                  U[2        R4                  " UR6                  S
9S.U R8                  D6I S h  vN n[V        RX                  R[                  U5      I S h  vN   []        US5      (       a  UR^                  n[a        UR                  U5      $ U Rb                  Re                  UR0                  UR                   UUR                  UR6                  S9nU Rb                  Rg                  UUS9I S h  vN n	[V        RX                  R[                  U	5      I S h  vN   [a        U	R                  U	5      $ U R                  5       (       Ga  U R                  5       I S h  vN U l        UR                   nU R#                  5       (       a  [$        R&                  " 5       nU R                  R)                  U5      I S h  vN   U R                  R*                  (       a1  SU;   a+  SU;   a  UR-                  SS5      nOUR-                  SS	5      n U R                  R.                  " SUR0                  UUR                  U[2        R4                  " UR6                  S
9S.U R8                  D6I S h  vN n[V        RX                  R[                  U5      I S h  vN   Un
[]        U
S5      (       a  U
R^                  n
[a        U
R                  U
Ri                  5       I S h  vN /5      $ U Rb                  R/                  UR0                  UR                   UR                  UUR6                  S9I S h  vN n	[V        RX                  R[                  U	5      I S h  vN   [a        U	R                  U	Rh                  /5      $  GNW GNZ GN GNQ! [2        R:                  [2        R<                  [2        R>                  [2        R@                  [B        RD                  4 a  n[F        RH                  " S[J        RL                  " SS5      -   5      I S h  vN    [N        RQ                  SU-  5        U RS                  U RT                  5      U l        U R                  5       I S h  vN  U l        U R                  R.                  " SUR0                  UUR                  U[2        R4                  " UR6                  S
9S.U R8                  D6I S h  vN  n S nAGNS nAff = f GN~ GN GN GN GN' GNy GNU GN! [2        R:                  [2        R<                  [2        R>                  [2        R@                  [B        RD                  4 Gaq  n[F        RH                  " S[J        RL                  " SS5      -   5      I S h  vN    [N        RQ                  SU-  5        U RS                  U RT                  5      U l        U R                  5       I S h  vN  U l        U R                  R.                  " SUR0                  UUR                  U[2        R4                  " UR6                  S
9S.U R8                  D6I S h  vN  n[V        RX                  R[                  U5      I S h  vN    Un[]        US5      (       a  UR^                  n[a        UR                  URi                  5       I S h  vN  /5      s S nA$ S nAff = f GN GNd7f)Nr  r  r  r   r  r  r  r  r  totalr  r   r   	   z!Retrying due to aiohttp error: %s	_responser  r  r   r~   r?   r   rx   r*   )5r3  ri   r5  r  r?   rM  r  r   r   r   r   r  encoderb  r  ra  r~   r^  r   r  r  r  r  r  r   r   ClientTimeoutrx   re  ClientConnectorErrorClientConnectorDNSErrorClientOSErrorServerDisconnectedErrorauth_exceptionsTransportErrorr-  sleeprandomrandintr:   rT  rd  rN  r   r   raise_for_async_responser}  r.  r   r_  r  r  r   )r   r  r  r   r~   r!  r   r   r"  client_responseunwrapped_responseunwrapped_retry_responses               r2   _async_request_once!BaseApiClient._async_request_once  s     !D }}$,,$--$22445
6 ?+ 
		t00AA.. 	23 ))511zz,++,33G<  					&*&?&?&A A&&((#>>@
%%<<    ""++0@C0GCKK*,Ic KK 02GHc	0088 !(("**++,2F2FG 77 (@ oo66x@@@8[)) ''(H,,h77 00>>&&   (( (( ? 
 !% 8 8 = = !> !
 
 oo66GGGO33_EE					&*&?&?&A A&&((#>>@
%%<<    ""++0@C0GCKK*,Ic KK 02GHc0	0088 !(("**++,2F2FG 77 ( 88B
B
B$,
'55!3!=!= ((1C1H1H1J+J*K J !% 8 8 @ @&&   (( (( !A !
 
 oo66GGGO33o6J6J5KLL{ 5 !B ((++!!++**
 	 a&..A"667
7
7
++9A=
> **4+=+=> 
1 )-(A(A(C"C"C$
0088 !(("**++,2F2FG 77  (	0 	A
 	H !B C ,K ((++!!++**
 	 a&..A"667
7
7
++9A=
> **4+=+=> 
1 )-(A(A(C"C"C$
0088 !(("**++,2F2FG 77  ( 88B
B
B*2
"-{;;'?'I'I$&..-224445 9	D
 	Hs  A
c$V#C?c$V&Ac$*V)+Ac$<AV/ V,V/  "c$\ Bc$\&c$\Ac$\	Ac$%\&Ac$7A\ \&\ =\>A\ \
\ A	c$c&c$=c!>&c$&c$)c$,V/ /A[= 1[81X42A[8ZA)[8,[/-[82c$8[==c$c$c$	c$c$\ \ \ Ac*1c^Ac,_/-A)ca'c>b?Acc		ccc$cc$!c$c                 Z  #    U(       ay  [        U[        5      (       a  [        S0 UD6OUnUR                  (       aF  [	        UR                  5      n[
        R                  " S0 UD6nU" U R                  X5      I S h  vN $ U R                  U R                  X5      I S h  vN $  N( N7fr&  )	r   r   r   rg  r  r   rl  r?  rm  r'  s          r2   _async_requestBaseApiClient._async_request,  s       d++ 
%
%  
	&	&!/"?"?@&&66433\JJJ""  ,   Ks$   A<B+>B'?#B+"B)#B+)B+c                     [        U R                  [        5      (       a  U R                  R                  5       nU$ U R                  nU$ r   )r   rN  r   
model_dump)r   copieds     r2   get_read_only_http_options(BaseApiClient.get_read_only_http_optionsA  sB    $$$i00!!,,.f M !!fMr1   c                     U R                  XX45      nU R                  XTSS9nUR                  (       a  UR                  S   OSn[        UR                  US9$ )NFr  r   r   r?   body)r
  r  r   SdkHttpResponser?   )r   r  r_   r  r6  r  r   response_bodys           r2   r  BaseApiClient.requestH  s_     &&<L }}\}FH'/'?'?  #R  8#3#3-HHr1   c              #   (  #    U R                  XX45      nU R                  XTSS9nUR                  5        H  n[        R                  " U5      n UR                  S5      (       aV  [        R                  " U5      n	[        R                  R                  U	R                  S0 5      R                  S5      U	U5        [        UR                   US9v   M     g ! [        R                  R                   a    [        R                  SU-  5         NSf = f7f)NTr  	{"error":errorr   1Failed to decode chunk that contains an error: %srJ  )r
  r  r   r   r  rd   r   r   r   raise_errorr9   decoderr   r:   debugrL  r?   )
r   r  r_   r  r6  r  session_responser   
chunk_dump
chunk_jsons
             r2   request_streamedBaseApiClient.request_streamedX  s      &&<L }}\}M!**,::e$j  --zz*-*
//
%
%nnWb)--f5 $4$<$<:NN - \\)) ?*L	
 		s+   ADA,C;D6DDDDc                    #    U R                  XX45      nU R                  XTSS9I S h  vN nUR                  (       a  UR                  S   OSn[        UR                  US9$  N:7f)NFr  r6  r  r   r   rJ  )r
  rB  r   rL  r?   )r   r  r_   r  r6  r  resultrM  s           r2   async_requestBaseApiClient.async_requestu  so      &&<L &&!U '  F 281G1GF**1-RM6>>FF	s   &A%A#;A%c                 ~   ^#    U R                  XX45      nU R                  XTSS9I S h  vN mU4S jnU" 5       $  N7f)NTr\  c                   >#    T  S h  vN n [         R                  " U 5      n UR                  S5      (       a^  [         R                  " U5      n[        R
                  R                  UR                  S0 5      R                  S5      UT5      I S h  vN   [        TR                  US97v   M   N N! [         R                  R                   a    [        R                  SU-  5         NUf = f
 g 7f)NrP  rQ  r   rR  rJ  )r   r  rd   r   r   r   raise_error_asyncr9   rT  r   r:   rU  rL  r?   )r   rW  rX  r   s      r2   async_generator=BaseApiClient.async_request_streamed.<locals>.async_generator  s     ! IZZ&
	"";//J/J//33w+//7   h&6&6ZHHI

 ||++ 	
,,AJN 		 "sU   C5C3B3C3C5A/B7B5B7C53C35B776C0-C5/C00C5)r
  rB  )r   r  r_   r  r6  r  rc  r   s          @r2   async_request_streamed$BaseApiClient.async_request_streamed  sT      &&<L ((!T )  HI$ -s   &=;=r6  	file_path
upload_urlupload_sizec                    [        U[        R                  5      (       a  U R                  XX4S9$ [	        US5       nU R                  XRX4S9sSSS5        $ ! , (       d  f       g= f)a  Transfers a file to the given URL.

Args:
  file_path: The full path to the file or a file like object inherited from
    io.BytesIO. If the local file path is not found, an error will be
    raised.
  upload_url: The URL to upload the file to.
  upload_size: The size of file content to be uploaded, this will have to
    match the size requested in the resumable upload request.
  http_options: The http options to use for the request.

returns:
      The HttpResponse object from the finalize request.
rg  rbN)r   ioIOBase
_upload_fdopen)r   rh  ri  rj  r6  files         r2   upload_fileBaseApiClient.upload_file  s`    , )RYY''__
    	4 Dk  
 !  s   A
A$rq  c                   SnU(       a  UOU R                   n[        U[        5      (       a  UR                  S5      O[	        USS5      nU(       aC  [        U5      n[        U5      n[        UR                  UR                  UR                  S95      n UR                  [        5      n	Sn
U	(       a  [        U	5      n
SnX-   U:  a  US-  n[        U[        5      (       a  UR                  S5      OUR                  nUcK  [        U R                   [        5      (       a  U R                   R                  OU R                   R                  n[        U5      n[        U[        5      (       a  UR                  S0 5      O[	        US0 5      =(       d    0 nU(       a  [        U5      O0 nUR                  U[!        U5      [!        U
5      S	.5        [#        X5        SnU[$        :  au  U R&                  R)                  S
UUU	US9nUR*                  R                  S5      (       a  O7[,        [.        U-  -  nUS-  n[0        R2                  " U5        U[$        :  a  Mu  XZ-  nWR*                  R                  S5      S:w  a  OX5::  a  [5        S5      eGM  [6        R8                  R;                  U5        UR*                  R                  S5      S:w  a  [5        S5      e[=        UR*                  UR>                  /S9$ )a  Transfers a file to the given URL.

Args:
  file: A file like object inherited from io.BytesIO.
  upload_url: The URL to upload the file to.
  upload_size: The size of file content to be uploaded, this will have to
    match the size requested in the resumable upload request.
  http_options: The http options to use for the request.

returns:
      The HttpResponse object from the finalize request.
r   r^   Nr  netlocupload
, finalizerx   r?   zX-Goog-Upload-CommandzX-Goog-Upload-OffsetzContent-LengthPOSTr/  x-goog-upload-statusr   activeFAll content has been uploaded, but the upload status is not finalized.final6Failed to upload file: Upload status is not finalized.r   ) rN  r   r   r9   rO   r   r   re   r  rv  read
CHUNK_SIZEr   rx   rz   updaterZ   r]   MAX_RETRY_COUNTr\  r  r?   INITIAL_RETRY_DELAYDELAY_MULTIPLIERtimer8  ro   r   r   r  r   r   )r   rq  ri  rj  r6  offsetr^   rf   parsed_upload
file_chunk
chunk_sizeupload_commandrx   rX   user_headersupload_headersretry_countr   delay_secondss                      r2   ro  BaseApiClient._upload_fd  s   ( F#/<T5G5GL lD)) 	$\:t4 
 X&kz*m

 
  ''0B0B ! j 99Z(jj	_
n			+,& d++ 

9
%## 
 
 $,,d33 &&##++ 	
 2': d++ 

9b
)i4: 
 .:tL)rn#1"%f+
O 	
 %^Hk/)%%--"& . 
  677
+/?/LMq

=! /) f					4	5	A		
 	
k r OO&&x023w>OPP((8==/JJr1   c                   U R                  SU0 US9nSnUR                  (       aL  [        UR                  [        5      (       d!  [        R
                  " UR                  5      nOUR                  nU R                  R                  UR                  UR                  UR                  UUR                  S9n[        R                  R                  U5        [        UR                  UR!                  5       /S9R"                  S   $ )Downloads the file data.

Args:
  path: The request path with query params.
  http_options: The http options to use for the request.

returns:
      The file bytes
r9   r_   r  r6  Nr/  r   r   )r
  r   r   r   r   r  r\  r  r   r~   r?   rx   r   r   r  r   r  r   )r   r_   r6  r  r   r   s         r2   download_fileBaseApiClient.download_file,  s     &&Dr ' L )-D))511zz,++,  !!))""$$$$ * H OO&&x0x}}&7k! r1   c                  #    [        U[        R                  5      (       a  U R                  XX4S9I Sh  vN $ [        R
                  " U5      nUR                  S5      I Sh  vN nU ISh  vN   U R                  XbX4S9I Sh  vN sSSS5      ISh  vN   $  Ng N: N1 N N! , ISh  vN  (       d  f       g= f7f)a  Transfers a file asynchronously to the given URL.

Args:
  file_path: The full path to the file. If the local file path is not found,
    an error will be raised.
  upload_url: The URL to upload the file to.
  upload_size: The size of file content to be uploaded, this will have to
    match the size requested in the resumable upload request.
  http_options: The http options to use for the request.

returns:
      The HttpResponse object from the finalize request.
rg  Nrl  )r   rm  rn  _async_upload_fdanyioPathrp  )r   rh  ri  rj  r6  rq  fds          r2   async_upload_fileBaseApiClient.async_upload_fileS  s     * )RYY''((
 )    ZZ	"d4 b**K + 
 
 
 !
 s{   3CB.C$B%
C/B!0C3B'B#B'CB%CC!C#B'%C'B>-B0.B>:Cc          
      0  #    SnU(       a  UOU R                   n[        U[        5      (       a  UR                  S5      O[	        USS5      nU(       aC  [        U5      n[        U5      n[        UR                  UR                  UR                  S95      nU R                  5       (       Ga  U R                  5       I Sh  vN U l         [        U[        R                  5      (       a  UR                  [         5      n	OUR                  [         5      I Sh  vN n	Sn
U	(       a  [#        U	5      n
SnX-   U:  a  US-  n[        U[        5      (       a  UR                  S5      OUR$                  nUcK  [        U R                   [        5      (       a  U R                   R$                  OU R                   R$                  n['        U5      n[        U[        5      (       a  UR                  S0 5      O[	        US0 5      =(       d    0 nU(       a  [        U5      O0 nUR)                  U[+        U5      [+        U
5      S	.5        [-        X5        SnSnU[.        :  a  U R                  R1                  S
UU	U[2        R4                  " US9S9I Sh  vN nUR6                  R                  S5      (       a  O?[8        [:        U-  -  nUS-  n[<        R>                  " U5      I Sh  vN   U[.        :  a  M  XZ-  nUb   UR6                  R                  S5      S:w  a  OX5::  a  [A        S5      eGMb  [B        RD                  RG                  U5      I Sh  vN   Ub*  UR6                  R                  S5      S:w  a  [A        S5      e[I        UR6                  URK                  5       I Sh  vN /S9$  [        U[        R                  5      (       a  UR                  [         5      n	OUR                  [         5      I Sh  vN n	Sn
U	(       a  [#        U	5      n
SnX-   U:  a  US-  n[        U[        5      (       a  UR                  S5      OUR$                  nUcK  [        U R                   [        5      (       a  U R                   R$                  OU R                   R$                  n['        U5      n[        U[        5      (       a  UR                  S0 5      O[	        US0 5      =(       d    0 nU(       a  [        U5      O0 nUR)                  U[+        U5      [+        U
5      S	.5        [-        X5        SnSnU[.        :  a  U RL                  R1                  S
UU	UUS9I Sh  vN nUb2  UR6                  (       a!  UR6                  R                  S5      (       a  O7[8        [:        U-  -  nUS-  n[N        R>                  " U5        U[.        :  a  M  XZ-  nUb   UR6                  R                  S5      S:w  a  OX5::  a  [A        S5      eGM\  [B        RD                  RG                  U5      I Sh  vN   Ub*  UR6                  R                  S5      S:w  a  [A        S5      e[I        UR6                  URJ                  /S9$  GN GNg GN GN GN9 GN GN GN3 Nh7f)a  Transfers a file asynchronously to the given URL.

Args:
  file: A file like object inherited from io.BytesIO.
  upload_url: The URL to upload the file to.
  upload_size: The size of file content to be uploaded, this will have to
    match the size requested in the resumable upload request.
  http_options: The http options to use for the request.

returns:
      The HttpResponse object from the finalized request.
r   r^   Nru  rw  rx  rx   r?   ry  rz  r+  )r   r~   r   r?   rx   zX-Goog-Upload-Statusr   r|  r}  r~  r  r  r  r{  )(rN  r   r   r9   rO   r   r   re   r  rv  rb  r  ra  rm  rn  r  r  r   rx   rz   r  rZ   r]   r  r  r   r1  r?   r  r  r-  r8  ro   r   r   r;  r   r   r_  r  )r   rq  ri  rj  r6  r  r^   rf   r  r  r  r  rx   rX   r  r  r  r   r  r<  s                       r2   r  BaseApiClient._async_upload_fdt  s;    ( F#/<T5G5GL lD)) 	$\:t4 
 X&kz*m

 
  ''0B0B ! j $($=$=$??ddBII&&yy,*!YYz22*
:*!+-
L
(. <.. Y'&& 	
 ? D..55   ((%%-- 
 4G< ,-- Y+,	26<" 	
 0<l+%3$'K!*o
 	
 	'~JO+0088$++2DE 9  ( !!"899-1A;1NO-

+m,
,
, O+ 	   $$%;<H
   H OO44X>>>

""#9:gED
 	
 


8==?-B,C 
 dBII&&yy,*!YYz22*
:*!+-
L
(. ,-- Y'%% 	
 ? D..55   ((%%-- 
 4G< ,-- Y+,	26<" 	
 0<l+%3$'K!*o
 	
 	'~JO+"&":":"B"B $( #C # / )!))!))--.DEE-1A;1NO-

+
**]
#! O+$ 	'''++,BCxO
  E N OO44_EEE

%%%))*@AWLD
 	
 

!
!O4H4H3I C @
 3J - ? .C 3J< Fs   B>Z Y<AZY?E5ZZAZ"Z#Z3AZZAZ$Z
%AZ:Z;E#ZZA.ZAZ+Z,AZ?ZZZZZZZZc          
      N  #    U R                  SU0 US9nSnUR                  (       a[  [        UR                  [        5      (       d0  [        R
                  " UR                  5      R                  S5      nOUR                  nU R                  5       (       a  U R                  5       I Sh  vN U l	        U R                  R                  UR                  UR                  UR                  U[        R                  " UR                   S9S9I Sh  vN n["        R$                  R'                  U5      I Sh  vN   [)        UR                  UR+                  5       I Sh  vN /S9R,                  S   $ U R.                  R                  UR                  UR                  UR                  UUR                   S	9I Sh  vN n["        R$                  R'                  U5      I Sh  vN   [)        UR                  UR+                  5       /S9R,                  S   $  GNl GN N N Ne N@7f)
r  r9   r  Nr   r+  r  r  r   r/  )r
  r   r   r   r   r  r0  rb  r  ra  r  r   r~   r?   r   r1  rx   r   r   r;  r   r  r   r_  )r   r_   r6  r  r   r   r<  s          r2   async_download_file!BaseApiClient.async_download_file?  s     &&Dr ' L !D))511zz,++,33G<  $($=$=$??d,,44$$&&''l.B.BC 5  h OO44X>>>


x}})>(?A 
 #66>>$$&&&& ?  o OO44_EEE

!
!0D0D0F/GA 1 @ ? *? Fsn   B)H%+H,A#H%H&H%6H7'H%H
AH%;H!<&H%"H##5H%H%H%H%!H%#H%response_modelc                     g r   r*   )r   r  s     r2   _verify_responseBaseApiClient._verify_responsey  s    r1   c                     U R                   R                  (       d+  U R                  (       a  U R                  R                  5         U R                  (       a  U R                  R                  5         gg)zCloses the API client.N)rN  r]  r\  r!  rZ  r   s    r2   r!  BaseApiClient.close|  sM     **t/A/A
 
$$&  r1   c                 .  #    U R                   R                  (       d"  U R                  R                  5       I Sh  vN   U R                  (       a?  U R                   R
                  (       d#  U R                  R                  5       I Sh  vN   ggg NU N	7f)zCloses the API async client.N)rN  r`  r_  r   ra  rc  r!  r   s    r2   r   BaseApiClient.aclose  sn      00$$++---T%7%7%F%F!!''))) &G .)s"   9BBAB	B
BBc                     U R                   R                  (       d  U R                  5          [        R
                  " 5       R                  U R                  5       5        g! [         a     N@f = f! [         a     gf = f)zCloses the API client when the object is garbage collected.

ADK uses this client so cannot rely on the genai.[Async]Client.__del__
for cleanup.
N)rN  r]  r!  r   r-  r.  r/  r   r   s    r2   r"  BaseApiClient.__del__  sh    ,,

 ,,T[[];	  

  
s"   +A! 2A1 !
A.-A.1
A>=A>)ra  rR  rS  re  r_  rY  rm  rZ  rM  rN  r\  rk  rQ  ri  r4  rF  r5  ri   r3  )NNNNNNr   )Fr  r$  )Dr+   r,   r-   r.   r/   r   r~  rZ   rl   rm   rp   r   r   r   r[  r^  r   r  staticmethodr   r
   r   r  rX  rd  rh  rb  r  r  r-  rP  r  r   r  r   r   r|   r
  r   r#  r  r?  rB  rG  rL  r  r   rY  r^  re  rm  rn  intrr  ro  r   r  r  r  	AsyncFiler  r  r   r  r!  r   r"  r0   r*   r1   r2   r2  r2  Z  s   @ "&#CG# $26@?@? }@? FKK33??@	@?
 }@? @? ./@?D
T 
d S!>?S!j AAW!3!334A AF 1'{ 1'w7I7I 1' 1'f 4' 4'9K9K 4' 4'lD 53 5OS O!',, !2MsCx M8 37ii i f%	i
 ./i i\ ;; ; 	;@ 37	AA ./A 	A
 A* 7<eM%eM/3eMeMT 37	 ./ 	
 *'*<*<  37II I f%	I
 ./I I* 37OO O f%	O
 ./O $,-OD 37GG G f%	G
 ./G G, 37!! ! f%	!
 ./! !R 37
sBII~&
 
 	
 ./
 
L 37aKIIaK aK 	aK ./aK aKN 37	%% ./	%
 S%Z%Z 37
sBII~&
 
 	
 ./
 
N 37I"))U__S112I I 	I ./I I^ 37	55 ./	5
 S%Z5t	W->-> 	4 	'*r1   r2  r  c                     UR                   (       d  UR                  (       d  [        U5        UR                  (       d  [        S5      eUR                  $ )=Refreshes the authentication token for the given credentials.r  )r  r  rw   r  )r  rp   s     r2   r  r    s>    
  1 1			
I
JJ			r1   c                   #    UR                   (       d  UR                  (       dx  U R                  5       I Sh  vN nU ISh  vN   UR                   (       d  UR                  (       d#  [        R                  " [
        U5      I Sh  vN   SSS5      ISh  vN   UR                  (       d  [        S5      eUR                  $  N N N@ N2! , ISh  vN  (       d  f       NG= f7f)r  Nr  )r  r  r  r-  r  rw   r  )r  rp   r  s      r2   r  r    s     
  1 1"7799O			K$5$5k::: 
 
		
I
JJ			 : 	; sj   6C'C
C'CC'ACC		CC'C-C'C'	CC'C$CC$ C')qr/   r-  collections.abcr   r   dataclassesr   r  rm  r   r  r[   r7   r9  rB  rC   rO  r  typingr   r   r   r   r	   r
   r   urllib.parser   r   rH  r  r  google.authrl   google.auth.credentialsr   google.auth.transportr   ru   r   r   r6  r   pydanticr   r   r   requests.structuresr   r   r   r   r   r   typesr   r   r   rL  r   r   websockets.asyncio.clientr   r  ModuleNotFoundErrorwebsockets.clientr   r   rf  	multidictr    r  r"   	getLoggerr:   r  r   r  r  r  rU  ro   r(   rZ   r>   r   rH   rW   r   r  r]   rh   rr   rw   rz   r|   r  r  r
  r  r  r  r  r  r  r  AsyncClientr)  r2  rm   rp   r  r  r*   r1   r2   <module>r     s  "
  %  !  	    	  
 
   V V V ! #      / & < 5   $  3      $ 2 #  6= +
 (G 
		5	6
   !4L 2
 2:# :"8DcN 8t 8@)48E#s(^E19%s
:K1LE	EGC Gs Gs G%T	* u[#5E/F "k k 
eE3J'(
e_
 " " "C CT     !12  w7I7I  Fell *u(( (D DN2

((44
 	
((44 	SC  6556  s$   I! %I4 !I10I14I>=I>