o
    Z�¿ju3  ã                   @   sN  d dl m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mZ d dlmZ ddlmZ dd	lmZ d
dlmZ d
dlmZmZ d
dlmZ d
dlmZ zd dlmZ W n eym   d dl mZ Y nw eeƒ� d dl!m!Z" d dl#Z$W d  ƒ n1 s‡w   Y  e %d¡Z&h d£Z'dZ(G dd„ dƒZ)G dd„ dƒZ*dS )é    )ÚsuppressN)ÚFuture)ÚAnyÚOptionalÚUnion)Ú	AwaitableÚCallable)Ú	timedeltaé   )ÚCurl)ÚCurlCffiWarningé   )ÚCookies)Ú	HTTPErrorÚRequestException)ÚHeaders)Ú
STREAM_END)Úloads)Úmarkdownifyzcharset=([\w-]+)>   úutf-16úutf-32úutf-16beúutf-16leúutf-32beúutf-32leú	utf-16-beú	utf-16-leú	utf-32-beú	utf-32-leÚutf8Úutf16Úutf32úutf-8ú	utf-8-sig)i-  i.  i/  i3  i4  c                
   @   s2   e Zd ZdZ	d	dedededee fdd„ZdS )
ÚRequestzÑRepresenting a sent request.

    Attributes:
        url: request url.
        headers: request headers.
        method: request http method.
        body: request body as bytes, or None if not provided.
    NÚurlÚheadersÚmethodÚbodyc                 C   s   || _ || _|| _|| _d S ©N)r%   r&   r'   r(   )Úselfr%   r&   r'   r(   © r+   úT/home/dinkstrade/pdmp/venv/lib/python3.10/site-packages/curl_cffi/requests/models.pyÚ__init__;   s   
zRequest.__init__r)   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   r   Úbytesr-   r+   r+   r+   r,   r$   1   s    ûþýüûr$   c                   @   sn  e Zd ZdZd7dee dee fdd„Zdee	e
f fdd	„Zd
ee	e
f ddfdd„Zede	fdd„ƒZede	fdd„ƒZejde	ddfdd„ƒZedee	 fdd„ƒZede	fdd„ƒZde	fdd„Zdede	fdd„Zdd„ Zedefdd„ƒZd8d!d"„Zd9d#d$„Zd%d&„ Zd'd(„ Zd:d)d*„Zd8d+d,„Zd9d-d.„Zde	fd/d0„Z defd1d2„Z!d3d4„ Z"de	fd5d6„Z#dS );ÚResponsea¶  Contains information the server sends.

    Attributes:
        url: url used in the request.
        content: response body in bytes.
        text: response body in str.
        status_code: http status code.
        reason: http response reason, such as OK, Not Found.
        ok: is status_code in [200, 400)?
        headers: response headers.
        cookies: response cookies.
        elapsed: timedelta of the request duration.
        encoding: http body encoding.
        charset: alias for encoding.
        primary_ip: primary ip of the server.
        primary_port: primary port of the server.
        local_ip: local ip used in this connection.
        local_port: local port used in this connection.
        charset_encoding: encoding specified by the Content-Type header.
        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.
        redirect_count: how many redirects happened.
        redirect_url: the final redirected url.
        http_version: http version used.
        history: redirect responses, in request order. Response bodies are not
            available.
        download_size: total downloaded bytes (body).
        upload_size: total uploaded bytes (body).
        header_size: total header size.
        request_size: request size.
        response_size: download_size + header_size
    NÚcurlÚrequestc                 C   s¾   || _ || _d| _d| _d| _d| _d| _tƒ | _t	ƒ | _
tƒ | _d| _d| _d| _d| _d| _d| _d| _d| _g | _i | _d | _d | _d | _d | _d| _d| _d| _d| _d| _d| _ d S )	NÚ ó    éÈ   ÚOKTr"   r   F)!r5   r6   r%   ÚcontentÚstatus_codeÚreasonÚokr   r&   r   Úcookiesr	   ÚelapsedÚdefault_encodingÚredirect_countÚredirect_urlÚhttp_versionÚ
primary_ipÚprimary_portÚlocal_ipÚ
local_portÚhistoryÚinfosÚqueueÚstream_taskÚastream_taskÚquit_nowÚ_stream_closedÚdownload_sizeÚupload_sizeÚheader_sizeÚrequest_sizeÚresponse_size)r*   r5   r6   r+   r+   r,   r-   k   s<   
zResponse.__init__Úreturnc                 C   sN   t dd„ | j| j| j| jfD ƒƒrtdƒ‚| j ¡ }dD ]}| |d ¡ q|S )Nc                 s   s   � | ]}|d uV  qd S r)   r+   )Ú.0Úvaluer+   r+   r,   Ú	<genexpr>Œ   s
   € ÿ
ÿz(Response.__getstate__.<locals>.<genexpr>ziStreaming responses cannot be pickled; make the request without stream=True before pickling the response.)r5   rK   rL   rM   rN   )	ÚanyrK   rL   rM   rN   Ú	TypeErrorÚ__dict__ÚcopyÚpop)r*   ÚstateÚ	attributer+   r+   r,   Ú__getstate__‹   s   üþ	ÿ
zResponse.__getstate__r^   c                 C   s4   | j  |¡ d | _d | _d | _d | _d | _d| _d S ©NT)r[   Úupdater5   rK   rL   rM   rN   rO   )r*   r^   r+   r+   r,   Ú__setstate__¥   s   
zResponse.__setstate__c                 C   s   | j S )zAlias for encoding.)Úencoding©r*   r+   r+   r,   Úcharset®   s   zResponse.charsetc                 C   sR   t | dƒs&| j}|du r!t| jtƒr| j}nt| jƒr!|  | j¡}|p$d| _| jS )aé  
        Determines the encoding to decode byte content into text.

        The method follows a specific priority to decide the encoding:
        1. If ``.encoding`` has been explicitly set, it is used.
        2. The encoding specified by the ``charset`` parameter in the ``Content-Type``
            header.
        3. The encoding specified by the ``default_encoding`` attribute. This can either
            be a string (e.g., "utf-8") or a callable for charset autodetection.
        Ú	_encodingNr"   )ÚhasattrÚcharset_encodingÚ
isinstancerA   r2   Úcallabler;   rg   )r*   rd   r+   r+   r,   rd   ³   s   


zResponse.encodingrW   c                 C   s   t | dƒr	tdƒ‚|| _d S )NÚ_textz0Cannot set encoding after text has been accessed)rh   Ú
ValueErrorrg   )r*   rW   r+   r+   r,   rd   É   s   

c                 C   s0   | j  d¡}|rt |¡}|r| d¡S dS dS )z=Return the encoding, as specified by the Content-Type header.zContent-Typer   N)r&   ÚgetÚ
CHARSET_REÚsearchÚgroup)r*   Úcontent_typeÚcharset_matchr+   r+   r,   ri   Ï   s
   
zResponse.charset_encodingc                 C   s0   t | dƒs| jsd| _| jS |  | j¡| _| jS )Nrl   r7   )rh   r;   rl   Ú_decodere   r+   r+   r,   ÚtextØ   s   
ÿzResponse.textc                 C   s:   t  | j¡}| ¡ }|jdd�}td|› d|› d�ƒ}|S )NT)Úhtml_partialz<h1>z</h1><main>z</main>)ÚrdÚDocumentr;   ÚtitleÚsummaryÚmd)r*   Údocry   rz   Ú
body_as_mdr+   r+   r,   Úmarkdowná   s
   zResponse.markdownr;   c              	   C   s4   z	|j | jdd�W S  ttfy   |  d¡ Y S w )NÚreplace)Úerrorsr#   )Údecoderd   ÚUnicodeDecodeErrorÚLookupError)r*   r;   r+   r+   r,   rt   è   s
   ÿzResponse._decodec                 C   s&   | j std| j› d| j› �d| ƒ‚dS )z2Raise an error if status code is not in [200, 400)zHTTP Error z: r   N)r>   r   r<   r=   re   r+   r+   r,   Úraise_for_statusî   s   ÿzResponse.raise_for_statusc                 C   s   d| j v o	| jtv S )z0Whether this response is a well-formed redirect.Úlocation)r&   r<   ÚREDIRECT_STATIre   r+   r+   r,   Úis_redirectó   s   zResponse.is_redirectFc                 c   s�   � d}| j ||d�D ]2}|dur|| }|r| |¡n| ¡ }|r5|d r5|r5|d d |d kr5| ¡ nd}|E dH  q
|durF|V  dS dS ©z×
        iterate streaming content line by line, separated by ``\n``.

        Copied from: https://requests.readthedocs.io/en/latest/_modules/requests/models/
        which is under the License: Apache 2.0
        N)Ú
chunk_sizeÚdecode_unicodeéÿÿÿÿ)Úiter_contentÚsplitÚ
splitlinesr]   )r*   r‰   rŠ   Ú	delimiterÚpendingÚchunkÚlinesr+   r+   r,   Ú
iter_linesø   s,   €
ÿÿÿÿÿý
ÿzResponse.iter_linesc                 c   st   � |rt jdtdd� |rtƒ ‚| jr| jsJ dƒ‚	 | j ¡ }t|tƒr+|  	¡  |‚|t
u r0n|V  q|  	¡  dS ©zD
        iterate streaming content chunk by chunk in bytes.
        z9chunk_size is ignored, there is no way to tell curl that.r
   )Ú
stacklevelzstream mode is not enabled.TN)ÚwarningsÚwarnr   ÚNotImplementedErrorrK   r5   rn   rj   r   Ú_finalize_streamr   ©r*   r‰   rŠ   r‘   r+   r+   r,   rŒ     s(   €ý

ôzResponse.iter_contentc                 K   sJ   | j }|dur| ¡  dd¡}|tvrt| jfi |¤ŽS t| jfi |¤ŽS )z+return a parsed json object of the content.NÚ_ú-)ri   Úlowerr   ÚJSON_NATIVE_ENCODINGSr   ru   r;   )r*   Úkwri   rd   r+   r+   r,   Újson0  s   zResponse.jsonc                 C   s   |   ¡  dS ©z:Close the streaming connection, only valid in stream mode.N)r™   re   r+   r+   r,   Úclose9  s   zResponse.closec                 C   sj   | j rd S | jd u r| jd u r| jd u rd S d| _ | jr!| j ¡  | jr)| j ¡  | jr3| j ¡  d S d S ra   )rO   rK   rL   rN   ÚsetÚresultr5   r¢   re   r+   r+   r,   r™   =  s   

ÿzResponse._finalize_streamc                 C  s    �d}| j ||d�2 z93 dH W }|dur|| }|r| |¡n| ¡ }|r9|d r9|r9|d d |d kr9| ¡ nd}|D ]}|V  q=q
6 |durN|V  dS dS rˆ   )Úaiter_contentr�   rŽ   r]   )r*   r‰   rŠ   r�   r�   r‘   r’   Úliner+   r+   r,   Úaiter_linesJ  s2   €ÿÿÿÿÿýÿô
ÿzResponse.aiter_linesc                 C  s„   �|rt jdtdd� |rtƒ ‚| jr| jsJ dƒ‚	 | j ¡ I dH }t|tƒr1|  	¡ I dH  |‚|t
u r>|  	¡ I dH  dS |V  qr”   )r–   r—   r   r˜   rK   r5   rn   rj   r   Úacloser   rš   r+   r+   r,   r¥   e  s(   €ý
ózResponse.aiter_contentc                 Ã   s   �|   |  ¡ I dH ¡S )z*
        Return a decoded string.
        N)rt   Úacontentre   r+   r+   r,   Úatextƒ  s   €zResponse.atextc                 Ã   s2   �g }|   ¡ 2 z3 dH W }| |¡ q6 d |¡S )z8wait and read the streaming content in one bytes object.Nr8   )r¥   ÚappendÚjoin)r*   Úchunksr‘   r+   r+   r,   r©   ‰  s   €ÿ
zResponse.acontentc                 Ã   s   �| j r| j I dH  dS dS r¡   )rM   re   r+   r+   r,   r¨   �  s   €ÿzResponse.aclosec                 C   s   d| j › d�S )Nz<Response [z]>)r<   re   r+   r+   r,   Ú__repr__—  s   zResponse.__repr__)NN)NFN)NF)rU   N)$r.   r/   r0   r1   r   r   r$   r-   Údictr2   r   r`   rc   Úpropertyrf   rd   Úsetterri   ru   r~   r3   rt   r„   Úboolr‡   r“   rŒ   r    r¢   r™   r§   r¥   rª   r©   r¨   r®   r+   r+   r+   r,   r4   H   s>    " 	

	


r4   )+Ú
contextlibr   rK   Úrer–   Úconcurrent.futuresr   Útypingr   r   r   Úcollections.abcr   r   Údatetimer	   r5   r   Úutilsr   r?   r   Ú
exceptionsr   r   r&   r   Ústreamsr   Úorjsonr   ÚImportErrorr    r   r{   Úreadabilityrw   Úcompilero   rž   r†   r$   r4   r+   r+   r+   r,   Ú<module>   s8    ÿ

þ
