U
    ÎZjÏ  ã                   @   s’   d Z ddlZddlmZ dZddddœZe d	d
dg¡ZG dd	„ d	eƒZdd„ Z	dd„ Z
dd„ Zddd„Zdd„ Zdd„ Zd dd„Zd!dd„ZdS )"zGThis module provides functions for dumping information about responses.é    N)Úcompat)Údump_responseÚdump_alls   0.9s   1.0s   1.1)é	   é
   é   ÚPrefixSettingsÚrequestÚresponsec                       s   e Zd Z‡ fdd„Z‡  ZS )r   c                    s$   t |ƒ}t |ƒ}tt| ƒ | ||¡S )N)Ú_coerce_to_bytesÚsuperr   Ú__new__)Úclsr	   r
   ©Ú	__class__© ú_/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/requests_toolbelt/utils/dump.pyr      s    zPrefixSettings.__new__)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   r   r   r      s   c                 C   s<   t | jddƒr8i }| jj}| d¡r,d|d< ||d< |S d S )NÚproxy_managerFzhttps://ZCONNECTÚmethodÚrequest_path)ÚgetattrÚ
connectionr	   ÚurlÚ
startswith)r
   Ú
proxy_infoÚrequest_urlr   r   r   Ú_get_proxy_information   s    
r    c                 C   s   t | ƒd t |ƒ d S )Ns   : ó   
)r   )ÚnameÚvaluer   r   r   Ú_format_header&   s    ÿr$   c                 C   sN   t  | ¡}| d¡}|d k	r$||fS t|jƒ}|jrF|dt|jƒ 7 }||fS )Nr   ó   ?)r   ÚurlparseÚgetr   ÚpathÚquery)r   r   ÚuriÚ	proxy_urlr   r   r   r   Ú_build_request_path+   s    


r,   c                 C   sö   |d kri }|j }t| d| j¡ƒ}t| j|ƒ\}}| || d | d ¡ | j ¡ }t| d|j	¡ƒ}	| |d |	 d ¡ | 
¡ D ]\}
}| |t|
|ƒ ¡ qˆ| |d ¡ | jrèt| jtjƒrÞ| |t| jƒ ¡ n
| d¡ | d¡ d S )Nr   ó    s    HTTP/1.1
ÚHosts   Host: r!   z,<< Request body is not a string-like type >>)r	   r   Úpopr   r,   r   ÚextendÚheadersÚcopyÚnetlocÚitemsr$   ÚbodyÚ
isinstancer   Ú
basestring)r	   ÚprefixesÚbytearrr   Úprefixr   r   r*   r1   Zhost_headerr"   r#   r   r   r   Ú_dump_request_data8   s"    

r;   c           	      C   s¨   |j }| j}t |jd¡}| |d | d t|jƒ d¡ d t	| j
ƒ d ¡ |j}| ¡ D ](}| |¡D ]}| |t||ƒ ¡ qnq`| |d ¡ | | j¡ d S )Nr%   s   HTTP/r-   Úasciir!   )r
   ÚrawÚHTTP_VERSIONSr'   Úversionr0   ÚstrÚstatusÚencoder   Úreasonr1   ÚkeysÚgetlistr$   Úcontent)	r
   r8   r9   r:   r=   Úversion_strr1   r"   r#   r   r   r   Ú_dump_response_dataV   s$    ÿÿþþrH   c                 C   s"   t | tƒst| dƒr|  d¡} | S )NrB   zutf-8)r6   ÚbytesÚhasattrrB   )Údatar   r   r   r   m   s    
r   ó   < ó   > c                 C   sX   |dk	r|nt ƒ }t||ƒ}t| dƒs.tdƒ‚t| ƒ}t| j|||d� t| ||ƒ |S )a  Dump a single request-response cycle's information.

    This will take a response object and dump only the data that requests can
    see for that single request-response cycle.

    Example::

        import requests
        from requests_toolbelt.utils import dump

        resp = requests.get('https://api.github.com/users/sigmavirus24')
        data = dump.dump_response(resp)
        print(data.decode('utf-8'))

    :param response:
        The response to format
    :type response: :class:`requests.Response`
    :param request_prefix: (*optional*)
        Bytes to prefix each line of the request data
    :type request_prefix: :class:`bytes`
    :param response_prefix: (*optional*)
        Bytes to prefix each line of the response data
    :type response_prefix: :class:`bytes`
    :param data_array: (*optional*)
        Bytearray to which we append the request-response cycle data
    :type data_array: :class:`bytearray`
    :returns: Formatted bytes of request and response information.
    :rtype: :class:`bytearray`
    Nr	   z"Response has no associated request)r   )Ú	bytearrayr   rJ   Ú
ValueErrorr    r;   r	   rH   )r
   Úrequest_prefixÚresponse_prefixZ
data_arrayrK   r8   r   r   r   r   r   s   s    


ÿr   c                 C   s>   t ƒ }t| jdd… ƒ}| | ¡ |D ]} t| |||ƒ q&|S )a›  Dump all requests and responses including redirects.

    This takes the response returned by requests and will dump all
    request-response pairs in the redirect history in order followed by the
    final request-response.

    Example::

        import requests
        from requests_toolbelt.utils import dump

        resp = requests.get('https://httpbin.org/redirect/5')
        data = dump.dump_all(resp)
        print(data.decode('utf-8'))

    :param response:
        The response to format
    :type response: :class:`requests.Response`
    :param request_prefix: (*optional*)
        Bytes to prefix each line of the request data
    :type request_prefix: :class:`bytes`
    :param response_prefix: (*optional*)
        Bytes to prefix each line of the response data
    :type response_prefix: :class:`bytes`
    :returns: Formatted bytes of request and response information.
    :rtype: :class:`bytearray`
    N)rN   ÚlistÚhistoryÚappendr   )r
   rP   rQ   rK   rS   r   r   r   r   Ÿ   s    
r   )N)rL   rM   N)rL   rM   )Ú__doc__ÚcollectionsÚrequestsr   Ú__all__r>   Ú
namedtupleZ_PrefixSettingsr   r    r$   r,   r;   rH   r   r   r   r   r   r   r   Ú<module>   s*   ýÿ
  ÿ
,