U
    {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 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)lowerr3   Tr)   r*   .0kvr3    L/var/www/html/Darija-Ai-API/env/lib/python3.8/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  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)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?   ~   s     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   rU   r=   r>   
<dictcomp>   s     
 z Headers.keys.<locals>.<dictcomp>)r@   keysrU   r=   rU   r>   rY      s    zHeaders.keysztyping.ValuesView[str]c                 C  s^   i }| j D ]J\}}}|| j}|| j}||k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}||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.
        rZ   )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=   rV   rW   rU   r=   r>   r?      s   z'Headers.multi_items.<locals>.<listcomp>rT   rU   r=   rU   r>   multi_items   s    
zHeaders.multi_items
typing.Any)rN   defaultr4   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)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?      s     ,)r6   encoder3   r@   extendsplit)rE   rN   rc   r[   Zsplit_valuesrO   r=   re   r>   get_list   s    zHeaders.get_list)r2   r4   c                 C  s:   t |}| D ]}|| kr| | q| j|j d S rQ   )r.   rY   popr@   rj   )rE   r2   rN   r=   r=   r>   update   s
    zHeaders.updatec                 C  s   t | | jdS )Nr<   )r.   r3   rU   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=   rV   )r9   rS   
header_keyheader_valueZnormalized_keyrE   r=   r>   r?      s   z'Headers.__getitem__.<locals>.<listcomp>rZ   N)r6   ri   r3   r@   joinr`   )rE   rN   rC   r=   rr   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   rd   Z
lookup_keyr=   r>   r?      s   z'Headers.__setitem__.<locals>.<listcomp>r   Nr   )ri   rD   r6   	enumerater@   reversedappend)rE   rN   rO   Zset_key	set_valueZfound_indexesrw   r=   rx   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   rv   Zdel_keyr=   r>   r?     s   z'Headers.__delitem__.<locals>.<listcomp>N)r6   ri   r3   ry   r@   r`   rz   )rE   rN   Zpop_indexesrw   r=   r~   r>   __delitem__   s    
zHeaders.__delitem__c                 C  s$   |  | j}|dd | jD kS )Nc                 S  s   g | ]\}}}|qS r=   r=   )r9   rS   rN   r=   r=   r>   r?     s     z(Headers.__contains__.<locals>.<listcomp>)r6   ri   r3   r@   )rE   rN   rp   r=   r=   r>   __contains__  s    zHeaders.__contains__ztyping.Iterator[typing.Any]c                 C  s   t |  S rQ   )iterrY   rU   r=   r=   r>   __iter__  s    zHeaders.__iter__intc                 C  s
   t | jS rQ   )lenr@   rU   r=   r=   r>   __len__  s    zHeaders.__len__)otherr4   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=   rW   r=   r=   r>   r?      s     z"Headers.__eq__.<locals>.<listcomp>c                 S  s   g | ]\}}}||fqS r=   r=   rW   r=   r=   r>   r?   !  s     )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_str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   rY   r[   rC   r]   ra   rl   rn   ro   rt   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 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 )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_headerra   r	   r   ri   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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)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   rU   r=   r=   r>   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   rA   r   typingIterableAssertionErrorrs   r   r   rU   r=   r=   r>   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 rQ   r=   r9   partr=   r=   r>   r?     s     z!Request.aread.<locals>.<listcomp>N)	r   rA   r   r   AsyncIterabler   rs   r   r   rU   r=   r=   r>   aread  s    
zRequest.areadrG   c                 C  s,   | j j}t| j}d| d| jd|dS )N<r   rZ   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 kr||qS ))r   r   r=   r9   namerO   r=   r=   r>   rX     s    z(Request.__getstate__.<locals>.<dictcomp>__dict__rC   rU   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 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.   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k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   rU   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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   rU   r=   r=   r>   r     s
    
zResponse.requestrP   c                 C  s
   || _ d S rQ   )r   rR   r=   r=   r>   r     s    rG   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.1rI   ignoreerrors)r   r`   rL   )rE   r   r=   r=   r>   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_phraserI   r   r   )r   r`   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   rU   r=   r=   r>   r   0  s    zResponse.urlr   c                 C  s   t | dst | jS r   )r   r   r   rU   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   rs   rL   flush)rE   r   decoderr=   r=   r>   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.
        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krdS t|S )zO
        Return the encoding, as specified by the Content-Type header.
        zContent-TypeN)r2   ra   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 ]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)rc   r   r   )children)r   r2   rl   rf   r6   r   r{   r`   r   r   r   r   )rE   Zdecodersr[   rO   Zdecoder_clsr=   r=   r>   _get_content_decoderw  s     
zResponse._get_content_decoderrb   c                 C  s   t | jS )zU
        A property which is `True` for 1xx status codes, `False` otherwise.
        )r   is_informationalr   rU   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   rU   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   rU   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   rU   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   rU   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   rU   r=   r=   r>   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_REDIRECTr2   rU   r=   r=   r>   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   ra   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|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_cookiesrU   r=   r=   r>   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   )ra   )r9   r  r=   r=   r>   rX     s    z"Response.links.<locals>.<dictcomp>)r2   ra   r-   )rE   headerr=   r=   r>   links  s    zResponse.linksc                 C  s   | j S rQ   )r   rU   r=   r=   r>   num_bytes_downloaded  s    zResponse.num_bytes_downloadedc                 C  s   d| j  d| j dS )Nz<Response [ ]>)r   r   rU   r=   r=   r>   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=   r>   rX     s    z)Response.__getstate__.<locals>.<dictcomp>r   rU   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   r   )r   rs   
iter_bytesr   rU   r=   r=   r>   r   (  s    
zResponse.readz
int | Noneztyping.Iterator[bytes])
chunk_sizer4   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, 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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.
        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  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  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: | 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   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  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)rA   r   r&   r   r   r   r   r$  rU   r=   r=   r>   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 rQ   r=   r   r=   r=   r>   r?     s     z"Response.aread.<locals>.<listcomp>N)r   rs   aiter_bytesr   rU   r=   r=   r>   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_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p |  2 z,3 dH W }||}||D ]
}|V  qPq06 | }||D ]
}|V  qr| D ]
}|V  qW 5 Q R X dS r  )r   r3   r   r   r   r'  rL   r   r  r=   r=   r>   
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)  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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   rA   r   r   r   r   r   r   r   r   rL   r   acloser%  r=   r=   r>   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)rA   r   r   r   r   r   r   r+  rU   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 k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)r2  r,  add_cookie_header)rE   r   r3  r=   r=   r>   r     s    
zCookies.set_cookie_headerr   /rG   )r   rO   domainpathr4   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   rO   portport_specifiedr7  domain_specifieddomain_initial_dotr8  path_specifiedsecureexpiresdiscardcommentcomment_urlrestrfc2109)rb   
startswithr   r,  r.  )rE   r   rO   r7  r8  r  r/  r=   r=   r>   r-    s(    
zCookies.setr0   )r   r_   r7  r8  r4   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   r7  r8  r   rO   )rE   r   r_   r7  r8  rO   r/  r   r=   r=   r>   ra   3  s    


zCookies.get)r   r7  r8  r4   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 rQ   )r   r7  r8  r9   r/  r7  r   r8  r=   r>   r?   Y  s   
 
 
z"Cookies.delete.<locals>.<listcomp>)r,  clearr7  r8  r   )rE   r   r7  r8  remover/  r=   rJ  r>   deleteL  s    
zCookies.delete)r7  r8  r4   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,  rK  )rE   r7  r8  argsr=   r=   r>   rK  d  s    

zCookies.clearc                 C  s$   t |}|jD ]}| j| qd S rQ   )r   r,  r.  )rE   r   r/  r=   r=   r>   rn   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 krt||S rQ   )ra   r`   rO  r=   r=   r>   rt   y  s    
zCookies.__getitem__c                 C  s
   |  |S rQ   )rM  )rE   r   r=   r=   r>   r     s    zCookies.__delitem__r   rH   c                 C  s
   t | jS rQ   )r   r,  rU   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   rI  r=   r=   r>   	<genexpr>  s     z#Cookies.__iter__.<locals>.<genexpr>r,  rU   r=   r=   r>   r     s    zCookies.__iter__rb   c                 C  s   | j D ]} dS dS )NTFrQ  )rE   rS   r=   r=   r>   __bool__  s    
zCookies.__bool__c                 C  s"   d dd | jD }d| dS )NrZ   c              	   S  s*   g | ]"}d |j  d|j d|j dqS )z<Cookie =z for z />)r   rO   r7  rI  r=   r=   r>   r?     s   z$Cookies.__repr__.<locals>.<listcomp>z	<Cookies[r  )rs   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   r4  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   ru   c                   s   t  || || jj|< d S rQ   )rU  add_unredirected_headerr   r2   )rE   rN   rO   rV  r=   r>   rW    s    z4Cookies._CookieCompatRequest.add_unredirected_header)r   r   r   r   rF   rW  __classcell__r=   r=   rV  r>   r2    s   r2  c                   @  s.   e Zd ZdZdddddZddd	d
ZdS )zCookies._CookieCompatResponserT  r   r1   r0  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>   r[    s    

z"Cookies._CookieCompatResponse.infoN)r   r   r   r   rF   r[  r=   r=   r=   r>   r1    s   r1  )N)r   r6  )NNN)NN)NN)N)r   r   r   r   rF   r  r   r-  ra   rM  rK  rn   r}   rt   r   r   r   rR  r   urllibr   r   r2  r1  r=   r=   r=   r>   r     s.   	     
r   )D
__future__r   datetimeemail.messagerY  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'   _utilsr(   r)   r*   r+   r,   r-   MutableMappingrG   r.   r   r   r   r=   r=   r=   r>   <module>   s2   (
$	4 
 |     8