3 P\Z@sdZddlZddlmZdZdddd Zejd d d gZGd d d eZddZ ddZ ddZ dddZ ddZ ddZd ddZd!ddZdS)"zGThis module provides functions for dumping information about responses.N)compat dump_responsedump_alls0.9s1.0s1.1) PrefixSettingsrequestresponsecseZdZfddZZS)rcs$t|}t|}tt|j|||S)N)_coerce_to_bytessuperr__new__)clsr r ) __class__/usr/lib/python3.6/dump.pyr szPrefixSettings.__new__)__name__ __module__ __qualname__r __classcell__rr)rrrscCs<t|jddr8i}|jj}|jdr,d|d<||d<|SdS)NZ proxy_managerFzhttps://ZCONNECTmethod request_path)getattrZ connectionr url startswith)r proxy_infoZ request_urlrrr_get_proxy_informations rcCst|dt|dS)Ns: s )r )namevaluerrr_format_header&srcCsVtj|}|jd}|dk r,t|}||fSt|j}|jrN|dt|j7}||fS)Nr?)rZurlparsegetr pathZquery)rruriZ proxy_urlrrrr_build_request_path+s   r$c Cs|dkr i}|j}t|jd|j}t|j|\}}|j||d|d|jj}t|jd|j } |j|d| dx(|j D]\} } |j|t | | qW|j|d|j rt |j tjr|j|t|j n |jd|jddS)Nr s HTTP/1.1 ZHostsHost: s s,<< Request body is not a string-like type >>)r r poprr$rextendheaderscopyZnetlocitemsrZbody isinstancerZ basestring) r prefixesbytearrrprefixrrr#r(Z host_headerrrrrr_dump_request_data9s"  r/c Cs|j}|j}tj|jd}|j|d|dt|jjddt |j d|j }x8|j D],}x&|j |D]}|j|t||qrWqbW|j|d|j|jdS)Nr sHTTP/r%asciis )r raw HTTP_VERSIONSr!versionr'strZstatusencoder reasonr(keysZgetlistrZcontent) r r,r-r.r1Z version_strr(rrrrr_dump_response_dataWs4r8cCs0t|t r t|dr |jd}|dk r,|SdS)Nr5zutf-8)r+byteshasattrr5)datarrrr ns r < > cCsX|dk r |nt}t||}t|ds.tdt|}t|j|||dt||||S)aDump 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) bytearrayrr; ValueErrorrr/r r8)r request_prefixresponse_prefixZ data_arrayr<r,rrrrrus    cCsBt}t|jdd}|j|x|D]}t||||q(W|S)aDump 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?listhistoryappendr)r rArBr<rDrrrrs   )rr)N)r=r>N)r=r>)__doc__ collectionsZrequestsr__all__r2 namedtupleZ_PrefixSettingsrrrr$r/r8r rrrrrrs$     +