U ÀÒ.eã…ã@sÊdZddlZddlZddlZddlmZddlmZddl m Z ddl m Z m Z mZmZddlmZdd lmZdd lmZdd lmZdd lmZmZmZdd lmZmZmZm 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.m/Z/m0Z0m1Z1ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;mZ>ddl2m?Z@ddlAmBZBeBjCeBjDeBjEeBjFeBjGfZHdZIdZJdZKGdd„deLƒZMGdd„deLƒZNGdd„deNƒZOGdd„deMeNƒZPGdd„deLƒZQdS) 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)Ú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)Úselfr7Úpr8r:©r>ú?/usr/lib/python3.8/site-packages/pip/_vendor/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}|dk r\| 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)Zdoseq) Ú isinstancer*r+Úhasattrrr/r9Úencoder))ÚdataÚresultÚkZvsÚ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}|dk rft|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| dk�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.rBNrCéérA)ÚnamerGÚfilenameÚheaders)Ú content_type)Ú ValueErrorrDr/rrEr+r*r9ÚdecoderFÚtupleÚlistÚlenrÚ bytearrayrArZmake_multipartr)ÚfilesrGZ new_fieldsZfieldsZfieldÚvalrJrIZftZfhÚfnÚfpZfdataZrfÚbodyrQr>r>r?Ú _encode_filesmsT       ÿÿ          z"RequestEncodingMixin._encode_filesN)Ú__name__Ú __module__Ú __qualname__Úpropertyr@Ú staticmethodrKr]r>r>r>r?r3<s   r3c@seZdZdd„Zdd„ZdS)ÚRequestHooksMixincCsZ||jkrtd|ƒ‚t|tƒr2|j| |¡n$t|dƒrV|j| dd„|Dƒ¡dS)zProperly register a hook.z1Unsupported event specified, with event name "%s"rBcss|]}t|tƒr|VqdS©N)rDr$)Ú.0Úhr>r>r?Ú ¸s z2RequestHooksMixin.register_hook..N)ÚhooksrRrDr$r9rEÚextend©r<ÚeventÚhookr>r>r?Ú register_hook¯s     zRequestHooksMixin.register_hookcCs2z|j| |¡WdStk r,YdSXdS)ziDeregister a previously registered hook. Returns True if the hook existed, False if not. TFN)rhÚremoverRrjr>r>r?Úderegister_hookºs z!RequestHooksMixin.deregister_hookN)r^r_r`rmror>r>r>r?rc®s rcc @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¶|dkr gn|}|dkrgn|}|dkr,in|}|dkrr>r?Ú__init__âs"zRequest.__init__cCs d|jS)Nz©rr©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.) rrr7rPrXrGr0rsrtrurh) ÚPreparedRequestÚpreparerrr7rPrXrGr0rsrtrurh©r<r=r>r>r?r{þsö zRequest.prepare) NNNNNNNNNN)r^r_r`Ú__doc__rvryr{r>r>r>r?rpÆsþ rpc @s‚eZdZdZdd„Zddd„Zdd„Zd d „Zd d „Ze d d„ƒZ dd„Z dd„Z ddd„Z dd„Zd dd„Zdd„Zdd„ZdS)!rzaÇThe 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', 'https://httpbin.org/get') >>> r = req.prepare() >>> s = requests.Session() >>> s.send(r) cCs0d|_d|_d|_d|_d|_tƒ|_d|_dSrd)rrr7rPÚ_cookiesr\r rhÚ_body_positionrxr>r>r?rv"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<rrr7rPrXrGrsrtrurhr0r>r>r?r{3s     zPreparedRequest.preparecCs d|jS)Nzrwrxr>r>r?ryEszPreparedRequest.__repr__cCsXtƒ}|j|_|j|_|jdk r*|j ¡nd|_t|jƒ|_|j|_|j|_|j |_ |Srd) rzrrr7rPÚcopyrr~r\rhrr|r>r>r?r‡Hs zPreparedRequest.copycCs$||_|jdk r t|j ¡ƒ|_dS)zPrepares the given HTTP method.N)rrrÚupper)r<rrr>r>r?r€Ss zPreparedRequest.prepare_methodcCsDddlm}z|j|dd� d¡}Wn|jk r>t‚YnX|S)Nr)ÚidnaT)Zuts46rC)Z pip._vendorr‰rFrSZ IDNAErrorÚ UnicodeError)Úhostr‰r>r>r?Ú_get_idna_encoded_hostYs   z&PreparedRequest._get_idna_encoded_hostc Cs,t|tƒr| d¡}ntr"t|ƒnt|ƒ}| ¡}d|krR| ¡ d¡sR||_ dSzt |ƒ\}}}}}}} Wn,t k r˜} zt | j Ž‚W5d} ~ XYnX|sºd} |  t|dƒ¡} t| ƒ‚|sÊt d|ƒ‚t|ƒ�sz| |¡}Wntk rþt dƒ‚YnXn| 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ú:ZhttpNzDInvalid URL {0!r}: No schema supplied. Perhaps you meant http://{0}?z Invalid URL %r: No host suppliedzURL has an invalid label.Ú*r6ú@r4rCz%s&%s)rDr+rSr,Zunicoder*ÚlstripÚlowerÚ startswithr7rrrÚargsÚformatrrrrŒrŠrFrKrr')r<r7rsZschemertr‹Zportr8r:ZfragmentÚeÚerrorZnetlocZ enc_paramsr>r>r?r�csh                 zPreparedRequest.prepare_urlcCs<tƒ|_|r8| ¡D]"}t|ƒ|\}}||jt|ƒ<qdS)z Prepares the given HTTP headers.N)r rPrqr#r)r<rPÚheaderrNÚvaluer>r>r?r‚¹s  zPreparedRequest.prepare_headersc Csrd}d}|s6|dk r6d}t |¡}t|tƒs6| d¡}tt|dƒt|ttt t fƒ gƒ}z t |ƒ}Wnt t tfk r‚d}YnX|rø|}t|ddƒdk rÌz| ¡|_Wn ttfk rÊtƒ|_YnX|rØtdƒ‚|rìt|ƒ|jd<n d|jd <np|�r| ||¡\}}n2|�rB| |¡}t|tƒ�s8t|d ƒ�r>d}nd }| |¡|�rhd |jk�rh||jd <||_dS)z"Prepares the given HTTP body data.Nzapplication/jsonrCrBÚtellz1Streamed bodies and files are mutually exclusive.úContent-LengthZchunkedzTransfer-EncodingrAz!application/x-www-form-urlencodedz content-typez Content-Type)Ú complexjsonÚdumpsrDr+rFÚallrEr/rUrTr%r"Ú TypeErrorÚAttributeErrorr ÚgetattrršrÚIOErrorÚOSErrorÚobjectÚNotImplementedErrorr.rPr]rKÚprepare_content_lengthr\)r<rGrXr0r\rQZ is_streamÚlengthr>r>r?r„ÄsL    þ      zPreparedRequest.prepare_bodycCsL|dk r$t|ƒ}|rHt|ƒ|jd<n$|jdkrH|j d¡dkrHd|jd<dS)z>Prepare Content-Length header based on request method and bodyNr›)ZGETZHEADÚ0)r"r.rPrrÚget)r<r\r§r>r>r?r¦ s z&PreparedRequest.prepare_content_lengthr6cCsj|dkr"t|jƒ}t|ƒr|nd}|rft|tƒrDt|ƒdkrDt|Ž}||ƒ}|j |j¡|  |j ¡dS)z"Prepares the given HTTP auth data.NrL) rr7ÚanyrDrTrVr Ú__dict__Úupdater¦r\)r<rtr7Zurl_authÚrr>r>r?r…s zPreparedRequest.prepare_authcCs@t|tjƒr||_n t|ƒ|_t|j|ƒ}|dk r<||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. NZCookie)rDr&Z CookieJarr~rrrP)r<ruZ cookie_headerr>r>r?rƒ,s   zPreparedRequest.prepare_cookiescCs&|pg}|D]}| |||¡q dS)zPrepares the given hooks.N)rm)r<rhrkr>r>r?r†@szPreparedRequest.prepare_hooks) NNNNNNNNNN)N)r6)r^r_r`r}rvr{ryr‡r€rbrŒr�r‚r„r¦r…rƒr†r>r>r>r?rzs2þ   V E rzc @seZdZdZdddddddd d d g Zd d „Zdd„Zdd„Zdd„Zdd„Z dd„Z dd„Z dd„Z dd„Z edd„ƒZed d!„ƒZed"d#„ƒZed$d%„ƒZed&d'„ƒZd;d*d+„Zed)d,fd-d.„Zed/d0„ƒZed1d2„ƒZd3d4„Zed5d6„ƒZd7d8„Zd9d:„Zd,S)<ÚResponsezhThe :class:`Response ` object, which contains a server's response to an HTTP request. Ú_contentÚ status_coderPr7ÚhistoryÚencodingÚreasonruÚelapsedÚrequestcCs^d|_d|_d|_d|_tƒ|_d|_d|_d|_g|_ d|_ t iƒ|_ t  d¡|_d|_dS)NFr)r¯Ú_content_consumedÚ_nextr°r rPÚrawr7r²r±r³rruÚdatetimeZ timedeltar´rµrxr>r>r?rvTs  zResponse.__init__cCs|Srdr>rxr>r>r?Ú __enter__ƒszResponse.__enter__cGs | ¡dSrd)Úclose)r<r”r>r>r?Ú__exit__†szResponse.__exit__cs ˆjs ˆj‡fdd„ˆjDƒS)Ncsi|]}|tˆ|dƒ“qSrd)r¡)reÚattrrxr>r?Ú �sz)Response.__getstate__..)r¶ÚcontentÚ __attrs__rxr>rxr?Ú __getstate__‰szResponse.__getstate__cCs:| ¡D]\}}t|||ƒqt|ddƒt|ddƒdS)Nr¶Tr¸)rqÚsetattr)r<ÚstaterNr™r>r>r?Ú __setstate__‘s zResponse.__setstate__cCs d|jS)Nz)r°rxr>r>r?ry™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``. ©Úokrxr>r>r?Ú__bool__œszResponse.__bool__cCs|jSrÅrÆrxr>r>r?Ú __nonzero__¦szResponse.__nonzero__cCs | d¡S)z,Allows you to use a response as an iterator.é€)Ú iter_contentrxr>r>r?rB°szResponse.__iter__cCs(z | ¡Wntk r"YdSXdS)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_statusrrxr>r>r?rÇ´s  z Response.okcCsd|jko|jtkS)zšTrue if this Response is a well-formed HTTP redirect that could have been processed automatically (by :meth:`Session.resolve_redirects`). Úlocation)rPr°ÚREDIRECT_STATIrxr>r>r?Ú is_redirectÃszResponse.is_redirectcCsd|jko|jtjtjfkS)z@True if this Response one of the permanent versions of redirect.rÍ)rPr°r1Zmoved_permanentlyÚpermanent_redirectrxr>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·rxr>r>r?ÚnextÏsz Response.nextcCst |j¡dS)z7The apparent encoding, provided by the chardet library.r²)r-Zdetectr¿rxr>r>r?Úapparent_encodingÔszResponse.apparent_encodingr Fcs|‡‡fdd„}ˆjr(tˆjtƒr(tƒ‚n"ˆdk rJtˆ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Ätk rV}z t|ƒ‚W5d}~XYqÄtk r~}z t|ƒ‚W5d}~XYqÄtk r¦}z t|ƒ‚W5d}~XYqÄXnˆj  ˆ¡}|s¼qÄ|Vqªdˆ_ dS)NÚstreamT)Zdecode_content) rEr¸rÔrrrrrrrAr¶)Úchunkr–©Ú chunk_sizer<r>r?Úgenerateês   z'Response.iter_content..generateNz.chunk_size must be an int, it is instead a %s.) r¶rDr¯ÚboolrÚintrŸÚtyper r)r<r×Údecode_unicoderØZ reused_chunksZ stream_chunksZchunksr>rÖr?rËÙs  zResponse.iter_contentNccs”d}|j||d�D]n}|dk r&||}|r6| |¡}n| ¡}|rl|drl|rl|dd|dkrl| ¡}nd}|D] }|Vqtq|dk r�|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ÜZ delimiterZpendingrÕÚlinesÚliner>r>r?Ú iter_liness $  zResponse.iter_linescCsV|jdkrJ|jrtdƒ‚|jdks,|jdkr4d|_nd | t¡¡pFd|_d|_|jS)z"Content of the response, in bytes.Fz2The content for this response was already consumedrNóT)r¯r¶Ú RuntimeErrorr°r¸r;rËÚCONTENT_CHUNK_SIZErxr>r>r?r¿/s ÿzResponse.contentc Cshd}|j}|jstdƒS|jdkr(|j}zt|j|dd�}Wn&ttfk rbt|jdd�}YnX|S)aÀContent 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. Nr6Úreplace)Úerrors)r²r¿r*rÓÚ LookupErrorrŸ)r<r¿r²r>r>r?ÚtextCs z Response.textcKsj|jsZ|jrZt|jƒdkrZt|jƒ}|dk rZztj|j |¡f|ŽWStk rXYnXtj|jf|ŽS)zØReturns 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) r²r¿rVr!rœÚloadsrSÚUnicodeDecodeErrorrê)r<Úkwargsr²r>r>r?r0is  ÿÿz Response.jsoncCsF|j d¡}i}|rBt|ƒ}|D] }| d¡p6| d¡}|||<q |S)z8Returns the parsed header links of the response, if any.ÚlinkZrelr7)rPr©r)r<r˜ÚlÚlinksrîÚkeyr>r>r?rðƒs  zResponse.linkscCs¸d}t|jtƒrDz|j d¡}WqJtk r@|j d¡}YqJXn|j}d|jkr`dkrxnnd|j||jf}n,d|jkrŽdkr¤nnd|j||jf}|r´t||d �‚d S) z2Raises stored :class:`HTTPError`, if one occurred.r6rCz iso-8859-1i�iôz%s Client Error: %s for url: %siXz%s Server Error: %s for url: %s)ZresponseN)rDr³r+rSrìr°r7r)r<Zhttp_error_msgr³r>r>r?rÌ•s zResponse.raise_for_statuscCs0|js|j ¡t|jddƒ}|dk r,|ƒ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<ròr>r>r?r»®s  zResponse.close)r F)r^r_r`r}rÀrvrºr¼rÁrÄryrÈrÉrBrarÇrÏrÑrÒrÓrËÚITER_CHUNK_SIZErãr¿rêr0rðrÌr»r>r>r>r?r®JsTþ/        7  % r®)Rr}r¹ÚsysZencodings.idnaZ encodingsZpip._vendor.urllib3.fieldsrZpip._vendor.urllib3.filepostrZpip._vendor.urllib3.utilrZpip._vendor.urllib3.exceptionsrrrrÚior rhr Z structuresr rtr rurrrÚ exceptionsrrrrrrrZ_internal_utilsrrZutilsrrrrrrr r!r"r#Úcompatr$r%r&r'r(r)r*r+r,r-r.r/r0rœZ status_codesr1ZmovedÚfoundÚotherZtemporary_redirectrÐrÎZDEFAULT_REDIRECT_LIMITrærór¤r3rcrprzr®r>r>r>r?ÚsD       $08  ûrJ<