U
    Wi                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZ	d dl
mZ d dlmZmZ ddlmZmZmZmZ ddlmZmZmZmZmZmZmZmZ ddlmZmZm Z m!Z!m"Z"m#Z#m$Z$ dd	l%m&Z& dd
l'm(Z( ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= ddddgZ>G dd dej?e@e@f ZAG dd dZBG dd dZCG dd dej?e@e@f ZDdS )    )annotationsN)Mapping)Cookie	CookieJar   )
ByteStreamUnattachedStreamencode_requestencode_response)SUPPORTED_DECODERSByteChunkerContentDecoderIdentityDecoderLineDecoderMultiDecoderTextChunkerTextDecoder)CookieConflictHTTPStatusErrorRequestNotReadResponseNotReadStreamClosedStreamConsumedrequest_context)(get_multipart_boundary_from_content_type)codes)AsyncByteStreamCookieTypesHeaderTypesQueryParamTypesRequestContentRequestDataRequestExtensionsRequestFilesResponseContentResponseExtensionsSyncByteStream)URL)is_known_encodingnormalize_header_keynormalize_header_valueobfuscate_sensitive_headersparse_content_type_charsetparse_header_linksCookiesHeadersRequestResponsec                   @  sZ  e Zd ZdZdBddddddZed	d
ddZejd	ddddZedd
ddZdd
ddZ	dd
ddZ
dd
ddZdd
ddZdCd	dddd d!ZdDd	d#d$d%d&d'ZdEddd(d)d*Zd d
d+d,Zd	d	d-d.d/Zd	d	dd0d1d2Zd	dd-d3d4Zdd#d-d5d6Zd7d
d8d9Zd:d
d;d<Zdd#d=d>d?Zd	d
d@dAZdS )Fr/   z9
    HTTP headers, as a case-insensitive multi-dict.
    NHeaderTypes | None
str | NoneNone)headersencodingreturnc                   sj   |d krg | _ nPt|tr(t|j | _ n8t|trL fdd| D | _ n fdd|D | _  | _d S )Nc                   s4   g | ],\}}t |d  dt |d dt| fqS F)lowerr6   Tr)   r*   .0kvr6    S/home/afrizal/public_html/fastapi/venv/lib/python3.8/site-packages/httpx/_models.py
<listcomp>J   s
   z$Headers.__init__.<locals>.<listcomp>c                   s4   g | ],\}}t |d  dt |d dt| fqS r8   r:   r;   r?   r@   rA   rB   S   s
   )_list
isinstancer/   listr   items	_encoding)selfr5   r6   r@   r?   rA   __init__@   s    



	
	zHeaders.__init__strr7   c              
   C  sn   | j dkrhdD ]R}| jD ]<\}}z|| || W q tk
rR   Y  qY qX q|| _  qhqd| _ | j S )zn
        Header encoding is mandated as ascii, but we allow fallbacks to utf-8
        or iso-8859-1.
        N)asciiutf-8z
iso-8859-1)rG   rawdecodeUnicodeDecodeError)rH   r6   keyvaluer@   r@   rA   r6   ^   s    

zHeaders.encodingrR   r7   c                 C  s
   || _ d S N)rG   rH   rR   r@   r@   rA   r6   w   s    zlist[tuple[bytes, bytes]]c                 C  s   dd | j D S )zH
        Returns a list of the raw header items, as byte pairs.
        c                 S  s   g | ]\}}}||fqS r@   r@   )r<   Zraw_key_rR   r@   r@   rA   rB      s     zHeaders.raw.<locals>.<listcomp>rC   rH   r@   r@   rA   rN   {   s    zHeaders.rawztyping.KeysView[str]c                   s    fdd j D  S )Nc                   s    i | ]\}}}|  jd qS rT   rO   r6   r<   rV   rQ   rR   rX   r@   rA   
<dictcomp>   s     
 z Headers.keys.<locals>.<dictcomp>)rC   keysrX   r@   rX   rA   r\      s    zHeaders.keysztyping.ValuesView[str]c                 C  s^   i }| j D ]J\}}}|| j}|| j}||krL||  d| 7  < q
|||< q
| S )N, )rC   rO   r6   valuesrH   Zvalues_dictrV   rQ   rR   Zstr_keyZ	str_valuer@   r@   rA   r^      s    
zHeaders.valuesztyping.ItemsView[str, str]c                 C  s^   i }| j D ]J\}}}|| j}|| j}||krL||  d| 7  < q
|||< q
| S )z
        Return `(key, value)` items of headers. Concatenate headers
        into a single comma separated value when a key occurs multiple times.
        r]   )rC   rO   r6   rF   r_   r@   r@   rA   rF      s    
zHeaders.itemszlist[tuple[str, str]]c                   s    fdd j D S )z
        Return a list of `(key, value)` pairs of headers. Allow multiple
        occurrences of the same key without concatenating into a single
        comma separated value.
        c                   s*   g | ]"\}}}|  j|  jfqS r@   rY   rZ   rX   r@   rA   rB      s   z'Headers.multi_items.<locals>.<listcomp>rW   rX   r@   rX   rA   multi_items   s    
zHeaders.multi_items
typing.Any)rQ   defaultr7   c                 C  s(   z
| | W S  t k
r"   | Y S X dS )z
        Return a header value. If multiple occurrences of the header occur
        then concatenate them together with commas.
        N)KeyError)rH   rQ   rb   r@   r@   rA   get   s    
zHeaders.getFboolz	list[str])rQ   split_commasr7   c                   sZ   |  j  fddjD }|s.|S g }|D ]}|dd |dD  q6|S )z
        Return a list of all header values for a given key.
        If `split_commas=True` is passed, then any comma separated header
        values are split into multiple return strings.
        c                   s*   g | ]"\}}}|   kr|jqS r@   )r9   rO   r6   )r<   rV   item_keyZ
item_valueZget_header_keyrH   r@   rA   rB      s   z$Headers.get_list.<locals>.<listcomp>c                 S  s   g | ]}|  qS r@   )strip)r<   itemr@   r@   rA   rB      s     ,)r9   encoder6   rC   extendsplit)rH   rQ   rf   r^   Zsplit_valuesrR   r@   rh   rA   get_list   s    zHeaders.get_list)r5   r7   c                 C  s:   t |}| D ]}|| kr| | q| j|j d S rT   )r/   r\   poprC   rm   )rH   r5   rQ   r@   r@   rA   update   s
    zHeaders.updatec                 C  s   t | | jdS )Nr?   )r/   r6   rX   r@   r@   rA   copy   s    zHeaders.copy)rQ   r7   c                   s@   |  j  fddjD }|r4d|S t|dS )z
        Return a single header value.

        If there are multiple headers with the same key, then we concatenate
        them with commas. See: https://tools.ietf.org/html/rfc7230#section-3.2.2
        c                   s&   g | ]\}}}| kr| jqS r@   rY   )r<   rV   
header_keyheader_valueZnormalized_keyrH   r@   rA   rB      s   z'Headers.__getitem__.<locals>.<listcomp>r]   N)r9   rl   r6   rC   joinrc   )rH   rQ   rF   r@   ru   rA   __getitem__   s    
zHeaders.__getitem__rQ   rR   r7   c                   s   | | jpd}| | jpd}|   fddt| jD }t|dd D ]}| j|= qP|r||d }| |f| j|< n| j| |f dS )zs
        Set the header `key` to `value`, removing any duplicate entries.
        Retains insertion order.
        rM   c                   s"   g | ]\}\}}}| kr|qS r@   r@   r<   idxrV   rg   Z
lookup_keyr@   rA   rB      s   z'Headers.__setitem__.<locals>.<listcomp>r   Nr   )rl   rG   r9   	enumeraterC   reversedappend)rH   rQ   rR   Zset_key	set_valueZfound_indexesrz   r@   r{   rA   __setitem__   s    

zHeaders.__setitem__c                   sN   |  | j  fddt| jD }|s4t|t|D ]}| j|= q<dS )z*
        Remove the header `key`.
        c                   s&   g | ]\}\}}}|   kr|qS r@   )r9   ry   Zdel_keyr@   rA   rB     s   z'Headers.__delitem__.<locals>.<listcomp>N)r9   rl   r6   r|   rC   rc   r}   )rH   rQ   Zpop_indexesrz   r@   r   rA   __delitem__   s    
zHeaders.__delitem__c                 C  s$   |  | j}|dd | jD kS )Nc                 S  s   g | ]\}}}|qS r@   r@   )r<   rV   rQ   r@   r@   rA   rB     s     z(Headers.__contains__.<locals>.<listcomp>)r9   rl   r6   rC   )rH   rQ   rs   r@   r@   rA   __contains__  s    zHeaders.__contains__ztyping.Iterator[typing.Any]c                 C  s   t |  S rT   )iterr\   rX   r@   r@   rA   __iter__  s    zHeaders.__iter__intc                 C  s
   t | jS rT   )lenrC   rX   r@   r@   rA   __len__  s    zHeaders.__len__)otherr7   c                 C  sT   zt |}W n tk
r"   Y dS X dd | jD }dd |jD }t|t|kS )NFc                 S  s   g | ]\}}}||fqS r@   r@   rZ   r@   r@   rA   rB   "  s     z"Headers.__eq__.<locals>.<listcomp>c                 S  s   g | ]\}}}||fqS r@   r@   rZ   r@   r@   rA   rB   #  s     )r/   
ValueErrorrC   sorted)rH   r   Zother_headersZ	self_listZ
other_listr@   r@   rA   __eq__  s    zHeaders.__eq__c                 C  sv   | j j}d}| jdkr"d| j}tt|  }t|}t|t|k}|rb| d|| dS | d|| dS )N rL   z, encoding=())	__class____name__r6   rE   r+   r`   dictr   )rH   
class_nameZencoding_strZas_listas_dictZno_duplicate_keysr@   r@   rA   __repr__&  s    
zHeaders.__repr__)NN)N)F)N)r   
__module____qualname____doc__rI   propertyr6   setterrN   r\   r^   rF   r`   rd   ro   rq   rr   rw   r   r   r   r   r   r   r   r@   r@   r@   rA   r/   ;   s4     

c                   @  s   e Zd Zdddddddddd	ddddddd	d
dddddddZdddddZeddddZddddZddddZdddd Z	d!dd"d#Z
d!dd$d%d&ZdS )'r0   N)	paramsr5   cookiescontentdatafilesjsonstream
extensionszstr | bytesz	URL | strzQueryParamTypes | Noner2   CookieTypes | NonezRequestContent | NonezRequestData | NonezRequestFiles | Noneztyping.Any | None'SyncByteStream | AsyncByteStream | NonezRequestExtensions | Noner4   )methodurlr   r5   r   r   r   r   r   r   r   r7   c       	      	   C  s   t |tr|d n| | _t|| _|d k	rD| jj|d| _t|| _	|d krZi n|| _
|rrt||  |
d kr| j	d}t||||	t|r|| j	jnd dd\}}
| | |
| _t |
tr|   n|
| _d S )NrL   )r   zcontent-type)content_type)r   r   r   r   boundary)rD   bytesrO   upperr   r'   r   Zcopy_merge_paramsr/   r5   r   r.   set_cookie_headerrd   r	   r   rl   r6   _preparer   r   read)rH   r   r   r   r5   r   r   r   r   r   r   r   r   r@   r@   rA   rI   7  s:    





zRequest.__init__dict[str, str]default_headersr7   c                 C  s   |  D ].\}}| dkr(d| jkr(q| j|| qg }d| jk}d| jkpXd| jk}|sx| jjrx|d| jjf |s| jdkr|d t	|| jj
 | _d S )Ntransfer-encodingzContent-LengthHostzTransfer-Encodings   Host)POSTPUTPATCH)s   Content-Length   0)rF   r9   r5   
setdefaultr   hostr~   netlocr   r/   rN   )rH   r   rQ   rR   Zauto_headersZhas_hostZhas_content_lengthr@   r@   rA   r   w  s    

zRequest._preparer   rK   c                 C  s   t | dst | jS N_content)hasattrr   r   rX   r@   r@   rA   r     s    
zRequest.contentc                 C  sH   t | dsBt| jtjstd| j| _t| jtsBt| j| _| jS )6
        Read and return the request content.
        r       )	r   rD   r   typingIterableAssertionErrorrv   r   r   rX   r@   r@   rA   r     s    
zRequest.readc                   sX   t | dsRt| jtjstddd | j2 I dH | _t| jtsRt| j| _| jS )r   r   r   c                   s   g | z3 d H W }|q6 S rT   r@   r<   partr@   r@   rA   rB     s     z!Request.aread.<locals>.<listcomp>N)	r   rD   r   r   AsyncIterabler   rv   r   r   rX   r@   r@   rA   aread  s    
zRequest.areadrJ   c                 C  s,   | j j}t| j}d| d| jd|dS )N<r   r]   z)>)r   r   rJ   r   r   )rH   r   r   r@   r@   rA   r     s    
zRequest.__repr__dict[str, typing.Any]c                 C  s   dd | j  D S )Nc                 S  s   i | ]\}}|d kr||qS ))r   r   r@   r<   namerR   r@   r@   rA   r[     s    z(Request.__getstate__.<locals>.<dictcomp>__dict__rF   rX   r@   r@   rA   __getstate__  s    zRequest.__getstate__stater7   c                 C  s0   |  D ]\}}t| || qi | _t | _d S rT   )rF   setattrr   r   r   rH   r   r   rR   r@   r@   rA   __setstate__  s    zRequest.__setstate__)r   r   r   rI   r   r   r   r   r   r   r   r   r@   r@   r@   rA   r0   6  s$   (@c                   @  s  e Zd Zddddddddddd
ddddddd	d
dddddddZdddddZeddddZejdddddZeddddZejdddd dZed!dd"d#Z	ed!dd$d%Z
ed&dd'd(Zed)dd*d+Zed!dd,d-Zeddd.d/Zejd!ddd0d/Zeddd1d2Zd3dd4d5Zed6dd7d8Zed6dd9d:Zed6dd;d<Zed6dd=d>Zed6dd?d@Zed6ddAdBZed6ddCdDZd ddEdFZdddGdHdIZedJddKdLZedMddNdOZedddPdQZd!ddRdSZdTddUdVZdTddWdXdYZd)ddZd[Z dxd\d]d^d_d`Z!dyd\dad^dbdcZ"daddddeZ#dzd\d]d^dfdgZ$dddhdiZ%d)ddjdkZ&d{d\dld^dmdnZ'd|d\dod^dpdqZ(doddrdsZ)d}d\dld^dtduZ*dddvdwZ+dS )~r1   NrM   )
r5   r   texthtmlr   r   requestr   historydefault_encodingr   r2   zResponseContent | Noner3   ra   r   zRequest | NonezResponseExtensions | Nonezlist[Response] | Nonez#str | typing.Callable[[bytes], str]r4   )status_coder5   r   r   r   r   r   r   r   r   r   r7   c       
         C  s   || _ t|| _|| _d | _|	d kr(i n|	| _|
d kr:g nt|
| _d| _d| _	|| _
|d krt||||\}}| | || _t|tr|   n|| _d| _d S )NFr   )r   r/   r5   _requestZnext_requestr   rE   r   	is_closedis_stream_consumedr   r
   r   r   rD   r   r   _num_bytes_downloaded)rH   r   r5   r   r   r   r   r   r   r   r   r   r@   r@   rA   rI     s"    



zResponse.__init__r   r   c                 C  s<   |  D ].\}}| dkr(d| jkr(q| j|| qd S )Nr   zcontent-length)rF   r9   r5   r   )rH   r   rQ   rR   r@   r@   rA   r     s    zResponse._preparezdatetime.timedeltarK   c                 C  s   t | dstd| jS )ze
        Returns the time taken for the complete request/response
        cycle to complete.
        _elapsedzK'.elapsed' may only be accessed after the response has been read or closed.)r   RuntimeErrorr   rX   r@   r@   rA   elapsed   s
    
zResponse.elapsed)r   r7   c                 C  s
   || _ d S rT   )r   )rH   r   r@   r@   rA   r     s    r0   c                 C  s   | j dkrtd| j S )zR
        Returns the request instance associated to the current response.
        Nz7The request instance has not been set on this response.)r   r   rX   r@   r@   rA   r     s
    
zResponse.requestrS   c                 C  s
   || _ d S rT   )r   rU   r@   r@   rA   r     s    rJ   c                 C  s8   z| j d }W n tk
r$   Y dS X |jdddS d S )Nhttp_versionzHTTP/1.1rL   ignoreerrors)r   rc   rO   )rH   r   r@   r@   rA   r      s
    zResponse.http_versionc                 C  sB   z| j d }W n  tk
r.   t| j Y S X |jdddS d S )Nreason_phraserL   r   r   )r   rc   r   Zget_reason_phraser   rO   )rH   r   r@   r@   rA   r   )  s
    zResponse.reason_phraser'   c                 C  s   | j jS )zA
        Returns the URL for which the request was made.
        )r   r   rX   r@   r@   rA   r   2  s    zResponse.urlr   c                 C  s   t | dst | jS r   )r   r   r   rX   r@   r@   rA   r   9  s    
zResponse.contentc                 C  sN   t | dsH| j}|sd| _n,t| jp&dd}d|| j| g| _| jS )N_textr   rM   r?   )r   r   r   r   r6   rv   rO   flush)rH   r   decoderr@   r@   rA   r   ?  s    
zResponse.textc                 C  sZ   t | dsT| j}|dks t|sJt| jtr4| j}nt | drJ| | j}|pPd| _| jS )a  
        Return an encoding to use for decoding the byte content into text.
        The priority for determining this is given by...

        * `.encoding = <>` has been set explicitly.
        * The encoding as specified by the charset parameter in the Content-Type header.
        * The encoding as determined by `default_encoding`, which may either be
          a string like "utf-8" indicating the encoding to use, or may be a callable
          which enables charset autodetection.
        rG   Nr   rM   )r   charset_encodingr(   rD   r   rJ   r   rG   )rH   r6   r@   r@   rA   r6   J  s    


zResponse.encodingc                 C  s   t | drtd|| _dS )z
        Set the encoding to use for decoding the byte content into text.

        If the `text` attribute has been accessed, attempting to set the
        encoding will throw a ValueError.
        r   z?Setting encoding after `text` has been accessed is not allowed.N)r   r   rG   rU   r@   r@   rA   r6   `  s
    
c                 C  s    | j d}|dkrdS t|S )zO
        Return the encoding, as specified by the Content-Type header.
        zContent-TypeN)r5   rd   r,   )rH   r   r@   r@   rA   r   n  s    zResponse.charset_encodingr   c              	   C  s   t | dsg }| jjddd}|D ]B}|  }zt| }||  W q" tk
rb   Y q"Y q"X q"t|dkr~|d | _	n"t|dkrt
|d| _	nt | _	| j	S )z
        Returns a decoder instance which can be used to decode the raw byte
        content, depending on the Content-Encoding used in the response.
        _decoderzcontent-encodingT)rf   r   r   )children)r   r5   ro   ri   r9   r   r~   rc   r   r   r   r   )rH   Zdecodersr^   rR   Zdecoder_clsr@   r@   rA   _get_content_decodery  s     
zResponse._get_content_decoderre   c                 C  s   t | jS )zU
        A property which is `True` for 1xx status codes, `False` otherwise.
        )r   is_informationalr   rX   r@   r@   rA   r     s    zResponse.is_informationalc                 C  s   t | jS )zU
        A property which is `True` for 2xx status codes, `False` otherwise.
        )r   
is_successr   rX   r@   r@   rA   r     s    zResponse.is_successc                 C  s   t | jS )a  
        A property which is `True` for 3xx status codes, `False` otherwise.

        Note that not all responses with a 3xx status code indicate a URL redirect.

        Use `response.has_redirect_location` to determine responses with a properly
        formed URL redirection.
        )r   is_redirectr   rX   r@   r@   rA   r     s    
zResponse.is_redirectc                 C  s   t | jS )zU
        A property which is `True` for 4xx status codes, `False` otherwise.
        )r   is_client_errorr   rX   r@   r@   rA   r     s    zResponse.is_client_errorc                 C  s   t | jS )zU
        A property which is `True` for 5xx status codes, `False` otherwise.
        )r   is_server_errorr   rX   r@   r@   rA   r     s    zResponse.is_server_errorc                 C  s   t | jS )z]
        A property which is `True` for 4xx and 5xx status codes, `False` otherwise.
        )r   is_errorr   rX   r@   r@   rA   r     s    zResponse.is_errorc                 C  s(   | j tjtjtjtjtjfko&d| jkS )zs
        Returns True for 3xx responses with a properly formed URL redirection,
        `False` otherwise.
        ZLocation)r   r   MOVED_PERMANENTLYFOUND	SEE_OTHERTEMPORARY_REDIRECTPERMANENT_REDIRECTr5   rX   r@   r@   rA   has_redirect_location  s    zResponse.has_redirect_locationc                 C  st   | j }|dkrtd| jr | S | jr,d}nd}| jd }dddd	d
}||d}|j| |d}t||| ddS )z>
        Raise the `HTTPStatusError` if one occurred.
        NzYCannot call `raise_for_status` as the request instance has not been set on this response.z{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'
Redirect location: '{0.headers[location]}'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}z{error_type} '{0.status_code} {0.reason_phrase}' for url '{0.url}'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{0.status_code}d   zInformational responsezRedirect responsezClient errorzServer error)r            zInvalid status code)
error_type)r   response)r   r   r   r   r   rd   formatr   )rH   r   messageZstatus_classZerror_typesr   r@   r@   rA   raise_for_status  s*    
zResponse.raise_for_status)kwargsr7   c                 K  s   t j| jf|S rT   )jsonlibloadsr   )rH   r   r@   r@   rA   r     s    zResponse.jsonr.   c                 C  s$   t | dst | _| j|  | jS )N_cookies)r   r.   r  extract_cookiesrX   r@   r@   rA   r      s    
zResponse.cookiesz dict[str | None, dict[str, str]]c                 C  s*   | j d}|dkri S dd t|D S )zI
        Returns the parsed header links of the response, if any
        linkNc                 S  s"   i | ]}| d p| d|qS )relr   )rd   )r<   r  r@   r@   rA   r[     s    z"Response.links.<locals>.<dictcomp>)r5   rd   r-   )rH   headerr@   r@   rA   links  s    zResponse.linksc                 C  s   | j S rT   )r   rX   r@   r@   rA   num_bytes_downloaded  s    zResponse.num_bytes_downloadedc                 C  s   d| j  d| j dS )Nz<Response [ ]>)r   r   rX   r@   r@   rA   r     s    zResponse.__repr__r   c                 C  s   dd | j  D S )Nc                 S  s   i | ]\}}|d kr||qS ))r   r   r   r   r@   r   r@   r@   rA   r[     s    z)Response.__getstate__.<locals>.<dictcomp>r   rX   r@   r@   rA   r     s    zResponse.__getstate__r   c                 C  s6   |  D ]\}}t| || qd| _i | _t | _d S )NT)rF   r   r   r   r   r   r   r@   r@   rA   r   #  s
    zResponse.__setstate__c                 C  s    t | dsd|  | _| jS )7
        Read and return the response content.
        r   r   )r   rv   
iter_bytesr   rX   r@   r@   rA   r   *  s    
zResponse.readz
int | Noneztyping.Iterator[bytes])
chunk_sizer7   c              	   c  s   t | drT|dkrt| jn|}tdt| jt|dD ]}| j|||  V  q8n|  }t|d}t| jdf | 	 D ]$}|
|}|
|D ]
}|V  qq|| }|
|D ]
}|V  q| D ]
}|V  qW 5 Q R X dS z
        A byte-iterator over the decoded response content.
        This allows us to handle gzip, deflate, brotli, and zstd encoded responses.
        r   Nr   r   r  r   )r   r   r   rangemaxr   r   r   r   iter_rawrO   r   rH   r  ir   chunkerZ	raw_bytesdecodedchunkr@   r@   rA   r  2  s     



zResponse.iter_bytestyping.Iterator[str]c              	   c  s   t | jp
dd}t|d}t| jdf |  D ]$}||}||D ]
}|V  qHq0| }||D ]
}|V  qh| D ]
}|V  q|W 5 Q R X dS z
        A str-iterator over the decoded response content
        that handles both gzip, deflate, etc but also detects the content's
        string encoding.
        rM   r?   r  r  N)r   r6   r   r   r   r  rO   r   rH   r  r   r  Zbyte_contentZtext_contentr  r@   r@   rA   	iter_textI  s    


zResponse.iter_textc              	   c  sZ   t  }t| jd> |  D ]}||D ]
}|V  q*q| D ]
}|V  q@W 5 Q R X d S Nr  )r   r   r   r  rO   r   rH   r   r   liner@   r@   rA   
iter_lines\  s    
zResponse.iter_linesc              	   c  s   | j rt | jrt t| jts,tdd| _ d| _t	|d}t
| jd: | jD ],}|  jt|7  _||D ]
}|V  qvqVW 5 Q R X | D ]
}|V  q|   dS )@
        A byte-iterator over the raw response content.
        z5Attempted to call a sync iterator on an async stream.Tr   r  r  N)r   r   r   r   rD   r   r&   r   r   r   r   r   r   rO   r   closerH   r  r  Zraw_stream_bytesr  r@   r@   rA   r  e  s"    

zResponse.iter_rawc              	   C  sF   t | jtstd| jsBd| _t| jd | j  W 5 Q R X dS )
        Close the response and release the connection.
        Automatically called if the response body is read to completion.
        z3Attempted to call an sync close on an async stream.Tr  N)rD   r   r&   r   r   r   r   r#  rX   r@   r@   rA   r#    s    zResponse.closec                   s0   t | ds*ddd |  2 I dH | _| jS )r  r   r   c                   s   g | z3 d H W }|q6 S rT   r@   r   r@   r@   rA   rB     s     z"Response.aread.<locals>.<listcomp>N)r   rv   aiter_bytesr   rX   r@   r@   rA   r     s    
 zResponse.areadztyping.AsyncIterator[bytes]c              	   C s   t | drT|dkrt| jn|}tdt| jt|dD ]}| j|||  V  q8n|  }t|d}t| jdp | 	 2 z,3 dH W }|
|}|
|D ]
}|V  qq|6 | }|
|D ]
}|V  q| D ]
}|V  qW 5 Q R X dS r  )r   r   r   r  r  r   r   r   r   	aiter_rawrO   r   r  r@   r@   rA   r&    s     


zResponse.aiter_bytesztyping.AsyncIterator[str]c              	   C s   t | jp
dd}t|d}t| jdp |  2 z,3 dH W }||}||D ]
}|V  qPq06 | }||D ]
}|V  qr| D ]
}|V  qW 5 Q R X dS r  )r   r6   r   r   r   r&  rO   r   r  r@   r@   rA   
aiter_text  s    

zResponse.aiter_textc              	   C sd   t  }t| jdH |  2 z"3 d H W }||D ]
}|V  q2q6 | D ]
}|V  qJW 5 Q R X d S r  )r   r   r   r(  rO   r   r  r@   r@   rA   aiter_lines  s    zResponse.aiter_linesc              	   C s   | j rt | jrt t| jts,tdd| _ d| _t	|d}t
| jdD | j2 z43 dH W }|  jt|7  _||D ]
}|V  q~qV6 W 5 Q R X | D ]
}|V  q|  I dH  dS )r"  z6Attempted to call an async iterator on an sync stream.Tr   r  r  N)r   r   r   r   rD   r   r   r   r   r   r   r   r   rO   r   acloser$  r@   r@   rA   r'    s"    
zResponse.aiter_rawc              	     sL   t | jtstd| jsHd| _t| jd | j I dH  W 5 Q R X dS )r%  z3Attempted to call an async close on an sync stream.Tr  N)rD   r   r   r   r   r   r   r*  rX   r@   r@   rA   r*    s    zResponse.aclose)N)N)N)N)N)N),r   r   r   rI   r   r   r   r   r   r   r   r   r   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r   r   r   r   r  r  r!  r  r#  r   r&  r(  r)  r'  r*  r@   r@   r@   rA   r1     s   (7


%		  
 c                   @  s:  e Zd ZdZd:dddddZddd	d
dZdddddZd;ddddddddZd<ddddddddZd=dddddddZ	d>ddddddZ
d?dddd d!Zdddd"d#d$Zddd%d&d'Zddd%d(d)Zd*d+d,d-Zd.d+d/d0Zd1d+d2d3Zdd+d4d5ZG d6d7 d7ejjZG d8d9 d9ZdS )@r.   z-
    HTTP Cookies, as a mutable mapping.
    Nr   r4   )r   r7   c                 C  s   |d kst |trDt | _t |tr| D ]\}}| || q,n`t |trrt | _|D ]\}}| || qZn2t |trt | _|jD ]}| j| qn|| _d S rT   )	rD   r   r   jarrF   setrE   r.   
set_cookie)rH   r   rQ   rR   cookier@   r@   rA   rI     s    



zCookies.__init__r1   r   r7   c                 C  s(   |  |}| |j}| j|| dS )zO
        Loads any cookies based on the response `Set-Cookie` headers.
        N)_CookieCompatResponse_CookieCompatRequestr   r+  r  )rH   r   Zurllib_responseurllib_requestr@   r@   rA   r    s    
zCookies.extract_cookiesr0   r   r7   c                 C  s   |  |}| j| dS )zM
        Sets an appropriate 'Cookie:' HTTP header on the `Request`.
        N)r1  r+  add_cookie_header)rH   r   r2  r@   r@   rA   r     s    
zCookies.set_cookie_headerr   /rJ   )r   rR   domainpathr7   c                 C  sT   d||dd|t ||d|t |dddddddidd}tf |}| j| dS )zU
        Set a cookie value by name. May optionally include domain and path.
        r   NF.THttpOnly)versionr   rR   portport_specifiedr6  domain_specifieddomain_initial_dotr7  path_specifiedsecureexpiresdiscardcommentcomment_urlrestrfc2109)re   
startswithr   r+  r-  )rH   r   rR   r6  r7  r   r.  r@   r@   rA   r,    s(    
zCookies.setr3   )r   rb   r6  r7  r7   c                 C  sn   d}| j D ]R}|j|kr
|dks*|j|kr
|dks<|j|kr
|dk	rVd| }t||j}q
|dkrj|S |S )z
        Get a cookie by name. May optionally include domain and path
        in order to specify exactly which cookie to retrieve.
        Nz!Multiple cookies exist with name=)r+  r   r6  r7  r   rR   )rH   r   rb   r6  r7  rR   r.  r   r@   r@   rA   rd   5  s    


zCookies.get)r   r6  r7  r7   c                   s\    dk	r dk	r | j  S  fdd| j D }|D ]}| j |j|j|j q<dS )z
        Delete a cookie by name. May optionally include domain and path
        in order to specify exactly which cookie to delete.
        Nc                   s>   g | ]6}|j kr d ks$|j krd ks6|jkr|qS rT   )r   r6  r7  r<   r.  r6  r   r7  r@   rA   rB   [  s   
 
 
z"Cookies.delete.<locals>.<listcomp>)r+  clearr6  r7  r   )rH   r   r6  r7  remover.  r@   rI  rA   deleteN  s    
zCookies.delete)r6  r7  r7   c                 C  sD   g }|dk	r| | |dk	r4|dk	s*t| | | jj|  dS )z
        Delete all cookies. Optionally include a domain and path in
        order to only delete a subset of all the cookies.
        N)r~   r   r+  rJ  )rH   r6  r7  argsr@   r@   rA   rJ  f  s    

zCookies.clearc                 C  s$   t |}|jD ]}| j| qd S rT   )r.   r+  r-  )rH   r   r.  r@   r@   rA   rq   s  s    
zCookies.update)r   rR   r7   c                 C  s   |  ||S rT   )r,  rH   r   rR   r@   r@   rA   r   x  s    zCookies.__setitem__)r   r7   c                 C  s   |  |}|d krt||S rT   )rd   rc   rN  r@   r@   rA   rw   {  s    
zCookies.__getitem__c                 C  s
   |  |S rT   )rL  )rH   r   r@   r@   rA   r     s    zCookies.__delitem__r   rK   c                 C  s
   t | jS rT   )r   r+  rX   r@   r@   rA   r     s    zCookies.__len__r  c                 C  s   dd | j D S )Nc                 s  s   | ]}|j V  qd S rT   )r   rH  r@   r@   rA   	<genexpr>  s     z#Cookies.__iter__.<locals>.<genexpr>r+  rX   r@   r@   rA   r     s    zCookies.__iter__re   c                 C  s   | j D ]} dS dS )NTFrP  )rH   rV   r@   r@   rA   __bool__  s    
zCookies.__bool__c                 C  s"   d dd | jD }d| dS )Nr]   c              	   S  s*   g | ]"}d |j  d|j d|j dqS )z<Cookie =z for z />)r   rR   r6  rH  r@   r@   rA   rB     s   z$Cookies.__repr__.<locals>.<listcomp>z	<Cookies[r  )rv   r+  )rH   Zcookies_reprr@   r@   rA   r     s    zCookies.__repr__c                      s>   e Zd ZdZddd fddZdddd fd	d
Z  ZS )zCookies._CookieCompatRequest
        Wraps a `Request` instance up in a compatibility interface suitable
        for use with `CookieJar` operations.
        r0   r4   r3  c                   s*   t  jt|jt|j|jd || _d S )N)r   r5   r   )superrI   rJ   r   r   r5   r   r   )rH   r   r   r@   rA   rI     s    z%Cookies._CookieCompatRequest.__init__rJ   rx   c                   s   t  || || jj|< d S rT   )rT  add_unredirected_headerr   r5   )rH   rQ   rR   rU  r@   rA   rV    s    z4Cookies._CookieCompatRequest.add_unredirected_header)r   r   r   r   rI   rV  __classcell__r@   r@   rU  rA   r1    s   r1  c                   @  s.   e Zd ZdZdddddZddd	d
ZdS )zCookies._CookieCompatResponserS  r1   r4   r/  c                 C  s
   || _ d S rT   )r   )rH   r   r@   r@   rA   rI     s    z&Cookies._CookieCompatResponse.__init__zemail.message.MessagerK   c                 C  s,   t j }| jj D ]\}}|||< q|S rT   )emailr   Messager   r5   r`   )rH   inforQ   rR   r@   r@   rA   rZ    s    

z"Cookies._CookieCompatResponse.infoN)r   r   r   r   rI   rZ  r@   r@   r@   rA   r0    s   r0  )N)r   r5  )NNN)NN)NN)N)r   r   r   r   rI   r  r   r,  rd   rL  rJ  rq   r   rw   r   r   r   rQ  r   urllibr   r0   r1  r0  r@   r@   r@   rA   r.     s.   	     
)E
__future__r   datetimeemail.messagerX  r   r  r   urllib.requestr[  collections.abcr   http.cookiejarr   r   r   r   r   r	   r
   	_decodersr   r   r   r   r   r   r   r   _exceptionsr   r   r   r   r   r   r   Z
_multipartr   Z_status_codesr   _typesr   r   r   r   r    r!   r"   r#   r$   r%   r&   Z_urlsr'   Z_utilsr(   r)   r*   r+   r,   r-   __all__MutableMappingrJ   r/   r0   r1   r.   r@   r@   r@   rA   <module>   s4   (
$	4 	 |     8