
    Xh                    v   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
 d dlmZmZmZ d dlmZ d dl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mZ dd
lm Z m!Z!m"Z" ddl#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z, ddl-m.Z.m/Z/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5m6Z6m7Z7 ddl8m9Z9m:Z: ddl'm;Z;m<Z<m=Z= ddl>m?Z?m@Z@  eeA      5  d dlBZBddd        eeA      5  d dlCZDddd       ej                  dk\  r ede:e:      ZFn
 ede:      ZFer?d dlGmHZH  G d ded      ZI G d deeF   ed      ZJ G d  d!ed      ZK G d" d#eKd      ZLn G d$ d%      ZM eM       ZHeNeOeOf   ZIeZJeecZKZLed&   ZPed'   ZQd1d(ZRd2d3d)ZSd2d4d*ZT G d+ d,eeF         ZU G d- d.eUeF         ZV G d/ d0eUeF         ZWy# 1 sw Y   xY w# 1 sw Y   xY w)5    )annotationsN)ThreadPoolExecutor)asynccontextmanagercontextmanagersuppress)BytesIO)	TYPE_CHECKINGCallableGenericLiteralOptional	TypedDictTypeVarUnioncast)urlparse   )	AsyncCurl)CurlHttpVersionCurlInfoCurlOpt)Curl	CurlErrorCurlMime)CurlCffiWarning   )CookiesCookieTypes
CurlMorsel)RequestExceptionSessionClosed
code2error)HeadersHeaderTypes)BrowserTypeLiteralExtraFingerprintsExtraFpDict)
STREAM_ENDResponse)not_setset_curl_optionsHttpVersionLiteral)AsyncWebSocket	WebSocket)      R)bounddefault)r2   )Unpackc                  @    e Zd ZU ded<   ded<   ded<   ded<   ded<   y)	ProxySpecstrallhttphttpswswssN__name__
__module____qualname____annotations__     R/home/Script/Script_env/lib/python3.12/site-packages/curl_cffi/requests/session.pyr6   r6   4   s    	
rC   r6   F)totalc                     e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   ded<   d	ed<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   d	ed<   d	ed<   ded<   ded<   ded<   ded <   d!ed"<   d#ed$<   ded%<   d	ed&<   d'ed(<   d)ed*<   ded+<   y,)-BaseSessionParamsOptional[HeaderTypes]headersOptional[CookieTypes]cookiesOptional[tuple[str, str]]authOptional[ProxySpec]proxiesOptional[str]proxy
proxy_authbase_urlOptional[dict]paramsboolverify!Union[float, tuple[float, float]]timeout	trust_envallow_redirectsintmax_redirectsOptional[BrowserTypeLiteral]impersonateja3akamai/Optional[Union[ExtraFingerprints, ExtraFpDict]]extra_fpdefault_headers"Union[str, Callable[[bytes], str]]default_encodingcurl_optionsOptional[list]
curl_infos4Optional[Union[CurlHttpVersion, HttpVersionLiteral]]http_versiondebug	interface%Optional[Union[str, tuple[str, str]]]certOptional[type[R]]response_classdiscard_cookiesNr=   rB   rC   rD   rG   rG   ;   s    &&&&''$$--2211AA<<$$""JJ  33))rC   rG   c                  :   e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded <   ded!<   ded"<   d#ed$<   ded%<   d&ed'<   d(ed)<   d*ed+<   ded,<   d-ed.<   d/ed0<   d1ed2<   d3ed4<   y5)6StreamRequestParams"Optional[Union[dict, list, tuple]]rU   AOptional[Union[dict[str, str], list[tuple], str, BytesIO, bytes]]dataOptional[dict | list]jsonrH   rI   rJ   rK   rT   filesrL   rM   3Optional[Union[float, tuple[float, float], object]]rY   Optional[bool]r[   Optional[int]r]   rN   rO   rP   rQ   rR   rW   refereraccept_encodingOptional[Callable]content_callbackr^   r_   r`   ra   rb   rc   rd   re   rf   Union[str, Literal[False]]quoterj   rk   rm   rn   ro   r\   max_recv_speedOptional[CurlMime]	multipartrV   rr   Nr=   rB   rC   rD   rt   rt   X   s    22OO##&&&&''DD''$$$$--&&,,11AA''<<))JJ  33%%rC   rt   c                      e Zd ZU ded<   y)RequestParamsr|   streamNr=   rB   rC   rD   r   r   x   s    rC   r   c                      e Zd Zed        Zy)_Unpackc                      y NrB   )argskwargss     rD   __getitem__z_Unpack.__getitem__~   s    rC   N)r>   r?   r@   staticmethodr   rB   rC   rD   r   r   }   s    		 
	rC   r   )eventletgevent)	GETPOSTPUTDELETEOPTIONSHEADTRACEPATCHQUERYc                ^    t        |       }t        |j                  xr |j                        S )z,Check if the provided url is an absolute url)r   rV   schemehostname)url
parsed_urls     rD   _is_absolute_urlr      s'    #J
!!9j&9&9::rC   c                D    	 | j                   d   S # t        $ r |cY S w xY wNr   )queue
IndexErrorqr3   s     rD   _peek_queuer      s)    wwqz     c                D    	 | j                   d   S # t        $ r |cY S w xY wr   )_queuer   r   s     rD   _peek_aio_queuer      s)    xx{ r   c                     e Zd ZdZdddddddddddddddddddddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 dd	Zd
 Zedd       Zej                  dd       Zy)BaseSessionzMProvide common methods for setting curl options and reading info in sessions.NT   utf-8F)rI   rK   rM   rO   rQ   rR   rS   rU   rW   rY   rZ   r[   r]   r_   r`   ra   rc   rd   rf   rg   ri   rk   rl   rm   ro   rq   rr   c                  t        |      | _        t        |      | _        || _        || _        || _        |	| _        |
| _        || _	        || _
        || _        || _        || _        || _        || _        || _        || _        |xs i | _        |xs g | _        || _        || _        || _        || _        |!t1        |t2              du rt5        d| d      |xs t2        | _        || _        |r|rt5        d      |rd|i}|xs i | _        || _        | j
                  r t?        | j
                        stA        d      d| _!        y )NFzZ`response_class` must be a subclass of `curl_cffi.requests.models.Response`, not of type ``z)Cannot specify both 'proxy' and 'proxies'r8   z2You need to provide an absolute url for 'base_url')"r#   rI   r   _cookiesrM   rS   rU   rW   rY   rZ   r[   r]   r_   r`   ra   rc   rd   rf   rg   ri   rk   rl   rm   ro   
issubclassr)   	TypeErrorrq   rr   rO   rR   r   
ValueError_closed)selfrI   rK   rM   rO   rQ   rR   rS   rU   rW   rY   rZ   r[   r]   r_   r`   ra   rc   rd   rf   rg   ri   rk   rl   rm   ro   rq   rr   s                               rD   __init__zBaseSession.__init__   sY   > w'(	 ".*& . 0(.B$*(
"	%*^X*NRW*W  ./q2 
 -8.WGHHenG")-R$==!1$--!@QRRrC   c                
   |}t        t        | j                  |            }t        t        |j	                  t
        j                              j                         |_        |r|j                         |_
        t        t        |j	                  t
        j                              |_        t        t        |j	                  t
        j                              |_        d|j                  cxk  xr dk  nc |_        |j                         j#                         }g }	|D ]  }
|
j%                         s|
j'                  d      r'|j)                  |
      j                         |_        g }	L|
j'                  d      s|
j'                  d      r|	dxx   |
z  cc<   ||	j-                  |
        t/        |	      |_        t3               |_        |j0                  j7                  d      }|D ]  }	 t8        j4                  j;                         }|j=                  |       |j?                         D ]g  \  }}|j4                  jA                  ||jB                  |jE                  dd	      |jE                  d
d      tG        |jE                  d                   i  |xs | jJ                  }|sZ|j	                  t
        jL                        D cg c]  }tO        jP                  |       }}| jR                  jU                  |       t        t        j	                  t
        jV                              j                         |_,        t        t        |j	                  t
        jZ                              |_.        t        t        |j	                  t
        j^                              j                         |_0        t        t        |j	                  t
        jb                              |_2        ||_3        t        th        |j	                  t
        jj                              |_6        t        t        |j	                  t
        jn                              |_8        t        t        |j	                  t
        jr                              }	 |j                         |_:        | jx                  D ]   }|j	                  |      |jz                  |<   " |S # tH        $ r Y w xY wc c}w # tv        $ r |j                  d      |_:        Y gw xY w)N   i  s   HTTP/       	z
set-cookiedomain path/secure)r   r   r   zlatin-1)>r   r1   rq   bytesgetinfor   EFFECTIVE_URLdecoder   getvaluecontentr\   HTTP_VERSIONrk   RESPONSE_CODEstatus_codeok
splitlinesstrip
startswithget_reason_phrasereasonappendr#   rI   r   rK   get_listr9   SimpleCookieloaditemssetvaluegetrV   	Exceptionrr   
COOKIELISTr   from_curl_formatr   update_cookies_from_curl
PRIMARY_IP
primary_ipPRIMARY_PORTprimary_portLOCAL_IPlocal_ip
LOCAL_PORT
local_portrf   float
TOTAL_TIMEelapsedREDIRECT_COUNTredirect_countREDIRECT_URLredirect_urlUnicodeDecodeErrorri   infos)r   curlbufferheader_bufferrf   rr   crspheader_linesheader_listheader_lineset_cookie_headers
set_cookiecookienamemorselmorselsredirect_url_bytesinfos                      rD   _parse_responsezBaseSession._parse_response   s    1d))!,-uaii(>(>?@GGI //+CKQYYx/D/D%EFsAIIh.D.D$EF-#-$--/::< $&' 	,K$$&%%h/00=DDF
 %%d+{/E/Ee/LB;.{+	, k* i [[11,?, 	J224J'$*LLN LD&KKOO%zz(B7#ZZ4#FJJx$89 $ 		  *AT-A-A89		(BUBU8V34
++A.G  MM227;eQYYx/B/B%CDKKMQYYx/D/D%EFE199X->->#?@GGIc199X-@-@#AB/5!))H,?,?"@A!#qyy1H1H'IJ!%83H3H)IJ	D188:C
 OO 	.DiioCIIdO	. 
9   " 	D188CC	Ds+   -B)SSS 	SSS>=S>c                2    | j                   rt        d      y )Nz'Session is closed, cannot send request.)r   r!   r   s    rD   _check_session_closedz!BaseSession._check_session_closed;  s    << IJJ rC   c                    | j                   S r   )r   r  s    rD   rK   zBaseSession.cookies?  s    }}rC   c                $    t        |      | _        y r   )r   r   )r   rK   s     rD   rK   zBaseSession.cookiesC  s      (rC   )6rI   rH   rK   rJ   rM   rL   rO   rN   rQ   rP   rR   rL   rS   rP   rU   rT   rW   rV   rY   rX   rZ   rV   r[   rV   r]   r\   r_   r^   r`   rP   ra   rP   rc   rb   rd   rV   rf   re   rg   rT   ri   rh   rk   rj   rl   rV   rm   rP   ro   rn   rq   rp   rr   rV   )returnr1   )r  r   )rK   r   r  None)
r>   r?   r@   __doc__r   r  r  propertyrK   setterrB   rC   rD   r   r      s   W
 *.)-*.'+#04"&!%57 $48! $DH $?F'+%)MQ#'6:,0 %;I 'I '	I
 (I %I I .I  I I I 3I I I I  2!I" #I$ %I& B'I( )I* =+I, %-I. #/I0 K1I2 3I4 !5I6 47I8 *9I: ;IVI	
IVK   ^^) )rC   r   c                      e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 d fdZed        Zed        Zd Zd Z	ddZ
e	 	 	 	 	 	 dd	       Z	 d	 d d
Zd!dZdddddddedddddddddddddddddddddddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 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Zd#dZ xZS )$SessionzA request session, cookies and connections will be reused. This object is
    thread-safe, but it's recommended to use a separate session for each thread.Nc                `   t        |   di | || _        || _        d| _        d| _        |rat        j                         | _        |rd| _	        || j                  _
        yd| _	        t        | j                        | j                  _
        y|r|nt        | j                        | _        y)a	  
        Parameters set in the ``__init__`` method will be overriden by the same
        parameter in request method.

        Args:
            curl: curl object to use in the session. If not provided, a new one will be
                created. Also, a fresh curl object will always be created when accessed
                from another thread.
            thread: thread engine to use for working with other thread implementations.
                choices: eventlet, gevent.
            headers: headers to use in the session.
            cookies: cookies to add in the session.
            auth: HTTP basic auth, a tuple of (username, password), only basic auth is
                supported.
            proxies: dict of proxies to use, prefer to use proxy if they are the same.
                format: ``{"http": proxy_url, "https": proxy_url}``.
            proxy: proxy to use, format: "http://proxy_url".
                Cannot be used with the above parameter.
            proxy_auth: HTTP basic auth for proxy, a tuple of (username, password).
            base_url: absolute url to use as base for relative urls.
            params: query string for the session.
            verify: whether to verify https certs.
            timeout: how many seconds to wait before giving up.
            trust_env: use http_proxy/https_proxy and other environments, default True.
            allow_redirects: whether to allow redirection.
            max_redirects: max redirect counts, default 30, use -1 for unlimited.
            impersonate: which browser version to impersonate in the session.
            ja3: ja3 string to impersonate in the session.
            akamai: akamai string to impersonate in the session.
            extra_fp: extra fingerprints options, in complement to ja3 and akamai str.
            interface: which interface use.
            default_encoding: encoding for decoding response content if charset is not
                found in headers. Defaults to "utf-8". Can be set to a callable for
                automatic detection.
            cert: a tuple of (cert, key) filenames for client cert.
            response_class: A customized subtype of ``Response`` to use.

        Notes:
            This class can be used as a context manager.

        .. code-block:: python

            from curl_cffi.requests import Session

            with Session() as s:
                r = s.get("https://example.com")
        NTFrl   rB   )superr   _thread_use_thread_local_curlr   	_executor	threadinglocal_local_is_customized_curlr   r   rl   _curl)r   r   threaduse_thread_local_curlr   	__class__s        rD   r   zSession.__init__M  s    l 	"6"&;# #//+DK+/(#' +0(#'djj#9 !%4djj+ADJrC   c                &   | j                   rz| j                  rt        j                  dt        d       t        | j                  dd       s%t        | j                        | j                  _	        | j                  j                  S | j                  S )Nz/Creating fresh curl handle in different thread.r   )
stacklevelr   r  )r  r  warningswarnr   getattrr  r   rl   r   r  r  s    rD   r   zSession.curl  sj    &&''E# 
 4;;5#'djj#9 ;;###::rC   c                P    | j                   t               | _         | j                   S r   )r  r   r  s    rD   executorzSession.executor  s     >>!/1DN~~rC   c                    | S r   rB   r  s    rD   	__enter__zSession.__enter__  s    rC   c                $    | j                          y r   closer   r   s     rD   __exit__zSession.__exit__  s    

rC   c                F    d| _         | j                  j                          y)Close the session.TN)r   r   r'  r  s    rD   r'  zSession.close  s    		rC   c              +     K    | j                   d||d|ddi}	 | |j                          y# |j                          w xY ww)z6Equivalent to ``with request(..., stream=True) as r:``methodr   r   TNrB   )requestr'  r   r.  r   r   r   s        rD   r   zSession.stream  sC      dllI&cIVIDI	IIIKCIIKs   A2 AAAc                    | j                          | j                  j                         }| j                  j                          t	        |||||      } |j
                  |fi | |S )a"  Connects to a websocket url.

        Note: This method is deprecated, use WebSocket instead.

        Args:
            url: the ws url to connect.
            on_message: message callback, ``def on_message(ws, str)``
            on_error: error callback, ``def on_error(ws, error)``
            on_open: open callback, ``def on_open(ws)``
            on_close: close callback, ``def on_close(ws)``

        Other parameters are the same as ``.request``

        Returns:
            a WebSocket instance to communicate with the server.
        )r   
on_messageon_erroron_openon_close)r  r   	duphandleresetr.   connect)	r   r   r2  r3  r4  r5  r   r   r;   s	            rD   
ws_connectzSession.ws_connect  sc    & 	""$yy""$		!
 	

3!&!	rC   c                6    | j                   j                         S r   )r   upkeepr  s    rD   r;  zSession.upkeep  s    yy!!rC   gzip, deflate, brr   r   r   Fc"           	     B   !*+,-./0  j                          |r5 j                  j                         + j                  j                          n j                  +t	        +fi d|d|d j
                  |gd j                  d|d|d j                  |gd j                  |gd	|d
|	xs  j                  d|
t        u r j                  n|
d| j                  n|d| j                  n|d j                  |gd|d|xs  j                  d j                   |gd|d|d|d|xs  j"                  d|xs  j$                  d|xs  j&                  d|xs  j(                  d| j*                  n|d|d|xs  j,                  d|xs  j.                  d|d|d | d!|xs  j0                  d" j2                  d#t4        j6                  d$t8        j:                  \  0*,/.}"|rt9        j:                         -*+!,./0 f	d%}#+-fd&}$ j<                  j?                  |#      }%|%jA                  |$       .jC                           jE                  +*,!      }&-jG                          tI        /      }'tK        |'tL              r+j                          |'0|&_'        |%|&_(        |"|&_)        /|&_        |&S 	  jT                  d'k(  r*tV        jX                  j[                  +j\                         ne jT                  d(k(  rFt_        j`                         jb                  je                  +j\                        jg                          n+j]                           jE                  +*,!      }&0|&_'        |&+j                          S # th        $ r^}( jE                  +*,!      }&0|&_'        tk        |(jl                  to        |(            }) |)to        |(      |(jl                  |&      |(d}(~(ww xY w# +j                          w xY w))zESend the request, see ``requests.request`` for details on parameters.r.  r   params_listrS   rw   ry   headers_listcookies_listrz   rM   rY   r[   Nr]   proxies_listrQ   rR   verify_listr~   r   r   r_   r`   ra   rc   rd   r   rk   rm   r   r   r   ro   rg   queue_classevent_classc                   	 	 j                          t        t        j                        j                         s(t        t        j                        j                          j                  t               y # t        $ rU} 
j                        }	|_        j	                  t        t        |       | j                  |             Y d } ~ d } ~ ww xY w# t        t        j                        j                         s(t        t        j                        j                          j                  t               w xY wr   )performr   r  r/  
put_nowaitr    r7   coder   r  Eventis_setr   putr(   )er   r   r   rf   rr   r   header_recvedr   reqr   s     rD   rF  z Session.request.<locals>.performF  s    &IIK  	?FFHY__m<@@BEE*% ! H..6=2BOC #&CKLL!1#a&!&&#!FGGH  	?FFHY__m<@@BEE*%s+   A9 9	CACC CC A'Ec                F    j                          j                          y r   )waitr7  )futr   header_parseds    rD   cleanupz Session.request.<locals>.cleanupT  s    ""$	rC   r   r   )8r  r   r6  r7  r+   rU   rS   rI   r   rM   r*   rY   r[   r]   rO   rR   rW   r_   r`   ra   rc   rd   rk   rm   ro   rg   r   Queuer  rI  r"  submitadd_done_callbackrP  r  r   r   
isinstancer    r/  stream_taskquit_nowr  r   tpoolexecuterF  r   get_hub
threadpoolspawnr   r   r"   rH  r7   )1r   r.  r   rU   rw   ry   rI   rK   rz   rM   rY   r[   r]   rO   rQ   rR   rW   r~   r   r   r_   r`   ra   rc   rd   rf   r   rk   rm   ro   r   r   r   rr   rY  rF  rS  rX  r   first_elementrL  errorr   r   r   rR  rM  r   rN  s1   `                        `       `        @@@@@@@rD   r/  zSession.request  s   L 	""$ 		##%AIIOO		AAQ+B
+B
 +B
 f-	+B

 ]]+B
 +B
 +B
 ,,0+B
 --1+B
 +B
 "+B
 %,w$6DLLG+B
 & !00 +B
" $ ,,%+B
& ,,0'+B
( )+B
* "4T__++B
, f--+B
. /+B
0 ,1+B
2 .3+B
4 $7t'7'75+B
6 txx7+B
8 (T[[9+B
: .;+B
> & !00 A+B
B C+B
D &:):):E+B
F  14>>G+B
H I+B
J *K+B
L  M+B
N "O+B
P **Q+B
R S+B
T "U+B
>V]A}hZ %OO-M& & --..w7K))'2  &&6=*:OC  (NM-)9:	##CK)CO#CLCIJ<<:-NN**1995\\X-NN$//55aii@DDFIIK **v}.> "	  8**v}.> ""1663q62CFAFFC0a78 	s,   BN" 4P "	P	+APP		P Pc                ,     | j                   dd|d|S Nr   r-  rB   r/  r   r   r   s      rD   headzSession.head      t||=6s=f==rC   c                ,     | j                   dd|d|S Nr   r-  rB   rc  rd  s      rD   r   zSession.get      t||<5c<V<<rC   c                ,     | j                   dd|d|S Nr   r-  rB   rc  rd  s      rD   postzSession.post  rf  rC   c                ,     | j                   dd|d|S Nr   r-  rB   rc  rd  s      rD   rK  zSession.put  ri  rC   c                ,     | j                   dd|d|S Nr   r-  rB   rc  rd  s      rD   patchzSession.patch      t||>7>v>>rC   c                ,     | j                   dd|d|S Nr   r-  rB   rc  rd  s      rD   deletezSession.delete      t||?8???rC   c                ,     | j                   dd|d|S Nr   r-  rB   rc  rd  s      rD   optionszSession.options      t||@9#@@@rC   c                ,     | j                   dd|d|S Nr   r-  rB   rc  rd  s      rD   tracezSession.trace  rr  rC   c                ,     | j                   dd|d|S Nr   r-  rB   rc  rd  s      rD   queryzSession.query  rr  rC   )NNT)r   zOptional[Curl]r  zOptional[ThreadType]r  rV   r   Unpack[BaseSessionParams[R]]r  r  r.  
HttpMethodr   r7   r   zUnpack[StreamRequestParams])NNNN)r  r.   )r  r\   Br.  r  r   r7   rU   ru   rw   rv   ry   rx   rI   rH   rK   rJ   rz   rT   rM   rL   rY   r{   r[   r|   r]   r}   rO   rN   rQ   rP   rR   rL   rW   r|   r~   rP   r   rP   r   r   r_   r^   r`   rP   ra   rP   rc   rb   rd   r|   rf   re   r   r   rk   rj   rm   rP   ro   rn   r   r|   r   r\   r   r   rr   rV   r   r7   r   zUnpack[RequestParams])r>   r?   r@   r	  r   r
  r   r"  r$  r)  r'  r   r   r9  r;  r*   r/  re  r   rl  rK  rq  ru  ry  r}  r  __classcell__r  s   @rD   r  r  I  s   T
  $'+&*	DBDB %DB  $	DB
 /DBL    

   .	  KO!	!F" 6:RV&*)-)- $*.GN*.'+'+#04!%!%)</348! $DH*.?F,.MQ#'6:!%(, %E__ _ 3	_
 P_ $_ '_ '_ _ (_ E_ (_ %_ %_ _  .!_" #_$ %_& ''_( -)_* 2+_, -_. /_0 B1_2 (3_4 =5_6 *7_8 K9_: !;_< 4=_> ?_@ A_B &C_D E_B>=>=?@A??rC   r  c                  T    e Zd ZdZdddd	 	 	 	 	 d! fdZed        Zed        Zd Zd	 Z	d
 Z
d Zd Zd"dZd Ze	 	 	 	 	 	 d#d       Zdddddeddddddddddddddddddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$dZdddddddedddddddddddddddddddddddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 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Zd&d Z xZS )'AsyncSessionzAAn async request session, cookies and connections will be reused.N
   )loop
async_curlmax_clientsc               n    t        |   di | || _        || _        || _        | j                          y)a
  
        Parameters set in the ``__init__`` method will be override by the same parameter
        in request method.

        Parameters:
            loop: loop to use, if not provided, the running loop will be used.
            async_curl: [AsyncCurl](/api/curl_cffi#curl_cffi.AsyncCurl) object to use.
            max_clients: maxmium curl handle to use in the session,
                this will affect the concurrency ratio.
            headers: headers to use in the session.
            cookies: cookies to add in the session.
            auth: HTTP basic auth, a tuple of (username, password), only basic auth is
                supported.
            proxies: dict of proxies to use, prefer to use ``proxy`` if they are the
                same. format: ``{"http": proxy_url, "https": proxy_url}``.
            proxy: proxy to use, format: "http://proxy_url".
                Cannot be used with the above parameter.
            proxy_auth: HTTP basic auth for proxy, a tuple of (username, password).
            base_url: absolute url to use for relative urls.
            params: query string for the session.
            verify: whether to verify https certs.
            timeout: how many seconds to wait before giving up.
            trust_env: use http_proxy/https_proxy and other environments, default True.
            allow_redirects: whether to allow redirection.
            max_redirects: max redirect counts, default 30, use -1 for unlimited.
            impersonate: which browser version to impersonate in the session.
            ja3: ja3 string to impersonate in the session.
            akamai: akamai string to impersonate in the session.
            extra_fp: extra fingerprints options, in complement to ja3 and akamai str.
            default_encoding: encoding for decoding response content if charset is not
                found in headers. Defaults to "utf-8". Can be set to a callable for
                automatic detection.
            cert: a tuple of (cert, key) filenames for client cert.
            response_class: A customized subtype of ``Response`` to use.

        Notes:
            This class can be used as a context manager, and it's recommended to use via
            ``async with``.
            However, unlike aiohttp, it is not required to use ``with``.

        .. code-block:: python

            from curl_cffi.requests import AsyncSession

            # recommended.
            async with AsyncSession() as s:
                r = await s.get("https://example.com")

            s = AsyncSession()  # it also works.
        NrB   )r  r   _loop_acurlr  	init_pool)r   r  r  r  r   r  s        rD   r   zAsyncSession.__init__  s6    t 	"6"
 &rC   c                d    | j                   t        j                         | _         | j                   S r   )r  asyncioget_running_loopr  s    rD   r  zAsyncSession.loop  s&    :: 113DJzzrC   c                h    | j                   t        | j                        | _         | j                   S )N)r  )r  r   r  r  s    rD   acurlzAsyncSession.acurl  s&    ;;#3DK{{rC   c                    t        j                  | j                        | _        	 	 | j                  j	                  d        # t         j
                  $ r Y y w xY wr   )r  	LifoQueuer  poolrG  	QueueFullr  s    rD   r  zAsyncSession.init_pool  sS    %%d&6&67			$$T*  $$ s   A AAc                   K   | j                   j                          d {   }|t        | j                        }|S 7 w)Nr  )r  r   r   rl   r   r   s     rD   pop_curlzAsyncSession.pop_curl  s7     YY]]_$<djj)D  %s   A?Ac                    t        t        j                        5  | j                  j	                  |       d d d        y # 1 sw Y   y xY wr   )r   r  r  r  rG  r  s     rD   	push_curlzAsyncSession.push_curl  s7    g''( 	'II  &	' 	' 	's	   ?Ac                   K   | S wr   rB   r  s    rD   
__aenter__zAsyncSession.__aenter__  s     s   c                @   K   | j                          d {    y 7 wr   r&  r(  s     rD   	__aexit__zAsyncSession.__aexit__  s     jjl 	s   c                   K   | j                   j                          d{    d| _        	 	 | j                  j	                         }|r|j                          .7 :# t
        j                  $ r Y yw xY ww)r+  NT)r  r'  r   r  
get_nowaitr  
QueueEmptyr  s     rD   r'  zAsyncSession.close  sl     jj   yy++-JJL	  	! %% s2   A6AA6,A A6A30A62A33A6c                    |j                          | j                  s=| j                  j                  |       |j	                          | j                  |       y |j                          y r   )clean_after_performr   r  remove_handler7  r  r'  r  s     rD   release_curlzAsyncSession.release_curl  sE      "||JJ$$T*JJLNN4 JJLrC   c                  K    | j                   d||d|ddi d{   }	 | |j                          d{    y7 #7 # |j                          d{  7   w xY ww)z<Equivalent to ``async with request(..., stream=True) as r:``r-  r   TNrB   )r/  acloser0  s        rD   r   zAsyncSession.stream%  s^      !DLLOCO6O$OO	I**,	 P #**,sA   A%AA%A A%AA%A%A"AA""A%Tr<  r   r   c                B  K   | j                          | j                          d{   }t        d"i d|ddd|d| j                  d| j                  |gd| j
                  |gd	| j                  |gd
|xs | j                  d|t        u r| j                  n|d|| j                  n|d|	| j                  n|	d| j                  |
gd|d|xs | j                  d| j                  |gd|d|d|xs | j                  d|xs | j                   d|xs | j"                  d|xs | j$                  d|| j&                  n|d|d|xs | j(                  d|xs | j*                  d|d|xs | j,                  dt.        j0                  dt.        j2                   |j5                  t6        j8                  d        | j:                  j=                  d|j>                         d{    tA        tC        tD        tF           |       ||!      S 7 7 *w)#a  Connects to a WebSocket.

        Args:
            url: url for the requests.
            autoclose: whether to close the WebSocket after receiving a close frame.
            params: query string for the requests.
            headers: headers to send.
            cookies: cookies to use.
            auth: HTTP basic auth, a tuple of (username, password), only basic auth is
                supported.
            timeout: how many seconds to wait before giving up.
            allow_redirects: whether to allow redirection.
            max_redirects: max redirect counts, default 30, use -1 for unlimited.
            proxies: dict of proxies to use, prefer to use ``proxy`` if they are the
                same. format: ``{"http": proxy_url, "https": proxy_url}``.
            proxy: proxy to use, format: "http://user@pass:proxy_url".
                Can't be used with `proxies` parameter.
            proxy_auth: HTTP basic auth for proxy, a tuple of (username, password).
            verify: whether to verify https certs.
            referer: shortcut for setting referer header.
            accept_encoding: shortcut for setting accept-encoding header.
            impersonate: which browser version to impersonate.
            ja3: ja3 string to impersonate.
            akamai: akamai string to impersonate.
            extra_fp: extra fingerprints options, in complement to ja3 and akamai str.
            default_headers: whether to set default browser headers.
            quote: Set characters to be quoted, i.e. percent-encoded. Default safe
                string is ``!#$%&'()*+,/:;=?@[]~``. If set to a sting, the character
                will be removed from the safe string, thus quoted. If set to False, the
                url will be kept as is, without any automatic percent-encoding, you must
                encode the URL yourself.
            curl_options: extra curl options to use.
            http_version: limiting http version, defaults to http2.
            interface: which interface to use.
            cert: a tuple of (cert, key) filenames for client cert.
            max_recv_speed: maximum receive speed, bytes per second.
        Nr   r.  r   r   rS   r>  r?  r@  rM   rY   r[   r]   rA  rQ   rR   rB  r~   r   r_   r`   ra   rc   rd   r   rk   rm   r   ro   rC  rD  r   )	autocloserB   )$r  r  r+   rS   rU   rI   rK   rM   r*   rY   r[   r]   rO   rR   rW   r_   r`   ra   rc   rd   rk   rm   ro   r  rT  rI  setoptr   CONNECT_ONLYr  run_in_executorrF  r-   r   r  r)   )r   r   r  rU   rI   rK   rM   rY   r[   r]   rO   rQ   rR   rW   r~   r   r_   r`   ra   rc   rd   r   rk   rm   ro   r   r   s                              rD   r9  zAsyncSession.ws_connect3  s    D 	""$]]_$ $	
$	
$	
 $	
 ]]	$	

 f-$	
 ,,0$	
 ,,0$	
 "$	
 %,w$6DLLG$	
 & !00 $	
 $ ,,$	
  ,,0!$	
" #$	
$ "4T__%$	
& f-'$	
( )$	
* ,+$	
, $7t'7'7-$	
. txx/$	
0 (T[[1$	
2 .3$	
6 & !00 9$	
: ;$	
< &:):):=$	
>  14>>?$	
@ *A$	
B "C$	
D  E$	
F  G$	
J 	G((!,ii''dll;;;h'.
 	
S %P 	<s"   $HHGH2H3(HHr   Fc"           
     H   !*+,-./0K    j                           j                          d{   +t        d(i d+d|d|d j                  |gd j                  d|d|d	 j
                  |gd
 j                  |gd|d|	xs  j                  d|
t        u r j                  n|
d| j                  n|d| j                  n|d j                  |gd|d|xs  j                  d j                  |gd|d|d|d|xs  j                  d|xs  j                   d|xs  j"                  d|xs  j$                  d| j&                  n|d|d|xs  j(                  d|xs  j*                  d|d |d!| d"|xs  j,                  d# j.                  d$t0        j2                  d%t0        j4                  \  /*,.-}"|r j6                  j9                  +      0*+!,-./ 0f
d&}#+ fd'}$t1        j:                   |#             }%|%j=                  |$       t?        t0        j4                  -      jA                          d{     jC                  +*,!      }&tE        .      }'tG        |'tH              r jK                  +       |'/|&_&        |%|&_'        |"|&_(        .|&_)        |&S 	  j6                  j9                  +      00 d{     jC                  +*,!      }&/|&_&        |& jK                  +       S 7 7 7 8# tT        $ r^}( jC                  +*,!      }&/|&_&        tW        |(jX                  t[        |(            }) |)t[        |(      |(jX                  |&      |(d}(~(ww xY w#  jK                  +       w xY ww))zISend the request, see ``curl_cffi.requests.request`` for details on args.Nr   r.  r   r>  rS   rw   ry   r?  r@  rz   rM   rY   r[   r]   rA  rQ   rR   rB  r~   r   r   r_   r`   ra   rc   rd   r   rk   rm   r   r   r   ro   rg   rC  rD  c                   
K   	  d {    t        t        j                        j                         s(t        t        j                        j                          j                  t               d {    y 7 r# t         $ rU} 
j                        }	|_        j                  t	        t        |       | j                  |             Y d } ~ d } ~ ww xY w7 h# t        t        j                        j                         s(t        t        j                        j                          j                  t               d {  7   w xY wwr   )r   r  r/  rG  r    r7   rH  r   r  rI  rJ  r   rK  r(   )rL  r   r   r   rf   rr   r   rM  r   rN  r   tasks     rD   rF  z%AsyncSession.request.<locals>.perform  s     ,JJ  }=DDFW]]M:>>@%%
+++   H..fm5EC #&CKLL!1#a&!&&#!FGGH ,  }=DDFW]]M:>>@%%
+++sj   EA? A=A? A(E7C 8E=A? ?	CACC" CC"  E"A)EEEEc                (    j                         y r   )r  )rQ  r   r   s    rD   rS  z%AsyncSession.request.<locals>.cleanup  s    !!$'rC   rB   ).r  r  r+   rU   rS   rI   rK   rM   r*   rY   r[   r]   rO   rR   rW   r_   r`   ra   rc   rd   rk   rm   ro   rg   r  rT  rI  r  
add_handlecreate_taskrV  r   rP  r  r   rW  r    r  r/  astream_taskrY  r   r   r"   rH  r7   )1r   r.  r   rU   rw   ry   rI   rK   rz   rM   rY   r[   r]   rO   rQ   rR   rW   r~   r   r   r_   r`   ra   rc   rd   rf   r   rk   rm   ro   r   r   r   rr   rY  rF  rS  rX  r   r_  rL  r`  r   r   r   rM  r   rN  r  s1   `                        `       `        @@@@@@@rD   r/  zAsyncSession.request  sj    L 	""$]]_$AQ +B
+B
+B
 +B
 f-	+B

 ]]+B
 +B
 +B
 ,,0+B
 ,,0+B
 +B
 "+B
 %,w$6DLLG+B
 & !00 +B
" $ ,,%+B
& ,,0'+B
( )+B
* "4T__++B
, f--+B
. /+B
0 ,1+B
2 .3+B
4 $7t'7'75+B
6 txx7+B
8 (T[[9+B
: .;+B
> & !00 A+B
B C+B
D &:):):E+B
F  14>>G+B
H I+B
J *K+B
L  M+B
N "O+B
P **Q+B
R  S+B
T  U+B
>V]A}hX ::((.D, ,( "--gi8K))'2w}}m499;;; &&fm-=C ,A.M-)9:!!$'##CK*C#CLCIJ(zz,,T2

 **&-1A? "!!$'U %H <.  8**&-1A? ""1663q62CFAFFC0a78 !!$'sl   $N"LH/N" L!A%N" L" 'L (L" ,N 	N"N" L" "	N	+ANN		N NN"c                ,     | j                   dd|d|S rb  rc  rd  s      rD   re  zAsyncSession.head:  rf  rC   c                ,     | j                   dd|d|S rh  rc  rd  s      rD   r   zAsyncSession.get=  ri  rC   c                ,     | j                   dd|d|S rk  rc  rd  s      rD   rl  zAsyncSession.post@  rf  rC   c                ,     | j                   dd|d|S rn  rc  rd  s      rD   rK  zAsyncSession.putC  ri  rC   c                ,     | j                   dd|d|S rp  rc  rd  s      rD   rq  zAsyncSession.patchF  rr  rC   c                ,     | j                   dd|d|S rt  rc  rd  s      rD   ru  zAsyncSession.deleteI  rv  rC   c                ,     | j                   dd|d|S rx  rc  rd  s      rD   ry  zAsyncSession.optionsL  rz  rC   c                ,     | j                   dd|d|S r|  rc  rd  s      rD   r}  zAsyncSession.traceO  rr  rC   c                ,     | j                   dd|d|S r  rc  rd  s      rD   r  zAsyncSession.queryR  rr  rC   )r  zOptional[AsyncCurl]r  r\   r   r  r  r  )4r   r7   r  rV   rU   ru   rI   rH   rK   rJ   rM   rL   rY   r{   r[   r|   r]   r}   rO   rN   rQ   rP   rR   rL   rW   r|   r~   rP   r   rP   r_   r^   r`   rP   ra   rP   rc   rb   rd   r|   r   r   rk   rj   rm   rP   ro   rn   r   r\   r  r-   r  r  )r>   r?   r@   r	  r   r
  r  r  r  r  r  r  r  r'  r  r   r   r*   r9  r/  re  r   rl  rK  rq  ru  ry  r}  r  r  r  s   @rD   r  r    s)   K
 *.> (	>
 > />@  
  
'
   .	   59)-)-*.GN*.'+'+#04!%!%)<48! $DH*.,.MQ#'6:5q
q
 q
 3	q

 'q
 'q
 (q
 Eq
 (q
 %q
 %q
 q
 .q
 q
 q
  '!q
" 2#q
$ %q
& 'q
( B)q
* (+q
, *-q
. K/q
0 !1q
2 43q
4 5q
6 
7q
n 6:RV&*)-)- $*.GN*.'+'+#04!%!%)</348! $DH*.?F,.MQ#'6:!%(, %ER(R( R( 3	R(
 PR( $R( 'R( 'R( R( (R( ER( (R( %R( %R( R(  .!R(" #R($ %R(& ''R(( -)R(* 2+R(, -R(. /R(0 B1R(2 (3R(4 =5R(6 *7R(8 K9R(: !;R(< 4=R(> ?R(@ AR(B &CR(D ER(h>=>=?@A??rC   r  )r   r7   r  rV   r   )r   zqueue.Queue)r   zasyncio.Queue)X
__future__r   r  http.cookiesr9   r   sysr  r  concurrent.futuresr   
contextlibr   r   r   ior   typingr	   r
   r   r   r   r   r   r   r   urllib.parser   aior   constr   r   r   r   r   r   r   utilsr   rK   r   r   r   
exceptionsr    r!   r"   rI   r#   r$   r_   r%   r&   r'   modelsr(   r)   r*   r+   r,   
websocketsr-   r.   ImportErrorr   eventlet.tpoolr   version_infor1   typing_extensionsr4   r6   rG   rt   r   r   dictr7   
ThreadTyper  r   r   r   r   r  r  rB   rC   rD   <module>r     s   "    
   1 D D 
 
 
 "  6 6 , , # 5 5 C C ) K K ( @ @ 1k  k  w8X6A8$A(IU GAJ	 :iu @+5 
 
 YFS#XI!)2I&)*
P

;d)'!* d)NX?k!n X?v
o?;q> o?  s   9F#F/#F,/F8