a Æ*Na+ˆã@sÎdZddlZddlZddlZddlmZddlmZddl m Z ddl m Z m Z mZmZddlmZdd lmZdd lmZdd lmZdd lmZmZmZdd lmZmZmZm 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;mZ>m?Z?ddl3m@ZAddlBmCZCeCjDeCjEeCjFeCjGeCjHfZIdZJdZKdZLGdd„deMƒZNGdd„deMƒZOGdd„deOƒZPGdd„deNeOƒZQGdd„deMƒZRdS) z` requests.models ~~~~~~~~~~~~~~~ This module contains the primary objects that power Requests. éN)Ú RequestField)Úencode_multipart_formdata)Ú parse_url)Ú DecodeErrorÚReadTimeoutErrorÚ ProtocolErrorÚLocationParseError)ÚUnsupportedOperationé)Ú default_hooks)ÚCaseInsensitiveDict)Ú HTTPBasicAuth)Úcookiejar_from_dictÚget_cookie_headerÚ_copy_cookie_jar)Ú HTTPErrorÚ MissingSchemaÚ InvalidURLÚChunkedEncodingErrorÚContentDecodingErrorÚConnectionErrorÚStreamConsumedErrorÚInvalidJSONError)Úto_native_stringÚunicode_is_ascii) Úguess_filenameÚget_auth_from_urlÚ requote_uriÚstream_decode_response_unicodeÚto_key_val_listÚparse_header_linksÚ iter_slicesÚguess_json_utfÚ super_lenÚcheck_header_validity) ÚCallableÚMappingÚ cookielibÚ urlunparseÚurlsplitÚ urlencodeÚstrÚbytesÚis_py2ÚchardetÚ builtin_strÚ basestring)Újson)Úcodeséi(ic@s0eZdZedd„ƒZedd„ƒZedd„ƒZdS)ÚRequestEncodingMixincCsNg}t|jƒ}|j}|sd}| |¡|j}|rD| d¡| |¡d |¡S)zBuild the path URL to use.ú/ú?Ú)r)ÚurlÚpathÚappendÚqueryÚjoin)Úselfr8Úpr9r;©r?új/private/var/folders/js/6pj4vh5d4zd0k6bxv74qrbhr0000gr/T/pip-target-22xwyzbs/lib/python/requests/models.pyÚpath_url=s    zRequestEncodingMixin.path_urlcCs¶t|ttfƒr|St|dƒr |St|dƒr®g}t|ƒD]j\}}t|tƒsRt|dƒsX|g}|D]B}|dur\| t|tƒr€| d¡n|t|tƒr–| d¡n|f¡q\q6t|dd�S|SdS)züEncode 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__Núutf-8T)Údoseq) Ú isinstancer+r,Úhasattrrr0r:Úencoder*)ÚdataÚresultÚkÚvsÚvr?r?r@Ú_encode_paramsRs$   ÿÿ z#RequestEncodingMixin._encode_paramscCs®|stdƒ‚nt|tƒr tdƒ‚g}t|p,iƒ}t|p8iƒ}|D]|\}}t|tƒs\t|dƒsb|g}|D]T}|durft|tƒs„t|ƒ}| t|tƒrœ| d¡n|t|tƒr²|  d¡n|f¡qfq@|D]Ö\}}d}d} t|t t fƒ�r t |ƒdkrø|\} } n&t |ƒdk�r|\} } }n |\} } }} nt |ƒ�p,|} |} t| tttfƒ�rJ| } n(t| dƒ�r`|  ¡} n| du�rnqÂn| } t|| | | d �} | j|d �| | ¡qÂt|ƒ\}}||fS) a¥Build 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.rCNrDéérB)ÚnamerIÚfilenameÚheaders)Ú content_type)Ú ValueErrorrFr0rrGr,r+r:ÚdecoderHÚtupleÚlistÚlenrÚ bytearrayrBrÚmake_multipartr)ÚfilesrIÚ new_fieldsÚfieldsÚfieldÚvalrMrKÚftÚfhÚfnÚfpÚfdataÚrfÚbodyrTr?r?r@Ú _encode_filesmsT       ÿÿ          z"RequestEncodingMixin._encode_filesN)Ú__name__Ú __module__Ú __qualname__ÚpropertyrAÚ staticmethodrNrhr?r?r?r@r4<s   r4c@seZdZdd„Zdd„ZdS)ÚRequestHooksMixincCsZ||jvrtd|ƒ‚t|tƒr2|j| |¡n$t|dƒrV|j| dd„|Dƒ¡dS)zProperly register a hook.z1Unsupported event specified, with event name "%s"rCcss|]}t|tƒr|VqdS©N)rFr%)Ú.0Úhr?r?r@Ú ¸óz2RequestHooksMixin.register_hook..N)ÚhooksrUrFr%r:rGÚextend©r=ÚeventÚhookr?r?r@Ú register_hook¯s     zRequestHooksMixin.register_hookcCs0z|j| |¡WdSty*YdS0dS)ziDeregister a previously registered hook. Returns True if the hook existed, False if not. TFN)rtÚremoverUrvr?r?r@Úderegister_hookºs  z!RequestHooksMixin.deregister_hookN)rirjrkryr{r?r?r?r@rn®s rnc @s*eZdZdZd dd„Zdd„Zdd„ZdS) ÚRequesta{A 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 or list of tuples ``[(key, value)]`` 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: URL parameters to append to the URL. If a dictionary or list of tuples ``[(key, value)]`` is provided, form-encoding will take place. :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', 'https://httpbin.org/get') >>> req.prepare() Nc Cs¶|dur gn|}|durgn|}|dur,in|}|dur©r~©r=r?r?r@Ú__repr__ûszRequest.__repr__c Cs<tƒ}|j|j|j|j|j|j|j|j|j |j |j d� |S)zXConstructs a :class:`PreparedRequest ` for transmission and returns it.) r~r8rSr\rIr1rr€r�rt) ÚPreparedRequestÚpreparer~r8rSr\rIr1rr€r�rt©r=r>r?r?r@r‡þsö zRequest.prepare) NNNNNNNNNN)rirjrkÚ__doc__r‚r…r‡r?r?r?r@r|Æs þ r|c @s‚eZdZdZdd„Zddd„Zdd„Zd d „Zd d „Ze d d„ƒZ dd„Z dd„Z ddd„Z dd„Zd dd„Zdd„Zdd„ZdS)!r†a)The fully mutable :class:`PreparedRequest ` object, containing the exact bytes that will be sent to the server. Instances are generated from a :class:`Request ` object, and should not be instantiated manually; doing so may produce undesirable effects. Usage:: >>> import requests >>> req = requests.Request('GET', 'https://httpbin.org/get') >>> r = req.prepare() >>> r >>> s = requests.Session() >>> s.send(r) cCs0d|_d|_d|_d|_d|_tƒ|_d|_dSro)r~r8rSÚ_cookiesrgr rtÚ_body_positionr„r?r?r@r‚%szPreparedRequest.__init__Nc CsR| |¡| ||¡| |¡| |¡| ||| ¡| ||¡| | ¡dS)z6Prepares the entire request with the given parameters.N)Úprepare_methodÚ prepare_urlÚprepare_headersÚprepare_cookiesÚ prepare_bodyÚ prepare_authÚ prepare_hooks) r=r~r8rSr\rIrr€r�rtr1r?r?r@r‡6s     zPreparedRequest.preparecCs d|jS)Nzrƒr„r?r?r@r…HszPreparedRequest.__repr__cCsXtƒ}|j|_|j|_|jdur*|j ¡nd|_t|jƒ|_|j|_|j|_|j |_ |Sro) r†r~r8rSÚcopyrrŠrgrtr‹rˆr?r?r@r“Ks zPreparedRequest.copycCs$||_|jdur t|j ¡ƒ|_dS)zPrepares the given HTTP method.N)r~rÚupper)r=r~r?r?r@rŒVs zPreparedRequest.prepare_methodcCs>ddl}z|j|dd� d¡}Wn|jy8t‚Yn0|S)NrT)Úuts46rD)ÚidnarHrVÚ IDNAErrorÚ UnicodeError)Úhostr–r?r?r@Ú_get_idna_encoded_host\s  z&PreparedRequest._get_idna_encoded_hostc Cs,t|tƒr| d¡}ntr"t|ƒnt|ƒ}| ¡}d|vrR| ¡ d¡sR||_ dSzt |ƒ\}}}}}}} Wn.t yš} zt | j Ž‚WYd} ~ n d} ~ 00|s¼d} |  t|dƒ¡} t| ƒ‚|sÌt d|ƒ‚t|ƒ�sz| |¡}Wntyþt dƒ‚Yn0n| d¡�rt dƒ‚|�pd } | �r.| d 7} | |7} |�rL| dt|ƒ7} |�sVd }t�rÊt|tƒ�rr| d ¡}t| tƒ�rˆ|  d ¡} t|tƒ�rž| d ¡}t|tƒ�r´| d ¡}t| tƒ�rÊ|  d ¡} t|ttfƒ�rât|ƒ}| |¡} | �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.Ú*r7ú@r5rDz%s&%s)rFr,rVr-Úunicoder+ÚlstripÚlowerÚ startswithr8rrrÚargsÚformatrrrršr˜rHrNrr()r=r8rÚschemer€r™Úportr9r;ÚfragmentÚeÚerrorÚnetlocÚ enc_paramsr?r?r@r�fsh                   zPreparedRequest.prepare_urlcCs<tƒ|_|r8| ¡D]"}t|ƒ|\}}||jt|ƒ<qdS)z Prepares the given HTTP headers.N)r rSr}r$r)r=rSÚheaderrQÚvaluer?r?r@r޼s  zPreparedRequest.prepare_headersc Cs²d}d}|sp|durpd}ztj|dd�}Wn0tyZ}zt||d�‚WYd}~n d}~00t|tƒsp| d¡}tt|dƒt|t t t t fƒ gƒ}|�r8z t |ƒ}WntttfyÀd}Yn0|}t|ddƒdu�rz| ¡|_Wn ttf�ytƒ|_Yn0|�rtd ƒ‚|�r,t|ƒ|jd <n d |jd <np|�rP| ||¡\}}n2|�r‚| |¡}t|t ƒ�sxt|d ƒ�r~d}nd}| |¡|�r¨d|jv�r¨||jd<||_dS)z"Prepares the given HTTP body data.Nzapplication/jsonF)Ú allow_nan)ÚrequestrDrCÚtellz1Streamed bodies and files are mutually exclusive.úContent-LengthÚchunkedzTransfer-EncodingrBz!application/x-www-form-urlencodedz content-typez Content-Type)Ú complexjsonÚdumpsrUrrFr,rHÚallrGr0rXrWr&r#Ú TypeErrorÚAttributeErrorr Úgetattrr±r‹ÚIOErrorÚOSErrorÚobjectÚNotImplementedErrorr/rSrhrNÚprepare_content_lengthrg) r=rIr\r1rgrTÚveÚ is_streamÚlengthr?r?r@r�ÇsR "  þ      zPreparedRequest.prepare_bodycCsL|dur$t|ƒ}|rHt|ƒ|jd<n$|jdvrH|j d¡durHd|jd<dS)z>Prepare Content-Length header based on request method and bodyNr²)ÚGETÚHEADÚ0)r#r/rSr~Úget)r=rgrÁr?r?r@r¾s z&PreparedRequest.prepare_content_lengthr7cCsj|dur"t|jƒ}t|ƒr|nd}|rft|tƒrDt|ƒdkrDt|Ž}||ƒ}|j |j¡|  |j ¡dS)z"Prepares the given HTTP auth data.NrO) rr8ÚanyrFrWrYr Ú__dict__Úupdater¾rg)r=r€r8Úurl_authÚrr?r?r@r‘s zPreparedRequest.prepare_authcCs@t|tjƒr||_n t|ƒ|_t|j|ƒ}|dur<||jd<dS)aPrepares the given HTTP cookie data. This function eventually generates a ``Cookie`` header from the given cookies using cookielib. Due to cookielib's design, the header will not be regenerated if it already exists, meaning this function can only be called once for the life of the :class:`PreparedRequest ` object. Any subsequent calls to ``prepare_cookies`` will have no actual effect, unless the "Cookie" header is removed beforehand. NÚCookie)rFr'Ú CookieJarrŠrrrS)r=r�Ú cookie_headerr?r?r@r�4s   zPreparedRequest.prepare_cookiescCs&|pg}|D]}| |||¡q dS)zPrepares the given hooks.N)ry)r=rtrwr?r?r@r’HszPreparedRequest.prepare_hooks) NNNNNNNNNN)N)r7)rirjrkr‰r‚r‡r…r“rŒrmršr�rŽr�r¾r‘r�r’r?r?r?r@r†s"þ   V J r†c@sòeZdZdZgd¢Zdd„Zdd„Zdd„Zd d „Zd d „Z d d„Z dd„Z dd„Z dd„Z edd„ƒZedd„ƒZedd„ƒZedd„ƒZedd„ƒZd2d!d"„Zed d#fd$d%„Zed&d'„ƒZed(d)„ƒZd*d+„Zed,d-„ƒZd.d/„Zd0d1„Zd#S)3ÚResponsezhThe :class:`Response ` object, which contains a server's response to an HTTP request. ) Ú_contentÚ status_coderSr8ÚhistoryÚencodingÚreasonr�Úelapsedr°cCs^d|_d|_d|_d|_tƒ|_d|_d|_d|_g|_ d|_ t iƒ|_ t  d¡|_d|_dS)NFr)rÏÚ_content_consumedÚ_nextrÐr rSÚrawr8rÒrÑrÓrr�ÚdatetimeÚ timedeltarÔr°r„r?r?r@r‚\s  zResponse.__init__cCs|Sror?r„r?r?r@Ú __enter__‹szResponse.__enter__cGs | ¡dSro)Úclose)r=r¤r?r?r@Ú__exit__ŽszResponse.__exit__cs ˆjs ˆj‡fdd„ˆjDƒS)Ncsi|]}|tˆ|dƒ“qSro)r¹)rpÚattrr„r?r@Ú —rsz)Response.__getstate__..)rÕÚcontentÚ __attrs__r„r?r„r@Ú __getstate__‘szResponse.__getstate__cCs:| ¡D]\}}t|||ƒqt|ddƒt|ddƒdS)NrÕTr×)r}Úsetattr)r=ÚstaterQr®r?r?r@Ú __setstate__™s zResponse.__setstate__cCs d|jS)Nz)rÐr„r?r?r@r…¡szResponse.__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``. ©Úokr„r?r?r@Ú__bool__¤szResponse.__bool__cCs|jSrårær„r?r?r@Ú __nonzero__®szResponse.__nonzero__cCs | d¡S)z,Allows you to use a response as an iterator.é€)Ú iter_contentr„r?r?r@rC¸szResponse.__iter__cCs&z | ¡Wnty YdS0dS)axReturns True if :attr:`status_code` is less than 400, False if not. 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_statusrr„r?r?r@rç¼s   z Response.okcCsd|jvo|jtvS)zšTrue if this Response is a well-formed HTTP redirect that could have been processed automatically (by :meth:`Session.resolve_redirects`). Úlocation)rSrÐÚREDIRECT_STATIr„r?r?r@Ú is_redirectËszResponse.is_redirectcCsd|jvo|jtjtjfvS)z@True if this Response one of the permanent versions of redirect.rí)rSrÐr2Úmoved_permanentlyÚpermanent_redirectr„r?r?r@Úis_permanent_redirectÒszResponse.is_permanent_redirectcCs|jS)zTReturns a PreparedRequest for the next request in a redirect chain, if there is one.)rÖr„r?r?r@Únext×sz Response.nextcCst |j¡dS)zOThe apparent encoding, provided by the charset_normalizer or chardet libraries.rÒ)r.Údetectrßr„r?r?r@Úapparent_encodingÜszResponse.apparent_encodingr Fcs|‡‡fdd„}ˆjr(tˆjtƒr(tƒ‚n"ˆdurJtˆtƒsJtdtˆƒƒ‚tˆjˆƒ}|ƒ}ˆjrf|n|}|rxt |ˆƒ}|S)aøIterates 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. c 3sÔtˆjdƒr°z ˆjjˆdd�D] }|VqWqÊtyX}zt|ƒ‚WYd}~qÊd}~0ty‚}zt|ƒ‚WYd}~qÊd}~0ty¬}zt|ƒ‚WYd}~qÊd}~00nˆj  ˆ¡}|sÂqÊ|Vq°dˆ_ dS)NÚstreamT)Údecode_content) rGr×rörrrrrrrBrÕ)Úchunkr©©Ú chunk_sizer=r?r@Úgenerateòs    z'Response.iter_content..generateNz.chunk_size must be an int, it is instead a %s.) rÕrFrÏÚboolrÚintr·Útyper!r)r=rúÚdecode_unicoderûÚ reused_chunksÚ stream_chunksÚchunksr?rùr@rëás  zResponse.iter_contentNccs”d}|j||d�D]n}|dur&||}|r6| |¡}n| ¡}|rl|drl|rl|dd|dkrl| ¡}nd}|D] }|Vqtq|dur�|VdS)zõIterates 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. N)rúrÿéÿÿÿÿ)rëÚsplitÚ splitlinesÚpop)r=rúrÿÚ delimiterÚpendingrøÚlinesÚliner?r?r@Ú iter_liness $  zResponse.iter_linescCsV|jdurJ|jrtdƒ‚|jdks,|jdur4d|_nd | t¡¡pFd|_d|_|jS)z"Content of the response, in bytes.Fz2The content for this response was already consumedrNrsT)rÏrÕÚ RuntimeErrorrÐr×r<rëÚCONTENT_CHUNK_SIZEr„r?r?r@rß7s ÿzResponse.contentc Csfd}|j}|jstdƒS|jdur(|j}zt|j|dd�}Wn$ttfy`t|jdd�}Yn0|S)aÚContent of the response, in unicode. If Response.encoding is None, encoding will be guessed using ``charset_normalizer`` or ``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. Nr7Úreplace)Úerrors)rÒrßr+rõÚ LookupErrorr·)r=rßrÒr?r?r@ÚtextKs z Response.textcKsp|js\|jr\t|jƒdkr\t|jƒ}|dur\ztj|j |¡fi|¤ŽWStyZYn0tj|jfi|¤ŽS)a.Returns the json-encoded content of a response, if any. :param \*\*kwargs: Optional arguments that ``json.loads`` takes. :raises simplejson.JSONDecodeError: If the response body does not contain valid json and simplejson is installed. :raises json.JSONDecodeError: If the response body does not contain valid json and simplejson is not installed on Python 3. :raises ValueError: If the response body does not contain valid json and simplejson is not installed on Python 2. rPN) rÒrßrYr"r´ÚloadsrVÚUnicodeDecodeErrorr)r=ÚkwargsrÒr?r?r@r1qs   ÿÿ z Response.jsoncCsF|j d¡}i}|rBt|ƒ}|D] }| d¡p6| d¡}|||<q |S)z8Returns the parsed header links of the response, if any.ÚlinkÚrelr8)rSrÅr )r=r­ÚlÚlinksrÚkeyr?r?r@r�s  zResponse.linkscCs¶d}t|jtƒrBz|j d¡}WqHty>|j d¡}YqH0n|j}d|jkr^dkrvnnd|j||jf}n,d|jkrŒdkr¢nnd|j||jf}|r²t||d �‚d S) z+Raises :class:`HTTPError`, if one occurred.r7rDz iso-8859-1i�iôz%s Client Error: %s for url: %siXz%s Server Error: %s for url: %s)ÚresponseN)rFrÓr,rVrrÐr8r)r=Úhttp_error_msgrÓr?r?r@rì¢s  zResponse.raise_for_statuscCs0|js|j ¡t|jddƒ}|dur,|ƒdS)zØReleases 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)rÕr×rÛr¹)r=rr?r?r@rÛ»s  zResponse.close)r F)rirjrkr‰ràr‚rÚrÜrárär…rèrérCrlrçrïròrórõrëÚITER_CHUNK_SIZEr rßrr1rrìrÛr?r?r?r@rÎRs@/        7  % rÎ)Sr‰rØÚsysÚencodings.idnaÚ encodingsZurllib3.fieldsrZurllib3.filepostrZ urllib3.utilrZurllib3.exceptionsrrrrÚior rtr Ú structuresr r€r r�rrrÚ exceptionsrrrrrrrrÚ_internal_utilsrrÚutilsrrrrrr r!r"r#r$Úcompatr%r&r'r(r)r*r+r,r-r.r/r0r1r´Ú status_codesr2ÚmovedÚfoundÚotherÚtemporary_redirectrñrîÚDEFAULT_REDIRECT_LIMITr rr¼r4rnr|r†rÎr?r?r?r@ÚsD       (08  ûrJD