VY@sdZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z ddlmZmZmZdd lmZdd lmZdd lmZdd lmZmZmZmZdd lmZmZm Z m!Z!m"Z"m#Z#m$Z$ddl%m&Z&m'Z'ddl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=ddl3m>Z?ddl@mAZAeAjBeAjCeAjDeAjEeAjFfZGdZHddZIdZJGdddeKZLGdddeKZMGdddeMZNGdddeLeMZOGdd d eKZPdS)!z` requests.models ~~~~~~~~~~~~~~~ This module contains the primary objects that power Requests. N)UnsupportedOperation) default_hooks)CaseInsensitiveDict) HTTPBasicAuth)cookiejar_from_dictget_cookie_header_copy_cookie_jar) RequestField)encode_multipart_formdata) parse_url) DecodeErrorReadTimeoutError ProtocolErrorLocationParseError) HTTPError MissingSchema InvalidURLChunkedEncodingErrorContentDecodingErrorConnectionErrorStreamConsumedError)to_native_stringunicode_is_ascii) guess_filenameget_auth_from_url requote_uristream_decode_response_unicodeto_key_val_listparse_header_links iter_slicesguess_json_utf super_lencheck_header_validity) cookielib urlunparseurlsplit urlencodestrbytesis_py2chardet builtin_str basestring)json)codes iic@sFeZdZeddZeddZeddZdS)RequestEncodingMixincCsmg}t|j}|j}|s*d}|j||j}|r`|jd|j|dj|S)zBuild the path URL to use./?)r&urlpathappendqueryjoin)selfr6pr7r9r=3/tmp/pip-build-04bmskau/requests/requests/models.pypath_url<s     zRequestEncodingMixin.path_urlcCs t|ttfr|St|dr,|St|drg}xt|D]\}}t|tsyt|d r|g}xi|D]a}|dk r|jt|tr|jdn|t|tr|jdn|fqWqNWt|ddS|SdS)zEncode parameters in a piece of data. Will successfully encode parameters when passed as a dict or a list of 2-tuples. Order is retained if data is a list of 2-tuples but arbitrary if parameters are supplied as a dict. read__iter__Nzutf-8doseqT) isinstancer(r)hasattrrr-r8encoder')dataresultkvsvr=r=r>_encode_paramsQs    !0z#RequestEncodingMixin._encode_paramsc CsQ|stdnt|tr0tdg}t|pBi}t|pTi}x|D]\}}t|tst|d r|g}x|D]|}|dk rt|tst|}|jt|tr|jdn|t|tr|j dn|fqWqaWx|D] \}}d}d} t|t t frt |dkru|\} } qt |dkr|\} } }q|\} } }} nt |p|} |} t| tttfr| } n | j} td|d | d | d | } | jd ||j| q'Wt|\}}||fS) aBuild the body for a multipart/form-data request. Will successfully encode files when passed as a dict or a list of tuples. Order is retained if data is a list of tuples but arbitrary if parameters are supplied as a dict. The tuples may be 2-tuples (filename, fileobj), 3-tuples (filename, fileobj, contentype) or 4-tuples (filename, fileobj, contentype, custom_headers). zFiles must be provided.zData must not be a string.rANzutf-8namerFfilenameheaders content_type) ValueErrorrCr-rrDr)r(r8decoderEtuplelistlenr bytearrayr@r make_multipartr )filesrF new_fieldsfieldsfieldvalrJrHftfhfnfpfdatarfbodyrQr=r=r> _encode_fileslsH      !0  !z"RequestEncodingMixin._encode_filesN)__name__ __module__ __qualname__propertyr? staticmethodrKrer=r=r=r>r2;s r2c@s(eZdZddZddZdS)RequestHooksMixincCs|||jkrtd|t|tjrH|j|j|n0t|drx|j|jdd|DdS)zProperly register a hook.z1Unsupported event specified, with event name "%s"rAcss'|]}t|tjr|VqdS)N)rC collectionsCallable).0hr=r=r> sz2RequestHooksMixin.register_hook..N)hooksrRrCrlrmr8rDextend)r;eventhookr=r=r> register_hooks zRequestHooksMixin.register_hookc Cs9y|j|j|dSWntk r4dSYnXdS)ziDeregister a previously registered hook. Returns True if the hook existed, False if not. TFN)rqremoverR)r;rsrtr=r=r>deregister_hooks  z!RequestHooksMixin.deregister_hookN)rfrgrhrurwr=r=r=r>rks  rkc @sXeZdZdZdddddddddddd ZddZddZdS) RequestaA user-created :class:`Request ` object. Used to prepare a :class:`PreparedRequest `, which is sent to the server. :param method: HTTP method to use. :param url: URL to send. :param headers: dictionary of headers to send. :param files: dictionary of {filename: fileobject} files to multipart upload. :param data: the body to attach to the request. If a dictionary is provided, form-encoding will take place. :param json: json for the body to attach to the request (if files or data is not specified). :param params: dictionary of URL parameters to append to the URL. :param auth: Auth handler or (user, pass) tuple. :param cookies: dictionary or CookieJar of cookies to attach to this request. :param hooks: dictionary of callback hooks, for internal usage. Usage:: >>> import requests >>> req = requests.Request('GET', 'http://httpbin.org/get') >>> req.prepare() Nc Cs|dkrgn|}|dkr*gn|}|dkrBin|}|dkrZin|}| dkrrin| } t|_x6t| jD]"\} } |jd| d| qW||_||_||_||_||_ | |_ ||_ ||_ ||_ dS)Nrsrt)rrqrUitemsrumethodr6rPrYrFr.paramsauthcookies) r;rzr6rPrYrFr{r|r}rqr.rHrJr=r=r>__init__s"         zRequest.__init__cCs d|jS)Nz)rz)r;r=r=r>__repr__szRequest.__repr__cCsqt}|jd|jd|jd|jd|jd|jd|jd|jd|j d |j d |j |S) zXConstructs a :class:`PreparedRequest ` for transmission and returns it.rzr6rPrYrFr.r{r|r}rq) PreparedRequestpreparerzr6rPrYrFr.r{r|r}rq)r;r<r=r=r>rs            zRequest.prepare)rfrgrh__doc__r~rrr=r=r=r>rxs   rxc @seZdZdZddZdddddddddddd ZddZd d Zd d Ze d dZ ddZ ddZ dddZ ddZdddZddZddZdS)raThe fully mutable :class:`PreparedRequest ` object, containing the exact bytes that will be sent to the server. Generated from either a :class:`Request ` object or manually. Usage:: >>> import requests >>> req = requests.Request('GET', 'http://httpbin.org/get') >>> r = req.prepare() >>> s = requests.Session() >>> s.send(r) cCsFd|_d|_d|_d|_d|_t|_d|_dS)N)rzr6rP_cookiesrdrrq_body_position)r;r=r=r>r~s      zPreparedRequest.__init__Nc Csk|j||j|||j||j||j||| |j|||j| dS)z6Prepares the entire request with the given parameters.N)prepare_method prepare_urlprepare_headersprepare_cookies prepare_body prepare_auth prepare_hooks) r;rzr6rPrYrFr{r|r}rqr.r=r=r>r)s   zPreparedRequest.preparecCs d|jS)Nz)rz)r;r=r=r>r:szPreparedRequest.__repr__cCst}|j|_|j|_|jdk r?|jjnd|_t|j|_|j|_|j|_|j |_ |S)N) rrzr6rPcopyr rrdrqr)r;r<r=r=r>r=s   '   zPreparedRequest.copycCs4||_|jdk r0t|jj|_dS)zPrepares the given HTTP method.N)rzrupper)r;rzr=r=r>rHs zPreparedRequest.prepare_methodcCsyddlm}Wn+tk rAddl}|tjd_get_idna_encoded_hostNs  " z&PreparedRequest._get_idna_encoded_hostcCst|tr!|jd}ntr3t|n t|}|j}d|krz|jjd rz||_ dSy%t |\}}}}}}} Wn4t k r} zt | j WYdd} ~ XnX|sd} | jt|d} t| |st d|t|s_y|j|}Wqztk r[t dYqzXn|jdrzt d|pd } | r| d 7} | |7} |r| dt|7} |sd }trbt|tr|jd }t| tr| jd } t|tr&|jd }t|trD|jd }t| trb| jd } t|ttfrt|}|j|} | r|rd || f}n| }tt|| |d|| g}||_ dS)zPrepares the given HTTP URL.utf8:httpNzDInvalid URL {0!r}: No schema supplied. Perhaps you meant http://{0}?z Invalid URL %r: No host suppliedzURL has an invalid label.*r5@r3zutf-8z%s&%s)rCr)rSr*unicoder(lstriplower startswithr6r rrargsformatrrrrrrErKrr%)r;r6r{schemer|rportr7r9fragmenteerrornetloc enc_paramsr=r=r>r^sh " %"        $zPreparedRequest.prepare_urlcCsVt|_|rRx=|jD]/}t||\}}||jt|rs    zPreparedRequest.prepare_headerscCsd}d}| rR|dk rRd}tj|}t|tsR|jd}tt|dt|ttt t j f g}yt |}Wn!t ttfk rd}YnX|rV|}t|dddk ry|j|_Wn$ttfk rt|_YnX|r*td|rFt||jdrsJ %     zPreparedRequest.prepare_bodycCsl|dk r4t|}|rht||jdPrepare Content-Length header based on request method and bodyNzContent-LengthGETHEAD0)zGETzHEAD)r"r,rPrzget)r;rdrr=r=r>rs   'z&PreparedRequest.prepare_content_lengthr5cCs|dkr3t|j}t|r-|nd}|rt|trft|dkrft|}||}|jj|j|j |j dS)z"Prepares the given HTTP auth data.NrL) rr6anyrCrTrVr__dict__updaterrd)r;r|r6url_authrr=r=r>rs !  zPreparedRequest.prepare_authcCs\t|tjr||_nt||_t|j|}|dk rX||jd` object. Any subsequent calls to ``prepare_cookies`` will have no actual effect, unless the "Cookie" header is removed beforehand. NCookie)rCr$ CookieJarrrrrP)r;r} cookie_headerr=r=r>r's   zPreparedRequest.prepare_cookiescCs5|p g}x"|D]}|j|||qWdS)zPrepares the given hooks.N)ru)r;rqrsr=r=r>r;s  zPreparedRequest.prepare_hooks)rfrgrhrr~rrrrrjrrrrrrrrr=r=r=r>rs      V E  rc s]eZdZdZdddddddd d d g Zfd d ZddZddZddZddZ ddZ ddZ e ddZ e ddZe ddZe d d!Zd"d#d$d%Zed&d&d'd(Ze d)d*Ze d+d,Zd-d.Ze d/d0Zd1d2Zd3d4ZS)5ResponsezhThe :class:`Response ` object, which contains a server's response to an HTTP request. _content status_coderPr6historyencodingreasonr}elapsedrequestcstt|jd|_d|_d|_t|_d|_d|_ d|_ g|_ d|_ t i|_tjd|_d|_dS)NFr)superrr~r_content_consumedrrrPrawr6rrrrr}datetime timedeltarr)r;) __class__r=r>r~Os         zResponse.__init__cs0jsjtfddjDS)Nc3s'|]}|t|dfVqdS)N)r)rnattr)r;r=r>rpsz(Response.__getstate__..)rcontentdict __attrs__)r;r=)r;r> __getstate__s  zResponse.__getstate__cCsQx*|jD]\}}t|||q Wt|ddt|dddS)NrTr)rysetattr)r;staterNrr=r=r> __setstate__szResponse.__setstate__cCs d|jS)Nz)r)r;r=r=r>rszResponse.__repr__cCs|jS)akReturns True if :attr:`status_code` is less than 400. This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If the status code, is between 200 and 400, this will return True. This is **not** a check to see if the response code is ``200 OK``. )ok)r;r=r=r>__bool__szResponse.__bool__cCs|jS)akReturns True if :attr:`status_code` is less than 400. This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If the status code, is between 200 and 400, this will return True. This is **not** a check to see if the response code is ``200 OK``. )r)r;r=r=r> __nonzero__szResponse.__nonzero__cCs |jdS)z,Allows you to use a response as an iterator.) iter_content)r;r=r=r>rAszResponse.__iter__c Cs+y|jWntk r&dSYnXdS)akReturns True if :attr:`status_code` is less than 400. This attribute checks if the status code of the response is between 400 and 600 to see if there was a client error or a server error. If the status code, is between 200 and 400, this will return True. This is **not** a check to see if the response code is ``200 OK``. FT)raise_for_statusr)r;r=r=r>rs   z Response.okcCsd|jko|jtkS)zTrue if this Response is a well-formed HTTP redirect that could have been processed automatically (by :meth:`Session.resolve_redirects`). location)rPrREDIRECT_STATI)r;r=r=r> is_redirectszResponse.is_redirectcCs(d|jko'|jtjtjfkS)z?True if this Response one of the permanent versions of redirectr)rPrr/moved_permanentlypermanent_redirect)r;r=r=r>is_permanent_redirectszResponse.is_permanent_redirectcCstj|jdS)z6The apparent encoding, provided by the chardet libraryr)r+detectr)r;r=r=r>apparent_encodingszResponse.apparent_encodingrFcsfdd}jr<tjtr<tn2dk rntt rntdttj}|}jr|n|}|rt |}|S)aIterates over the response data. When stream=True is set on the request, this avoids reading the content at once into memory for large responses. The chunk size is the number of bytes it should read into memory. This is not necessarily the length of each item returned as decoding can take place. chunk_size must be of type int or None. A value of None will function differently depending on the value of `stream`. stream=True will read data as it arrives in whatever size the chunks are received. If stream=False, data is returned as a single chunk. If decode_unicode is True, content will be decoded using the best available encoding based on the response. c3stjdry,x%jjddD] }|Vq.WWqtk rq}zt|WYdd}~Xqtk r}zt|WYdd}~Xqtk r}zt|WYdd}~XqXn%x"jj }|sP|VqWd_ dS)Nstreamdecode_contentT) rDrrrrr rrrr@r)chunkr) chunk_sizer;r=r>generates  " z'Response.iter_content..generateNz.chunk_size must be an int, it is instead a %s.) rrCrboolrintrtyper r)r;rdecode_unicoder reused_chunks stream_chunkschunksr=)rr;r>rs  zResponse.iter_contentNccsd}x|jd|d|D]}|dk r;||}|rS|j|}n |j}|r|dr|r|dd|dkr|j}nd}x|D] }|VqWqW|dk r|VdS) zIterates over the response data, one line at a time. When stream=True is set on the request, this avoids reading the content at once into memory for large responses. .. note:: This method is not reentrant safe. Nrrrrrr)rsplit splitlinespop)r;rr delimiterpendingrlinesliner=r=r> iter_liness   .   zResponse.iter_linescCs|jdkru|jr$td|jdksB|jdkrNd|_n'tj|jtpot|_d|_|jS)z"Content of the response, in bytes.Fz2The content for this response was already consumedrNT) rr RuntimeErrorrrr)r:rCONTENT_CHUNK_SIZE)r;r=r=r>r#s   ' zResponse.contentc Csd}|j}|js"tdS|jdkr:|j}yt|j|dd}Wn-ttfk rt|jdd}YnX|S)aContent of the response, in unicode. If Response.encoding is None, encoding will be guessed using ``chardet``. The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set ``r.encoding`` appropriately before accessing this property. Nr5errorsreplace)rrr(r LookupErrorr)r;rrr=r=r>text7s    z Response.textc Ks|j rx|jrxt|jdkrxt|j}|dk rxy tj|jj||SWntk rwYnXtj|j|S)zReturns the json-encoded content of a response, if any. :param \*\*kwargs: Optional arguments that ``json.loads`` takes. :raises ValueError: If the response body does not contain valid json. rMN) rrrVr!rloadsrSUnicodeDecodeErrorr)r;kwargsrr=r=r>r.]s(  z Response.jsoncCsg|jjd}i}|rct|}x6|D].}|jdpR|jd}|||r ws  zResponse.linksc Csd}t|jtrXy|jjd}Wqatk rT|jjd}YqaXn |j}d|jko{dknrd|j||jf}n8d|jkodknrd|j||jf}|rt|d |d S) z2Raises stored :class:`HTTPError`, if one occurred.r5zutf-8z iso-8859-1iiz%s Client Error: %s for url: %siXz%s Server Error: %s for url: %sresponseN)rCrr)rSrrr6r)r;http_error_msgrr=r=r>rs  zResponse.raise_for_statuscCsB|js|jjt|jdd}|dk r>|dS)zReleases the connection back to the pool. Once this method has been called the underlying ``raw`` object must not be accessed again. *Note: Should not normally need to be called explicitly.* release_connN)rrcloser)r;rr=r=r>rs    zResponse.close)rfrgrhrrr~rrrrrrArirrrrrITER_CHUNK_SIZErrrr.r rrr=r=)rr>rEs, 0   7&  r)QrrlrrZencodings.idna encodingsiorrqr structuresrr|rr}rrr Zpackages.urllib3.fieldsr Zpackages.urllib3.filepostr Zpackages.urllib3.utilr Zpackages.urllib3.exceptionsr rrr exceptionsrrrrrrrZ_internal_utilsrrutilsrrrrrrr r!r"r#compatr$r%r&r'r(r)r*r+r,r-r.r status_codesr/movedfoundothertemporary_redirectrrDEFAULT_REDIRECT_LIMITrrrr2rkrxrrr=r=r=r>sB    "4FF  nE@