
    diW                       d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZ d dl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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" 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/m0Z0 ddl1m2Z2m3Z3m4Z4 ddl5m6Z6m7Z7 ddl8m9Z9m:Z:m;Z; ddl<m=Z=m>Z> ddl+m?Z?m@Z@mAZA ddlBmCZCmDZDmEZE ej                  dk\  r ede>e>      ZGn
 ede>      ZGer?d dlHmIZI  G d ded      ZJ G d  d!eeG   ed      ZK G d" d#ed      ZL G d$ d%eLd      ZMn G d& d'      ZN eN       ZIeOePePf   ZJeZKeecZLZMed(   ZQed)   ZRd3d*ZSd4d5d+ZTd4d6d,ZU G d- d.eeG         ZV G d/ d0eVeG         ZW G d1 d2eVeG         ZXy)7    )annotationsN)ThreadPoolExecutor)asynccontextmanagercontextmanagersuppress)Callable)BytesIO)TYPE_CHECKINGGenericLiteralOptional	TypedDictTypeVarUnioncast)urlparse)	timedelta   )	AsyncCurl)CurlHttpVersionCurlInfoCurlOpt)Curl	CurlErrorCurlMime)CurlCffiWarning   )CookiesCookieTypes
CurlMorsel)RequestExceptionSessionClosed
code2error)HeadersHeaderTypes)BrowserTypeLiteralExtraFingerprintsExtraFpDict)
STREAM_ENDResponse)HttpVersionLiteralnot_setset_curl_options)AsyncWebSocket	WebSocketWebSocketError)      R)bounddefault)r4   )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__     Q/root/funda.leads/venv/lib/python3.12/site-packages/curl_cffi/requests/session.pyr8   r8   0   s    	
rE   r8   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+<   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_cookiesraise_for_statusNr?   rD   rE   rF   rI   rI   7   s    &&&&''$$--2211AA<<$$""JJ  33))rE   rI   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]]rW   AOptional[Union[dict[str, str], list[tuple], str, BytesIO, bytes]]dataOptional[dict | list]jsonrJ   rK   rL   rM   rV   filesrN   rO   3Optional[Union[float, tuple[float, float], object]]r[   Optional[bool]r]   Optional[int]r_   rP   rQ   rR   rS   rT   rY   refereraccept_encodingOptional[Callable]content_callbackr`   ra   rb   rc   rd   re   rf   rg   rh   Union[str, Literal[False]]quoterl   rm   ro   rp   rq   r^   max_recv_speedOptional[CurlMime]	multipartrX   rt   Nr?   rD   rE   rF   rw   rw   U   s    22OO##&&&&''DD''$$$$--&&,,11AA''<<))JJ  33%%rE   rw   c                      e Zd ZU ded<   y)RequestParamsr   streamNr?   rD   rE   rF   r   r   u   s    rE   r   c                      e Zd Zed        Zy)_Unpackc                      y NrD   )argskwargss     rF   __getitem__z_Unpack.__getitem__{   s    rE   N)r@   rA   rB   staticmethodr   rD   rE   rF   r   r   z   s    		 
	rE   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   rX   schemehostname)url
parsed_urls     rF   _is_absolute_urlr      s'    #J
!!9j&9&9::rE   c                D    	 | j                   d   S # t        $ r |cY S w xY wNr   )queue
IndexErrorqr5   s     rF   _peek_queuer      s)    wwqz     c                D    	 | j                   d   S # t        $ r |cY S w xY wr   )_queuer   r   s     rF   _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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)rK   rM   rO   rQ   rS   rT   rU   rW   rY   r[   r\   r]   r_   ra   rb   rc   re   rf   rh   ri   rk   rm   rn   ro   rq   rs   rt   ru   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 tA        | j
                        stC        d      d| _"        | j                  du s| j                  TtF        jH                  jK                  d      xs- tF        jH                  jK                  d	      xs | j                  | _        y y )
NFzZ`response_class` must be a subclass of `curl_cffi.requests.models.Response`, not of type ``z)Cannot specify both 'proxy' and 'proxies'r:   z2You need to provide an absolute url for 'base_url'TREQUESTS_CA_BUNDLECURL_CA_BUNDLE)&r$   rK   r   _cookiesrO   rU   rW   rY   r[   r\   r]   r_   ra   rb   rc   re   rf   rh   ri   rk   rm   rn   ro   rq   
issubclassr*   	TypeErrorrs   rt   ru   rQ   rT   r   
ValueError_closedosenvironget)selfrK   rM   rO   rQ   rS   rT   rU   rW   rY   r[   r\   r]   r_   ra   rb   rc   re   rf   rh   ri   rk   rm   rn   ro   rq   rs   rt   ru   s                                rF   __init__zBaseSession.__init__   s   @ w'(	 ".*& . 0(.B$*(
"	%*^X*NRW*W  ./q2 
 -8. 0WGHHenG")-R$==!1$--!@QRR ;;$$++"5

34 ::>>"23;; K #6rE   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        ti        t        tj        |j	                  t
        jl                                    |_7        t        t        |j	                  t
        jp                              |_9        t        t        |j	                  t
        jt                              }	 |j                         |_;        t        t        |j	                  t
        jz                              |_>        t        t        |j	                  t
        j~                              |_@        t        t        |j	                  t
        j                              |_B        t        t        |j	                  t
        j                              |_D        |j|                  |j                  z   |_E        | j                  D ]   }|j	                  |      |j                  |<   " |S # tH        $ r Y w xY wc c}w # tx        $ r |j                  d      |_;        Y Nw xY w)N   i  s   HTTP/       	z
set-cookiedomain path/secure)r   r   r   )secondszlatin-1)Hr   r3   rs   bytesgetinfor   EFFECTIVE_URLdecoder   getvaluecontentr^   HTTP_VERSIONrm   RESPONSE_CODEstatus_codeok
splitlinesstrip
startswithget_reason_phrasereasonappendr$   rK   r   rM   get_listr;   SimpleCookieloaditemssetvaluer   rX   	Exceptionrt   
COOKIELISTr    from_curl_formatr   update_cookies_from_curl
PRIMARY_IP
primary_ipPRIMARY_PORTprimary_portLOCAL_IPlocal_ip
LOCAL_PORT
local_portrh   r   float
TOTAL_TIMEelapsedREDIRECT_COUNTredirect_countREDIRECT_URLredirect_urlUnicodeDecodeErrorSIZE_DOWNLOAD_Tdownload_sizeSIZE_UPLOAD_Tupload_sizeHEADER_SIZEheader_sizeREQUEST_SIZErequest_sizeresponse_sizerk   infos)r   curlbufferheader_bufferrh   rt   crspheader_linesheader_listheader_lineset_cookie_headers
set_cookiecookienamemorselmorselsredirect_url_bytesinfos                      rF   _parse_responsezBaseSession._parse_response   sq    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/UAIIh>Q>Q4R(ST!#qyy1H1H'IJ!%83H3H)IJ	D188:C !aii0H0H&IJsAIIh.D.D$EFsAIIh.B.B$CDQYYx/D/D%EF--? OO 	.DiioCIIdO	. 
E   " 	D188CC	Ds+   -B)V7WW 7	WWW/.W/c                2    | j                   rt        d      y )Nz'Session is closed, cannot send request.)r   r"   r   s    rF   _check_session_closedz!BaseSession._check_session_closedH  s    << IJJ rE   c                    | j                   S r   )r   r  s    rF   rM   zBaseSession.cookiesL  s    }}rE   c                $    t        |      | _        y r   )r   r   )r   rM   s     rF   rM   zBaseSession.cookiesP  s      (rE   )8rK   rJ   rM   rL   rO   rN   rQ   rP   rS   rR   rT   rN   rU   rR   rW   rV   rY   rX   r[   rZ   r\   rX   r]   rX   r_   r^   ra   r`   rb   rR   rc   rR   re   rd   rf   rX   rh   rg   ri   rV   rk   rj   rm   rl   rn   rX   ro   rR   rq   rp   rs   rr   rt   rX   ru   rX   )returnr3   )r  r   )rM   r   r  None)
r@   rA   rB   __doc__r   r  r  propertyrM   setterrD   rE   rF   r   r      s   W
 *.)-*.'+#04"&!%57 $48! $DH $?F'+%)MQ#'6:,0 %!&=S 'S '	S
 (S %S S .S  S S S 3S S S S  2!S" #S$ %S& B'S( )S* =+S, %-S. #/S0 K1S2 3S4 !5S6 47S8 *9S: ;S< =SjO	
ObK   ^^) )rE   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.
            raise_for_status: automatically raise an HTTPError for 4xx and 5xx
                status codes.

        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rn   rD   )superr   _thread_use_thread_local_curlr   	_executor	threadinglocal_local_is_customized_curlr  r   rn   _curl)r   r  threaduse_thread_local_curlr   	__class__s        rF   r   zSession.__init__Z  s    p 	"6"&;# #//+DK+/(#' +0(#'djj#9 !%4djj+ADJrE   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   rn   r  r)  r  s    rF   r  zSession.curl  sj    &&''E# 
 4;;5#'djj#9 ;;###::rE   c                P    | j                   t               | _         | j                   S r   )r$  r   r  s    rF   executorzSession.executor  s     >>!/1DN~~rE   c                    | S r   rD   r  s    rF   	__enter__zSession.__enter__  s    rE   c                $    | j                          y r   closer   r   s     rF   __exit__zSession.__exit__  s    

rE   c                F    d| _         | j                  j                          y)Close the session.TN)r   r  r8  r  s    rF   r8  zSession.close  s    		rE   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   TNrD   )requestr8  r   r?  r   r   r  s        rF   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   rC  rD  rE  rF  r   r  r=   s	            rF   
ws_connectzSession.ws_connect  sc    & 	""$yy""$		!
 	

3!&!	rE   c                6    | j                   j                         S r   )r  upkeepr  s    rF   rL  zSession.upkeep  s    yy!!rE   gzip, deflate, brr   r   r   Fc"           	        !,-./012  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:                  \  2,.10}"|rt9        j:                         /,-!.012 f	d%}#-/fd&}$ j<                  j?                  |#      }%|%jA                  |$       0jC                           jE                  -,.!      }&/jG                          tI        1      }'tK        |'tL              r-j                          |'2|&_'        |%|&_(        |"|&_)        1|&_         jT                  r|&jU                          |&S 	  jV                  d'k(  r*d(dl,}(|(jZ                  j]                  -j^                         ne jV                  d)k(  rFd(dl0})|)jc                         jd                  jg                  -j^                        ji                          n-j_                           jE                  -,.!      }&2|&_'         jT                  r|&jU                          |&-j                          S # tj        $ r^}* jE                  -,.!      }&2|&_'        tm        |*jn                  tq        |*            }+ |+tq        |*      |*jn                  |&      |*d}*~*ww xY w# -j                          w xY w)*zESend the request, see ``requests.request`` for details on parameters.r?  r   params_listrU   rz   r|   headers_listcookies_listr}   rO   r[   r]   Nr_   proxies_listrS   rT   verify_listr   r   r   ra   rb   rc   re   rf   r   rm   ro   r   r   r   rq   ri   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!   r9   coder   r%  Eventis_setr   putr)   )er  r  r  rh   rt   r  header_recvedr   reqr   s     rF   rW  z Session.request.<locals>.performU  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   )waitrH  )futr  header_parseds    rF   cleanupz Session.request.<locals>.cleanupc  s    ""$	rE   r   r   r   )9r  r  rG  rH  r-   rW   rU   rK   r   rO   r,   r[   r]   r_   rQ   rT   rY   ra   rb   rc   re   rf   rm   ro   rq   ri   r   Queuer%  rZ  r3  submitadd_done_callbackra  r  r   r   
isinstancer!   r@  stream_taskquit_nowru   r"  eventlet.tpooltpoolexecuterW  r   get_hub
threadpoolspawnr   r   r#   rY  r9   )3r   r?  r   rW   rz   r|   rK   rM   r}   rO   r[   r]   r_   rQ   rS   rT   rY   r   r   r   ra   rb   rc   re   rf   rh   r   rm   ro   rq   r   r   r   rt   rj  rW  rd  ri  r  first_elementr   r   r]  errorr  r  r  rc  r^  r   r_  s3   `                        `       `          @@@@@@@rF   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
 )8(?$$_+B
" '4&;""#+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
> )8(?$$_?+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,   2BO 9Q 	Q#AP<<QQ Qc                ,     | j                   dd|d|S Nr   r>  rD   r@  r   r   r   s      rF   headzSession.head      t||=6s=f==rE   c                ,     | j                   dd|d|S Nr   r>  rD   ru  rv  s      rF   r   zSession.get      t||<5c<V<<rE   c                ,     | j                   dd|d|S Nr   r>  rD   ru  rv  s      rF   postzSession.post  rx  rE   c                ,     | j                   dd|d|S Nr   r>  rD   ru  rv  s      rF   r\  zSession.put  r{  rE   c                ,     | j                   dd|d|S Nr   r>  rD   ru  rv  s      rF   patchzSession.patch      t||>7>v>>rE   c                ,     | j                   dd|d|S Nr   r>  rD   ru  rv  s      rF   deletezSession.delete  s    t||?8???rE   c                ,     | j                   dd|d|S Nr   r>  rD   ru  rv  s      rF   optionszSession.options  s    t||@9#@@@rE   c                ,     | j                   dd|d|S Nr   r>  rD   ru  rv  s      rF   tracezSession.trace  r  rE   c                ,     | j                   dd|d|S Nr   r>  rD   ru  rv  s      rF   queryzSession.query  r  rE   )NNT)r  zOptional[Curl]r*  zOptional[ThreadType]r+  rX   r   Unpack[BaseSessionParams[R]]r  r  r?  
HttpMethodr   r9   r   zUnpack[StreamRequestParams])NNNN)r  r/   )r  r^   )Br?  r  r   r9   rW   rx   rz   ry   r|   r{   rK   rJ   rM   rL   r}   rV   rO   rN   r[   r~   r]   r   r_   r   rQ   rP   rS   rR   rT   rN   rY   r   r   rR   r   rR   r   r   ra   r`   rb   rR   rc   rR   re   rd   rf   r   rh   rg   r   r   rm   rl   ro   rR   rq   rp   r   r   r   r^   r   r   rt   rX   )r   r9   r   Unpack[RequestParams])r@   rA   rB   r  r   r  r  r3  r5  r:  r8  r   r   rJ  rL  r,   r@  rw  r   r~  r\  r  r  r  r  r  __classcell__r,  s   @rF   r  r  V  s   T
  $'+&*	FBFB %FB  $	FB
 /FBP    

   .	  KO!	!F" 6:RV&*)-)- $*.GN*.'+'+#04!%!%)</348! $DH*.?F,.MQ#'6:!%(, %Egg g 3	g
 Pg $g 'g 'g g (g Eg (g %g %g g  .!g" #g$ %g& ''g( -)g* 2+g, -g. /g0 B1g2 (3g4 =5g6 *7g8 K9g: !;g< 4=g> ?g@ AgB &CgD EgR>=>=?@A??rE   r  c                       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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.
            raise_for_status: automatically raise an HTTPError for 4xx and 5xx
                status codes.

        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.
        NrD   )r!  r   _loop_acurlr  	init_pool)r   r  r  r  r   r,  s        rF   r   zAsyncSession.__init__  s6    x 	"6"
 &rE   c                d    | j                   t        j                         | _         | j                   S r   )r  asyncioget_running_loopr  s    rF   r  zAsyncSession.loop   s&    :: 113DJzzrE   c                h    | j                   t        | j                        | _         | j                   S )N)r  )r  r   r  r  s    rF   acurlzAsyncSession.acurl  s&    ;;#3DK{{rE   c                    t        j                  | j                        | _        	 	 | j                  j	                  d        # t         j
                  $ r Y y w xY wr   )r  	LifoQueuer  poolrX  	QueueFullr  s    rF   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   rn   r   r  s     rF   pop_curlzAsyncSession.pop_curl  s5     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  rX  r  s     rF   	push_curlzAsyncSession.push_curl  s7    g''( 	'II  &	' 	' 	's	   ?Ac                   K   | S wr   rD   r  s    rF   
__aenter__zAsyncSession.__aenter__  s     s   c                @   K   | j                          d {    y 7 wr   r7  r9  s     rF   	__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  r8  r   r  
get_nowaitr  
QueueEmptyr  s     rF   r8  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_handles_and_buffersr   r  remove_handlerH  r  r8  r  s     rF   release_curlzAsyncSession.release_curl1  sE    &&(||JJ$$T*JJLNN4 JJLrE   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   TNrD   )r@  acloserA  s        rF   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%TrM  r   r   i      FgMbP??   c"                  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        |"j5                  t6        j:                  d!       | j<                  j?                  d|"j@                         d{    tC        tE        tF        tH           |       |"|||||||| |!"      }#	 |#jK                          |#S 7 /7 E# tL        $ r |#jO                           w xY w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.
            recv_queue_size: The maximum number of incoming WebSocket
                messages to buffer internally. This queue stores messages received
                by the Curl socket that are waiting to be consumed by calling `recv()`.
            send_queue_size: The maximum number of outgoing WebSocket
                messages to buffer before applying network backpressure. When you call
                `send(...)` the message is placed in this queue and transmitted when
                the Curl socket is next available for sending.
            max_send_batch_size: The max batch size for sent frames.
            coalesce_frames: If `True`, multiple pending messages in the send queue
                may be merged into a single WebSocket frame for improved throughput.
                **Warning:** This breaks the one-to-one mapping of `send()` calls to
                frames and should only be used when the application protocol is
                designed to handle concatenated data streams. Defaults to `False`.
            retry_on_recv_error: Retries `ws_recv()` if a recv error is raised.
                Retries up to a limited number of times with a delay in between.
            yield_interval: How often to yield control back to the event loop.
                This is a trade-off between throughput and responsiveness. Lower values
                means the loop yields more frequently and enables other tasks to run,
                while higher values are better for throughput. The balanced default
                is `1ms` but you can customize this to fit your application/use case.
            fair_scheduling: Changes the I/O priority from favoring receives (`5:1`)
                to a balanced ratio (`1:1`). Enable this to improve send responsiveness
                under heavy, concurrent load, at the cost of significantly lower overall
                throughput.
            yield_mask: Controls the frequency of cooperative multitasking
                yields in the read loop. The loop yields every `yield_mask + 1`
                operations. For efficiency, this value must be a power of two minus one
                (e.g., `63`, `127`, `255`). Lower values yield more often, improving
                fairness at the cost of throughput. Higher values yield less often,
                prioritizing throughput.
        Nr  r?  r   r   rU   rO  rP  rQ  rO   r[   r]   r_   rR  rS   rT   rS  r   r   ra   rb   rc   re   rf   r   rm   ro   r   rq   rT  rU  r   r   )		autocloserecv_queue_sizesend_queue_sizemax_send_batch_sizecoalesce_framesretry_on_recv_erroryield_intervalfair_scheduling
yield_maskrD   )(r  r  r-   rU   rW   rK   rM   rO   r,   r[   r]   r_   rQ   rT   rY   ra   rb   rc   re   rf   rm   ro   rq   r  re  rZ  setoptr   TCP_NODELAYCONNECT_ONLYr  run_in_executorrW  r.   r   r  r*   _start_io_tasksr0   	terminate)$r   r   r  rW   rK   rM   rO   r[   r]   r_   rQ   rS   rT   rY   r   r   ra   rb   rc   re   rf   r   rm   ro   rq   r   r  r  r  r  r  r  r  r  r  r=   s$                                       rF   rJ  zAsyncSession.ws_connectH  s    P 	""$]]_$ $	
$	
$	
 $	
 ]]	$	

 f-$	
 ,,0$	
 ,,0$	
 "$	
 %,w$6DLLG$	
 )8(?$$_$	
 '4&;""$	
  ,,0!$	
" #$	
$ "4T__%$	
& f-'$	
( )$	
* ,+$	
, $7t'7'7-$	
. txx/$	
0 (T[[1$	
2 .3$	
6 )8(?$$_7$	
: ;$	
< &:):):=$	
>  14>>?$	
@ *A$	
B "C$	
D  E$	
F  G$	
J 	G''+G((!,ii''dll;;;+h'.++ 3+ 3)+!
	 
 	} %R 	<"  	LLN	s:   $I8IG+I8I/I8I I8I8I55I8r   c"           
        !*+,-./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                  +       |'/|&_&        |%|&_'        |"|&_(        .|&_)         jT                  r|&jU                          |&S 	  j6                  j9                  +      00 d{     jC                  +*,!      }&/|&_&         jT                  r|&jU                          |& jK                  +       S 7 $7 7 T# tV        $ r^}( jC                  +*,!      }&/|&_&        tY        |(jZ                  t]        |(            }) |)t]        |(      |(jZ                  |&      |(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   rO  rU   rz   r|   rP  rQ  r}   rO   r[   r]   r_   rR  rS   rT   rS  r   r   r   ra   rb   rc   re   rf   r   rm   ro   r   r   r   rq   ri   rT  rU  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@  rX  r!   r9   rY  r   r  rZ  r[  r   r\  r)   )r]  r  r  r  rh   rt   r  r^  r   r_  r   tasks     rF   rW  z%AsyncSession.request.<locals>.performJ  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  )rb  r  r   s    rF   rd  z%AsyncSession.request.<locals>.cleanupX  s    !!$'rE   rD   )/r  r  r-   rW   rU   rK   rM   rO   r,   r[   r]   r_   rQ   rT   rY   ra   rb   rc   re   rf   rm   ro   rq   ri   r  re  rZ  r  
add_handlecreate_taskrg  r   ra  r  r   rh  r!   r  r@  astream_taskrj  r   ru   r   r#   rY  r9   )1r   r?  r   rW   rz   r|   rK   rM   r}   rO   r[   r]   r_   rQ   rS   rT   rY   r   r   r   ra   rb   rc   re   rf   rh   r   rm   ro   rq   r   r   r   rt   rj  rW  rd  ri  r  rq  r]  rr  r  r  r  r^  r   r_  r  s1   `                        `       `        @@@@@@@rF   r@  zAsyncSession.request  s    L 	""$]]_$AQ +B
+B
+B
 +B
 f-	+B

 ]]+B
 +B
 +B
 ,,0+B
 ,,0+B
 +B
 "+B
 %,w$6DLLG+B
 )8(?$$_+B
" '4&;""#+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
> )8(?$$_?+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? "((((*!!$'] %H <2  8**&-1A? ""1663q62CFAFFC0a78  !!$'sl   $OMH0O!M"BO$ M MM 	9O OOM 	O$AN==OO OOc                H   K    | j                   dd|d| d {   S 7 wrt  ru  rv  s      rF   rw  zAsyncSession.head  '     !T\\CSCFCCCC   " "c                H   K    | j                   dd|d| d {   S 7 wrz  ru  rv  s      rF   r   zAsyncSession.get  '     !T\\BCB6BBBBr  c                H   K    | j                   dd|d| d {   S 7 wr}  ru  rv  s      rF   r~  zAsyncSession.post  r  r  c                H   K    | j                   dd|d| d {   S 7 wr  ru  rv  s      rF   r\  zAsyncSession.put  r  r  c                H   K    | j                   dd|d| d {   S 7 wr  ru  rv  s      rF   r  zAsyncSession.patch  '     !T\\DcDVDDDDr  c                H   K    | j                   dd|d| d {   S 7 wr  ru  rv  s      rF   r  zAsyncSession.delete  s'     !T\\EsEfEEEEr  c                H   K    | j                   dd|d| d {   S 7 wr  ru  rv  s      rF   r  zAsyncSession.options  s'     !T\\FFvFFFFr  c                H   K    | j                   dd|d| d {   S 7 wr  ru  rv  s      rF   r  zAsyncSession.trace  r  r  c                H   K    | j                   dd|d| d {   S 7 wr  ru  rv  s      rF   r  zAsyncSession.query  r  r  )r  zOptional[AsyncCurl]r  r^   r   r  r  r  )Dr   r9   r  rX   rW   rx   rK   rJ   rM   rL   rO   rN   r[   r~   r]   r   r_   r   rQ   rP   rS   rR   rT   rN   rY   r   r   rR   r   rR   ra   r`   rb   rR   rc   rR   re   rd   rf   r   r   r   rm   rl   ro   rR   rq   rp   r   r^   r  r^   r  r^   r  r^   r  rX   r  rX   r  r   r  rX   r  r^   r  r.   )Dr?  r  r   r9   rW   rx   rz   ry   r|   r{   rK   rJ   rM   rL   r}   rV   rO   rN   r[   r~   r]   r   r_   r   rQ   rP   rS   rR   rT   rN   rY   r   r   rR   r   rR   r   r   ra   r`   rb   rR   rc   rR   re   rd   rf   r   rh   rg   r   r   rm   rl   ro   rR   rq   rp   r   r   r   r^   r   r   rt   rX   r  r3   )r   r9   r   r  r  r3   )r@   rA   rB   r  r   r  r  r  r  r  r  r  r  r8  r  r   r   r,   rJ  r@  rw  r   r~  r\  r  r  r  r  r  r  r  s   @rF   r  r    s   K
 *.@ (	@
 @ /@D  
  
'
   .	   59)-)-*.GN*.'+'+#04!%!%)<48! $DH*.,.MQ#'6:""#& %$) % %Ehh h 3	h
 'h 'h (h Eh (h %h %h h .h h h  '!h" 2#h$ %h& 'h( B)h* (+h, *-h. K/h0 !1h2 43h4 5h6 7h8 9h: !;h< =h> "?h@ AhB ChD EhF 
Gh\ 6:RV&*)-)- $*.GN*.'+'+#04!%!%)</348! $DH*.?F,.MQ#'6:!%(, %EV(V( V( 3	V(
 PV( $V( 'V( 'V( V( (V( EV( (V( %V( %V( V(  .!V(" #V($ %V(& ''V(( -)V(* 2+V(, -V(. /V(0 B1V(2 (3V(4 =5V(6 *7V(8 K9V(: !;V(< 4=V(> ?V(@ AV(B &CV(D EV(F 
GV(pDCDCEFGEErE   r  )r   r9   r  rX   r   )r   zqueue.Queue)r   zasyncio.Queue)Y
__future__r   r  http.cookiesr;   r   r   sysr%  r/  concurrent.futuresr   
contextlibr   r   r   collections.abcr   ior	   typingr
   r   r   r   r   r   r   r   urllib.parser   datetimer   aior   constr   r   r   r  r   r   r   utilsr   rM   r   r   r    
exceptionsr!   r"   r#   rK   r$   r%   ra   r&   r'   r(   modelsr)   r*   r+   r,   r-   
websocketsr.   r/   r0   version_infor3   typing_extensionsr6   r8   rI   rw   r   r   dictr9   
ThreadTyper  r   r   r   r   r  r  rD   rE   rF   <module>r     s}   "   	  
   1 D D $ 	 	 	 "   6 6 , , # 5 5 C C ) K K ( @ @ A A w8X6A8$A(IU GAJ	 <iu @+5 
 
 YFS#XI!)2I&)*
P

;t)'!* t)nb?k!n b?JhE;q> hErE   