a
    Tf?                     @  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= G dd dej>e?e?f Z@G dd dZAG dd dZBG dd dej>e?e?f ZCd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_linksc                   @  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 )FHeadersz9
    HTTP headers, as a case-insensitive multi-dict.
    NHeaderTypes | None
str | NoneNone)headersencodingreturnc                   sj   |d u 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)lowerr3   Tr)   r*   .0kvr3    F/var/www/ai-form-bot/venv/lib/python3.9/site-packages/httpx/_models.py
<listcomp>H   s
   z$Headers.__init__.<locals>.<listcomp>c                   s4   g | ],\}}t |d  dt |d dt| fqS r5   r7   r8   r<   r=   r>   r?   Q   s
   )_list
isinstancer.   listr   items	_encoding)selfr2   r3   r=   r<   r>   __init__>   s    



	
	zHeaders.__init__strr4   c              
   C  sl   | j du rfdD ]P}| jD ]:\}}z|| || W q tyP   Y  qY q0 q|| _  qfq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)rD   rawdecodeUnicodeDecodeError)rE   r3   keyvaluer=   r=   r>   r3   \   s    

zHeaders.encodingrO   r4   c                 C  s
   || _ d S N)rD   rE   rO   r=   r=   r>   r3   u   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=   )r9   Zraw_key_rO   r=   r=   r>   r?   ~       zHeaders.raw.<locals>.<listcomp>r@   rE   r=   r=   r>   rK   y   s    zHeaders.rawztyping.KeysView[str]c                   s    fdd j D  S )Nc                   s    i | ]\}}}|  jd qS rQ   rL   r3   r9   rS   rN   rO   rV   r=   r>   
<dictcomp>   rT   z Headers.keys.<locals>.<dictcomp>)r@   keysrV   r=   rV   r>   rZ      s    zHeaders.keysztyping.ValuesView[str]c                 C  s^   i }| j D ]J\}}}|| j}|| j}||v rL||  d| 7  < q
|||< q
| S )N, )r@   rL   r3   valuesrE   Zvalues_dictrS   rN   rO   Zstr_keyZ	str_valuer=   r=   r>   r\      s    
zHeaders.valuesztyping.ItemsView[str, str]c                 C  s^   i }| j D ]J\}}}|| j}|| j}||v 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[   )r@   rL   r3   rC   r]   r=   r=   r>   rC      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=   rW   rX   rV   r=   r>   r?      s   z'Headers.multi_items.<locals>.<listcomp>rU   rV   r=   rV   r>   multi_items   s    
zHeaders.multi_items
typing.Any)rN   defaultr4   c                 C  s&   z
| | W S  t y    | Y S 0 dS )z
        Return a header value. If multiple occurrences of the header occur
        then concatenate them together with commas.
        N)KeyError)rE   rN   r`   r=   r=   r>   get   s    
zHeaders.getFboolz	list[str])rN   split_commasr4   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=   )r6   rL   r3   )r9   rS   item_keyZ
item_valueZget_header_keyrE   r=   r>   r?      s   z$Headers.get_list.<locals>.<listcomp>c                 S  s   g | ]}|  qS r=   )strip)r9   itemr=   r=   r>   r?      rT   ,)r6   encoder3   r@   extendsplit)rE   rN   rd   r\   Zsplit_valuesrO   r=   rf   r>   get_list   s    zHeaders.get_list)r2   r4   c                 C  s:   t |}| D ]}|| v r| | q| j|j d S rQ   )r.   rZ   popr@   rk   )rE   r2   rN   r=   r=   r>   update   s
    zHeaders.updatec                 C  s   t | | jdS )Nr<   )r.   r3   rV   r=   r=   r>   copy   s    zHeaders.copy)rN   r4   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=   rW   )r9   rS   
header_keyheader_valueZnormalized_keyrE   r=   r>   r?      s   z'Headers.__getitem__.<locals>.<listcomp>r[   N)r6   rj   r3   r@   joinra   )rE   rN   rC   r=   rs   r>   __getitem__   s    
zHeaders.__getitem__rN   rO   r4   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.
        rJ   c                   s"   g | ]\}\}}}| kr|qS r=   r=   r9   idxrS   re   Z
lookup_keyr=   r>   r?      s   z'Headers.__setitem__.<locals>.<listcomp>r   Nr   )rj   rD   r6   	enumerater@   reversedappend)rE   rN   rO   Zset_key	set_valueZfound_indexesrx   r=   ry   r>   __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=   )r6   rw   Zdel_keyr=   r>   r?     s   z'Headers.__delitem__.<locals>.<listcomp>N)r6   rj   r3   rz   r@   ra   r{   )rE   rN   Zpop_indexesrx   r=   r   r>   __delitem__   s    
zHeaders.__delitem__c                 C  s$   |  | j}|dd | jD v S )Nc                 S  s   g | ]\}}}|qS r=   r=   )r9   rS   rN   r=   r=   r>   r?     rT   z(Headers.__contains__.<locals>.<listcomp>)r6   rj   r3   r@   )rE   rN   rq   r=   r=   r>   __contains__  s    zHeaders.__contains__ztyping.Iterator[typing.Any]c                 C  s   t |  S rQ   )iterrZ   rV   r=   r=   r>   __iter__  s    zHeaders.__iter__intc                 C  s
   t | jS rQ   )lenr@   rV   r=   r=   r>   __len__  s    zHeaders.__len__)otherr4   c                 C  sR   zt |}W n ty    Y dS 0 dd | jD }dd |jD }t|t|kS )NFc                 S  s   g | ]\}}}||fqS r=   r=   rX   r=   r=   r>   r?      rT   z"Headers.__eq__.<locals>.<listcomp>c                 S  s   g | ]\}}}||fqS r=   r=   rX   r=   r=   r>   r?   !  rT   )r.   
ValueErrorr@   sorted)rE   r   Zother_headersZ	self_listZ
other_listr=   r=   r>   __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 rI   z, encoding=())	__class____name__r3   rB   r+   r^   dictr   )rE   
class_nameZencoding_strZas_listas_dictZno_duplicate_keysr=   r=   r>   __repr__$  s    
zHeaders.__repr__)NN)N)F)N)r   
__module____qualname____doc__rF   propertyr3   setterrK   rZ   r\   rC   r^   rb   rm   ro   rp   ru   r~   r   r   r   r   r   r   r=   r=   r=   r>   r.   9   s4     

r.   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 )'RequestN)	paramsr2   cookiescontentdatafilesjsonstream
extensionszstr | bytesz	URL | strzQueryParamTypes | Noner/   CookieTypes | NonezRequestContent | NonezRequestData | NonezRequestFiles | Noneztyping.Any | None'SyncByteStream | AsyncByteStream | NonezRequestExtensions | Noner1   )methodurlr   r2   r   r   r   r   r   r   r   r4   c       	      	   C  s   t |tr|d n| | _t|| _|d urD| jj|d| _t|| _	|d u rZi n|| _
|rrt||  |
d u r| j	d}t||||	t|r|| j	jnd dd\}}
| | |
| _t |
tr|   n|
| _d S )NrI   )r   zcontent-type)content_type)r   r   r   r   boundary)rA   bytesrL   upperr   r'   r   Zcopy_merge_paramsr.   r2   r   Cookiesset_cookie_headerrb   r	   r   rj   r3   _preparer   r   read)rE   r   r   r   r2   r   r   r   r   r   r   r   r   r=   r=   r>   rF   5  s:    





zRequest.__init__dict[str, str]default_headersr4   c                 C  s   |  D ].\}}| dkr(d| jv r(q| j|| qg }d| jv }d| jv pXd| jv }|sx| jjrx|d| jjf |s| jdv r|d t	|| jj
 | _d S )Ntransfer-encodingzContent-LengthHostzTransfer-Encodings   Host)POSTPUTPATCH)s   Content-Length   0)rC   r6   r2   
setdefaultr   hostr|   netlocr   r.   rK   )rE   r   rN   rO   Zauto_headersZhas_hostZhas_content_lengthr=   r=   r>   r   u  s    

zRequest._preparer   rH   c                 C  s   t | dst | jS N_content)hasattrr   r   rV   r=   r=   r>   r     s    
zRequest.contentc                 C  sH   t | dsBt| jtjsJ d| j| _t| jtsBt| j| _| jS )6
        Read and return the request content.
        r   rT   )r   rA   r   typingIterablert   r   r   rV   r=   r=   r>   r     s    
zRequest.readc                   sX   t | dsRt| jtjsJ ddd | j2 I dH | _t| jtsRt| j| _| jS )r   r   rT   c                   s   g | z3 d H W }|q6 S rQ   r=   r9   partr=   r=   r>   r?     rT   z!Request.aread.<locals>.<listcomp>N)r   rA   r   r   AsyncIterablert   r   r   rV   r=   r=   r>   aread  s    
zRequest.areadrG   c                 C  s,   | j j}t| j}d| d| jd|dS )N<r   r[   z)>)r   r   rG   r   r   )rE   r   r   r=   r=   r>   r     s    
zRequest.__repr__dict[str, typing.Any]c                 C  s   dd | j  D S )Nc                 S  s   i | ]\}}|d vr||qS ))r   r   r=   r9   namerO   r=   r=   r>   rY     s   z(Request.__getstate__.<locals>.<dictcomp>__dict__rC   rV   r=   r=   r>   __getstate__  s    zRequest.__getstate__stater4   c                 C  s0   |  D ]\}}t| || qi | _t | _d S rQ   )rC   setattrr   r   r   rE   r   r   rO   r=   r=   r>   __setstate__  s    zRequest.__setstate__)r   r   r   rF   r   r   r   r   r   r   r   r   r=   r=   r=   r>   r   4  s$   (@r   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 )~ResponseNrJ   )
r2   r   texthtmlr   r   requestr   historydefault_encodingr   r/   zResponseContent | Noner0   r_   r   zRequest | NonezResponseExtensions | Nonezlist[Response] | Nonez#str | typing.Callable[[bytes], str]r1   )status_coder2   r   r   r   r   r   r   r   r   r   r4   c       
         C  s   || _ t|| _|| _d | _|	d u r(i n|	| _|
d u r:g nt|
| _d| _d| _	|| _
|d u rt||||\}}| | || _t|tr|   n|| _d| _d S )NFr   )r   r.   r2   _requestZnext_requestr   rB   r   	is_closedis_stream_consumedr   r
   r   r   rA   r   r   _num_bytes_downloaded)rE   r   r2   r   r   r   r   r   r   r   r   r   r=   r=   r>   rF     s"    



zResponse.__init__r   r   c                 C  s<   |  D ].\}}| dkr(d| jv r(q| j|| qd S )Nr   zcontent-length)rC   r6   r2   r   )rE   r   rN   rO   r=   r=   r>   r     s    zResponse._preparezdatetime.timedeltarH   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   rV   r=   r=   r>   elapsed  s
    
zResponse.elapsed)r   r4   c                 C  s
   || _ d S rQ   )r   )rE   r   r=   r=   r>   r     s    r   c                 C  s   | j du 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   rV   r=   r=   r>   r     s
    
zResponse.requestrP   c                 C  s
   || _ d S rQ   )r   rR   r=   r=   r>   r     s    rG   c                 C  s6   z| j d }W n ty"   Y dS 0 |jdddS d S )Nhttp_versionzHTTP/1.1rI   ignoreerrors)r   ra   rL   )rE   r   r=   r=   r>   r     s
    zResponse.http_versionc                 C  s@   z| j d }W n ty,   t| j Y S 0 |jdddS d S )Nreason_phraserI   r   r   )r   ra   r   Zget_reason_phraser   rL   )rE   r   r=   r=   r>   r   '  s
    zResponse.reason_phraser'   c                 C  s   | j jS )zA
        Returns the URL for which the request was made.
        )r   r   rV   r=   r=   r>   r   0  s    zResponse.urlr   c                 C  s   t | dst | jS r   )r   r   r   rV   r=   r=   r>   r   7  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   rJ   r<   )r   r   r   r   r3   rt   rL   flush)rE   r   decoderr=   r=   r>   r   =  s    
zResponse.textc                 C  sZ   t | dsT| j}|du 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.
        rD   Nr   rJ   )r   charset_encodingr(   rA   r   rG   r   rD   )rE   r3   r=   r=   r>   r3   H  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   rD   rR   r=   r=   r>   r3   ^  s
    
c                 C  s    | j d}|du rdS t|S )zO
        Return the encoding, as specified by the Content-Type header.
        zContent-TypeN)r2   rb   r,   )rE   r   r=   r=   r>   r   l  s    zResponse.charset_encodingr   c              	   C  s   t | dsg }| jjddd}|D ]@}|  }zt| }||  W q" ty`   Y q"Y q"0 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)rd   r   r   )children)r   r2   rm   rg   r6   r   r|   ra   r   r   r   r   )rE   Zdecodersr\   rO   Zdecoder_clsr=   r=   r>   _get_content_decoderw  s     
zResponse._get_content_decoderrc   c                 C  s   t | jS )zU
        A property which is `True` for 1xx status codes, `False` otherwise.
        )r   is_informationalr   rV   r=   r=   r>   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   rV   r=   r=   r>   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   rV   r=   r=   r>   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   rV   r=   r=   r>   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   rV   r=   r=   r>   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   rV   r=   r=   r>   r     s    zResponse.is_errorc                 C  s(   | j tjtjtjtjtjfv o&d| jv 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_REDIRECTr2   rV   r=   r=   r>   has_redirect_location  s    zResponse.has_redirect_locationc                 C  st   | j }|du 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   rb   formatr   )rE   r   messageZstatus_classZerror_typesr   r=   r=   r>   raise_for_status  s*    
zResponse.raise_for_status)kwargsr4   c                 K  s   t j| jfi |S rQ   )jsonlibloadsr   )rE   r   r=   r=   r>   r     s    zResponse.jsonr   c                 C  s$   t | dst | _| j|  | jS )N_cookies)r   r   r  extract_cookiesrV   r=   r=   r>   r     s    
zResponse.cookiesz dict[str | None, dict[str, str]]c                 C  s*   | j d}|du 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   )rb   )r9   r  r=   r=   r>   rY     s   z"Response.links.<locals>.<dictcomp>)r2   rb   r-   )rE   headerr=   r=   r>   links  s    zResponse.linksc                 C  s   | j S rQ   )r   rV   r=   r=   r>   num_bytes_downloaded  s    zResponse.num_bytes_downloadedc                 C  s   d| j  d| j dS )Nz<Response [ ]>)r   r   rV   r=   r=   r>   r     s    zResponse.__repr__r   c                 C  s   dd | j  D S )Nc                 S  s   i | ]\}}|d vr||qS ))r   r   r   r   r=   r   r=   r=   r>   rY     s   z)Response.__getstate__.<locals>.<dictcomp>r   rV   r=   r=   r>   r     s    zResponse.__getstate__r   c                 C  s6   |  D ]\}}t| || qd| _i | _t | _d S )NT)rC   r   r   r   r   r   r   r=   r=   r>   r   !  s
    zResponse.__setstate__c                 C  s    t | dsd|  | _| jS )7
        Read and return the response content.
        r   rT   )r   rt   
iter_bytesr   rV   r=   r=   r>   r   (  s    
zResponse.readz
int | Noneztyping.Iterator[bytes])
chunk_sizer4   c                 c  s   t | drT|du rt| jn|}tdt| jt|dD ]}| j|||  V  q8n|  }t|d}t| jdp | 	 D ]$}|
|}|
|D ]
}|V  qq|| }|
|D ]
}|V  q| D ]
}|V  qW d   n1 s0    Y  dS z
        A byte-iterator over the decoded response content.
        This allows us to handle gzip, deflate, and brotli encoded responses.
        r   Nr   r   r  r   )r   r   r   rangemaxr   r   r   r   iter_rawrL   r   rE   r  ir   chunkerZ	raw_bytesdecodedchunkr=   r=   r>   r  0  s     



zResponse.iter_bytestyping.Iterator[str]c                 c  s   t | jp
dd}t|d}t| jdp |  D ]$}||}||D ]
}|V  qHq0| }||D ]
}|V  qh| D ]
}|V  q|W d   n1 s0    Y  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.
        rJ   r<   r  r  N)r   r3   r   r   r   r  rL   r   rE   r  r   r  Zbyte_contentZtext_contentr  r=   r=   r>   	iter_textG  s    


zResponse.iter_textc                 c  sn   t  }t| jdH |  D ]}||D ]
}|V  q*q| D ]
}|V  q@W d    n1 s`0    Y  d S Nr  )r   r   r   r  rL   r   rE   r   r   liner=   r=   r>   
iter_linesZ  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D | jD ],}|  jt|7  _||D ]
}|V  qvqVW d   n1 s0    Y  | 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   rA   r   r&   r   r   r   r   r   r   rL   r   closerE   r  r  Zraw_stream_bytesr  r=   r=   r>   r  c  s"    

(zResponse.iter_rawc                 C  sZ   t | jtstd| jsVd| _t| jd | j  W d   n1 sL0    Y  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)rA   r   r&   r   r   r   r   r"  rV   r=   r=   r>   r"  }  s    zResponse.closec                   s0   t | ds*ddd |  2 I dH | _| jS )r  r   rT   c                   s   g | z3 d H W }|q6 S rQ   r=   r   r=   r=   r>   r?     rT   z"Response.aread.<locals>.<listcomp>N)r   rt   aiter_bytesr   rV   r=   r=   r>   r     s    
 zResponse.areadztyping.AsyncIterator[bytes]c                 C s   t | drT|du rt| jn|}tdt| jt|dD ]}| j|||  V  q8n|  }t|d}t| jdz | 	 2 z,3 dH W }|
|}|
|D ]
}|V  qq|6 | }|
|D ]
}|V  q| D ]
}|V  qW d   n1 s0    Y  dS r  )r   r   r   r  r  r   r   r   r   	aiter_rawrL   r   r  r=   r=   r>   r%    s     


zResponse.aiter_bytesztyping.AsyncIterator[str]c                 C s   t | jp
dd}t|d}t| jdz |  2 z,3 dH W }||}||D ]
}|V  qPq06 | }||D ]
}|V  qr| D ]
}|V  qW d   n1 s0    Y  dS r  )r   r3   r   r   r   r%  rL   r   r  r=   r=   r>   
aiter_text  s    

zResponse.aiter_textc                 C sx   t  }t| jdR |  2 z"3 d H W }||D ]
}|V  q2q6 | D ]
}|V  qJW d    n1 sj0    Y  d S r  )r   r   r   r'  rL   r   r  r=   r=   r>   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N | j2 z43 dH W }|  jt|7  _||D ]
}|V  q~qV6 W d   n1 s0    Y  | 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   rA   r   r   r   r   r   r   r   r   rL   r   acloser#  r=   r=   r>   r&    s"    
*zResponse.aiter_rawc                   s`   t | jtstd| js\d| _t| jd  | j I dH  W d   n1 sR0    Y  dS )r$  z3Attempted to call an async close on an sync stream.Tr  N)rA   r   r   r   r   r   r   r)  rV   r=   r=   r>   r)    s    zResponse.aclose)N)N)N)N)N)N),r   r   r   rF   r   r   r   r   r   r   r   r   r   r   r3   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=   r>   r     s   (7


%		  
 r   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   r1   )r   r4   c                 C  s   |d u st |trDt | _t |tr| D ]\}}| || q,n`t |trrt | _|D ]\}}| || qZn2t |trt | _|jD ]}| j| qn|| _d S rQ   )	rA   r   r   jarrC   setrB   r   
set_cookie)rE   r   rN   rO   cookier=   r=   r>   rF     s    



zCookies.__init__r   r   r4   c                 C  s(   |  |}| |j}| j|| dS )zO
        Loads any cookies based on the response `Set-Cookie` headers.
        N)_CookieCompatResponse_CookieCompatRequestr   r*  r  )rE   r   Zurllib_responseurllib_requestr=   r=   r>   r  	  s    
zCookies.extract_cookiesr   r   r4   c                 C  s   |  |}| j| dS )zM
        Sets an appropriate 'Cookie:' HTTP header on the `Request`.
        N)r0  r*  add_cookie_header)rE   r   r1  r=   r=   r>   r     s    
zCookies.set_cookie_headerr   /rG   )r   rO   domainpathr4   c                 C  sX   d||dd|t ||d|t |dddddddidd}tf i |}| j| dS )zU
        Set a cookie value by name. May optionally include domain and path.
        r   NF.THttpOnly)versionr   rO   portport_specifiedr5  domain_specifieddomain_initial_dotr6  path_specifiedsecureexpiresdiscardcommentcomment_urlrestrfc2109)rc   
startswithr   r*  r,  )rE   r   rO   r5  r6  r   r-  r=   r=   r>   r+    s(    zCookies.setr0   )r   r`   r5  r6  r4   c                 C  sn   d}| j D ]R}|j|kr
|du s*|j|kr
|du s<|j|kr
|durVd| }t||j}q
|du 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   r5  r6  r   rO   )rE   r   r`   r5  r6  rO   r-  r   r=   r=   r>   rb   3  s    


zCookies.get)r   r5  r6  r4   c                   s\    dur du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 u s$|j krd u s6|jkr|qS rQ   )r   r5  r6  r9   r-  r5  r   r6  r=   r>   r?   Y  s
   
z"Cookies.delete.<locals>.<listcomp>)r*  clearr5  r6  r   )rE   r   r5  r6  remover-  r=   rH  r>   deleteL  s    
zCookies.delete)r5  r6  r4   c                 C  sD   g }|dur| | |dur4|dus*J | | | 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*  rI  )rE   r5  r6  argsr=   r=   r>   rI  d  s    

zCookies.clearc                 C  s$   t |}|jD ]}| j| qd S rQ   )r   r*  r,  )rE   r   r-  r=   r=   r>   ro   q  s    
zCookies.update)r   rO   r4   c                 C  s   |  ||S rQ   )r+  rE   r   rO   r=   r=   r>   r~   v  s    zCookies.__setitem__)r   r4   c                 C  s   |  |}|d u rt||S rQ   )rb   ra   rM  r=   r=   r>   ru   y  s    
zCookies.__getitem__c                 C  s
   |  |S rQ   )rK  )rE   r   r=   r=   r>   r     s    zCookies.__delitem__r   rH   c                 C  s
   t | jS rQ   )r   r*  rV   r=   r=   r>   r     s    zCookies.__len__r  c                 C  s   dd | j D S )Nc                 s  s   | ]}|j V  qd S rQ   )r   rG  r=   r=   r>   	<genexpr>  rT   z#Cookies.__iter__.<locals>.<genexpr>r*  rV   r=   r=   r>   r     s    zCookies.__iter__rc   c                 C  s   | j D ]} dS dS )NTFrO  )rE   rS   r=   r=   r>   __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   rO   r5  rG  r=   r=   r>   r?     s   z$Cookies.__repr__.<locals>.<listcomp>z	<Cookies[r
  )rt   r*  )rE   Zcookies_reprr=   r=   r>   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.
        r   r1   r2  c                   s*   t  jt|jt|j|jd || _d S )N)r   r2   r   )superrF   rG   r   r   r2   r   r   )rE   r   r   r=   r>   rF     s    z%Cookies._CookieCompatRequest.__init__rG   rv   c                   s   t  || || jj|< d S rQ   )rS  add_unredirected_headerr   r2   )rE   rN   rO   rT  r=   r>   rU    s    z4Cookies._CookieCompatRequest.add_unredirected_header)r   r   r   r   rF   rU  __classcell__r=   r=   rT  r>   r0    s   r0  c                   @  s.   e Zd ZdZdddddZddd	d
ZdS )zCookies._CookieCompatResponserR  r   r1   r.  c                 C  s
   || _ d S rQ   )r   )rE   r   r=   r=   r>   rF     s    z&Cookies._CookieCompatResponse.__init__zemail.message.MessagerH   c                 C  s,   t j }| jj D ]\}}|||< q|S rQ   )emailr   Messager   r2   r^   )rE   inforN   rO   r=   r=   r>   rY    s    

z"Cookies._CookieCompatResponse.infoN)r   r   r   r   rF   rY  r=   r=   r=   r>   r/    s   r/  )N)r   r4  )NNN)NN)NN)N)r   r   r   r   rF   r  r   r+  rb   rK  rI  ro   r~   ru   r   r   r   rP  r   urllibr   r   r0  r/  r=   r=   r=   r>   r     s.   	     
r   )D
__future__r   datetimeemail.messagerW  r   r   r   urllib.requestrZ  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-   MutableMappingrG   r.   r   r   r   r=   r=   r=   r>   <module>   s2   (
$	4 
 |     8