
ˀX                 @   s   d  Z  d d l Z d d l m Z d  Z d d d d	 d
 d i Z e j d d d g  Z Gd 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 d d  Z d d d d  Z d S)!zGThis module provides functions for dumping information about responses.    N)compatdump_responsedump_all	   s   0.9
   s   1.0   s   1.1PrefixSettingsrequestresponsec                   s"   e  Z d  Z   f d d   Z   S)r   c                s4   t  |  } t  |  } t t |   j |  | |  S)N)_coerce_to_bytessuperr   __new__)clsr	   r
   )	__class__ >/usr/lib/python3/dist-packages/requests_toolbelt/utils/dump.pyr      s    zPrefixSettings.__new__)__name__
__module____qualname__r   r   r   )r   r   r      s   c             C   sR   t  |  j d d  rN i  } |  j j } | j d  r@ d | d <| | d <| Sd  S)NZproxy_managerFzhttps://ZCONNECTmethodrequest_path)getattrZ
connectionr	   url
startswith)r
   
proxy_infoZrequest_urlr   r   r   _get_proxy_information   s    

r   c             C   s   t  |   d t  |  d S)Ns   : s   
)r   )namevaluer   r   r   _format_header&   s    r   c             C   sm   t  j |   } | j d  } | d  k	 r4 | | f St | j  } | j rc | d t | j  7} | | f S)Nr      ?)r   Zurlparsegetr   pathZquery)r   r   uriZ	proxy_urlr   r   r   r   _build_request_path+   s    
	r#   c             C   sO  | d  k r i  } | j  } t | j d |  j   } t |  j |  \ } } | j | | d | d  |  j j   } t | j d | j	   }	 | j | d |	 d  x4 | j
   D]& \ }
 } | j | t |
 |   q W| j | d  |  j r>t |  j t j  r1| j | t |  j   n | j d  | j d  d  S)Nr       s    HTTP/1.1
ZHosts   Host: s   
z,<< Request body is not a string-like type >>)r	   r   popr   r#   r   extendheaderscopyZnetlocitemsr   Zbody
isinstancer   Z
basestring)r	   prefixesbytearrr   prefixr   r   r"   r'   Zhost_headerr   r   r   r   r   _dump_request_data8   s"    		r.   c       	      C   s   | j  } |  j } t j | j d  } | j | d | d t | j  j d  d t	 |  j
  d  | j } xH | j   D]: } x1 | j |  D]  } | j | t | |   q Wq W| j | d  | j |  j  d  S)Nr   s   HTTP/r$   asciis   
)r
   rawHTTP_VERSIONSr    versionr&   strZstatusencoder   reasonr'   keysZgetlistr   Zcontent)	r
   r+   r,   r-   r0   Zversion_strr'   r   r   r   r   r   _dump_response_dataV   s    		>	"r7   c             C   s2   t  |  t  r. t |  d  r. |  j d  }  |  S)Nr4   zutf-8)r*   byteshasattrr4   )datar   r   r   r   m   s    r   s   < s   > c             C   s~   | d k	 r | n t    } t | |  } t |  d  sE 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 requestr   )	bytearrayr   r9   
ValueErrorr   r.   r	   r7   )r
   request_prefixresponse_prefixZ
data_arrayr:   r+   r   r   r   r   r   s   s    c             C   sW   t    } t |  j d d   } | j |   x! | D] }  t |  | | |  q6 W| 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)r;   listhistoryappendr   )r
   r=   r>   r:   r@   r   r   r   r      s    	)zdump_responsezdump_all)__doc__collectionsZrequestsr   __all__r1   
namedtupleZ_PrefixSettingsr   r   r   r#   r.   r7   r   r   r   r   r   r   r   <module>   s$   	+