
R\K                 @   s  d  Z  d d l Z d d l m Z d d l m Z d d l m Z m Z m Z m	 Z	 m
 Z
 d d l m Z m Z m Z m Z d d l m Z m Z d d	 l m Z m Z d d
 l m Z m Z d d l m Z m Z d d l m Z d d l m Z d d l m  Z  m! Z! m" Z" d d l# m$ Z$ e$ j% e$ j& e$ j' e$ j( f Z) d Z* e d d  Z+ e d d  Z, Gd d   d e-  Z. Gd d   d e.  Z/ d d   Z0 d S)z
requests.session
~~~~~~~~~~~~~~~~

This module provides a Session object to manage and persist settings across
requests (cookies, auth, proxies).

    N)Mapping)datetime   )	cookielibOrderedDicturljoinurlparsebuiltin_str)cookiejar_from_dictextract_cookies_to_jarRequestsCookieJarmerge_cookies)RequestPreparedRequest)default_hooksdispatch_hook)to_key_val_listdefault_headers)TooManyRedirectsInvalidSchema)CaseInsensitiveDict)HTTPAdapter)requote_uriget_environ_proxiesget_netrc_auth)codes   c             C   s   | d k r |  S|  d k r  | St  | t  o; t  |  t  sB |  S| t |   } | j t |    x0 |  j   D]" \ } } | d k rt | | =qt qt W| S)z
    Determines appropriate setting for a given request, taking into account the
    explicit setting on that request, and the setting in the session. If a
    setting is a dictionary, they will be merged together using `dict_class`
    N)
isinstancer   r   updateitems)request_settingsession_setting
dict_classmerged_settingkv r&   @/var/www/dbchiro/venv/build/pip/pip/_vendor/requests/sessions.pymerge_setting&   s    r(   c             C   sZ   | d k s! | j  d  g  k r% |  S|  d k sF |  j  d  g  k rJ | St |  | |  S)z
    Properly merges both requests and session hooks.

    This is necessary because when request_hooks == {'response': []}, the
    merge breaks Session hooks entirely.
    Nresponse)getr(   )request_hookssession_hooksr"   r&   r&   r'   merge_hooksE   s
    !!r-   c               @   s+   e  Z d  Z d d d d d d d  Z d S)SessionRedirectMixinFNTc             c   s  d } xd | j  k r| j t k r| j   }	 | j | |  j k r_ t d |  j   n  | j   | j  d }
 | j } |
 j	 d  r t
 | j  } d | j |
 f }
 n  t
 |
  } | j   }
 t
 |
  j s t | j t |
   }
 n t |
  }
 |
 |	 _ | j t j k r1| d k r1d } n  | j t j k rX| d k rXd } n  | j t j k r| d k rd } n  | |	 _ | j t j t j f k rd	 |	 j  k r|	 j  d	 =n  d
 |	 _ n  |	 j  } y | d =Wn t k
 rYn Xt |	 j |	 | j  |	 j j |  j  |	 j |	 j  |  j |	 d | d | d | d | d | d d } t |  j |	 | j  | d 7} | Vq	 Wd
 S)z6Receives a Response. Returns a generator of Responses.r   locationzExceeded %s redirects.z//z%s:%sHEADGETPOSTzContent-LengthNCookiestreamtimeoutverifycertproxiesallow_redirectsFr   ) headersstatus_codeREDIRECT_STATIcopycontentmax_redirectsr   closemethod
startswithr   urlschemegeturlnetlocr   r   r   	see_otherfoundmoved	temporaryresumebodyKeyErrorr   _cookiesrawr   cookiesprepare_cookiessend)selfrespreqr4   r5   r6   r7   r8   iprepared_requestrC   rA   parsed_rurlparsedr:   r&   r&   r'   resolve_redirectsV   sd    !
									
z&SessionRedirectMixin.resolve_redirects)__name__
__module____qualname__rZ   r&   r&   r&   r'   r.   U   s   r.   c               @   sN  e  Z d  Z d Z d d d d d d d d	 d
 d d d d d g Z d d   Z d d   Z d d   Z d d   Z d d d d d d d d d d d d d d d  Z	 d d   Z
 d d   Z d  d!   Z d d" d#  Z d d$ d%  Z d d& d'  Z d( d)   Z d* d+   Z d, d-   Z d. d/   Z d0 d1   Z d2 d3   Z d4 d5   Z d S)6SessionzA Requests session.

    Provides cookie persistence, connection-pooling, and configuration.

    Basic Usage::

      >>> import requests
      >>> s = requests.Session()
      >>> s.get('http://httpbin.org/get')
      200
    r:   rP   authr5   r8   hooksparamsr6   r7   prefetchadaptersr4   	trust_envr?   c             C   s   t    |  _ d  |  _ i  |  _ t   |  _ i  |  _ d |  _ d |  _ d  |  _	 t
 |  _ d |  _ t i   |  _ t   |  _ |  j d t    |  j d t    d  S)NFTzhttps://zhttp://)r   r:   r_   r8   r   r`   ra   r4   r6   r7   DEFAULT_REDIRECT_LIMITr?   rd   r
   rP   r   rc   mountr   )rS   r&   r&   r'   __init__   s    								zSession.__init__c             C   s   |  S)Nr&   )rS   r&   r&   r'   	__enter__   s    zSession.__enter__c             G   s   |  j    d  S)N)r@   )rS   argsr&   r&   r'   __exit__   s    zSession.__exit__c             C   s!  | j  p i  } t | t j  s0 t |  } n  t t t   |  j   |  } | j } |  j r | r |  j r t	 | j
  } n  t   } | j d | j j   d | j
 d | j d | j d t | j |  j d t d t | j |  j  d t | |  j  d	 | d
 t | j |  j   	| S)ab  Constructs a :class:`PreparedRequest <PreparedRequest>` for
        transmission and returns it. The :class:`PreparedRequest` has settings
        merged from the :class:`Request <Request>` instance and those of the
        :class:`Session`.

        :param request: :class:`Request` instance to prepare with this
            session's settings.
        rA   rC   filesdatar:   r"   ra   r_   rP   r`   )rP   r   r   	CookieJarr
   r   r   r_   rd   r   rC   r   preparerA   upperrk   rl   r(   r:   r   ra   r-   r`   )rS   requestrP   merged_cookiesr_   pr&   r&   r'   prepare_request   s(    							zSession.prepare_requestNTc             C   s  t  |  } t d | j   d | d | d | d | p9 i  d | pE i  d | d | d	 |  	} |  j |  } | pu i  } |  j rt |  p i  } x* | j   D] \ } } | j | |  q W| r | d
 k	 r t j	 j
 d  } n  | r| d
 k	 rt j	 j
 d  } qn  t | |  j  } t | |  j  } t | |  j  } t | |  j  } i | d 6|	 d 6| d 6| d 6| d 6|
 d 6} |  j | |  } | S)a)  Constructs a :class:`Request <Request>`, prepares it and sends it.
        Returns :class:`Response <Response>` object.

        :param method: method for the new :class:`Request` object.
        :param url: URL for the new :class:`Request` object.
        :param params: (optional) Dictionary or bytes to be sent in the query
            string for the :class:`Request`.
        :param data: (optional) Dictionary or bytes to send in the body of the
            :class:`Request`.
        :param headers: (optional) Dictionary of HTTP Headers to send with the
            :class:`Request`.
        :param cookies: (optional) Dict or CookieJar object to send with the
            :class:`Request`.
        :param files: (optional) Dictionary of 'filename': file-like-objects
            for multipart encoding upload.
        :param auth: (optional) Auth tuple or callable to enable
            Basic/Digest/Custom HTTP Auth.
        :param timeout: (optional) Float describing the timeout of the
            request.
        :param allow_redirects: (optional) Boolean. Set to True by default.
        :param proxies: (optional) Dictionary mapping protocol to the URL of
            the proxy.
        :param stream: (optional) whether to immediately download the response
            content. Defaults to ``False``.
        :param verify: (optional) if ``True``, the SSL cert will be verified.
            A CA_BUNDLE path can also be provided.
        :param cert: (optional) if String, path to ssl client cert file (.pem).
            If Tuple, ('cert', 'key') pair.
        rA   rC   r:   rk   rl   ra   r_   rP   r`   FREQUESTS_CA_BUNDLECURL_CA_BUNDLEr4   r5   r6   r7   r8   r9   )r	   r   ro   rs   rd   r   r   
setdefaultosenvironr*   r(   r8   r4   r6   r7   rR   )rS   rA   rC   ra   rl   r:   rP   rk   r_   r5   r9   r8   r`   r4   r6   r7   rU   prepenv_proxiesr$   r%   send_kwargsrT   r&   r&   r'   rp   #  sD    ,		
zSession.requestc             K   s#   | j  d d  |  j d | |  S)zSends a GET request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        r9   Tr1   )rv   rp   )rS   rC   kwargsr&   r&   r'   r*     s    zSession.getc             K   s#   | j  d d  |  j d | |  S)zSends a OPTIONS request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        r9   TOPTIONS)rv   rp   )rS   rC   r|   r&   r&   r'   options  s    zSession.optionsc             K   s#   | j  d d  |  j d | |  S)zSends a HEAD request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        r9   Fr0   )rv   rp   )rS   rC   r|   r&   r&   r'   head  s    zSession.headc             K   s   |  j  d | d | | S)a8  Sends a POST request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        r2   rl   )rp   )rS   rC   rl   r|   r&   r&   r'   post  s    zSession.postc             K   s   |  j  d | d | | S)a7  Sends a PUT request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        PUTrl   )rp   )rS   rC   rl   r|   r&   r&   r'   put  s    zSession.putc             K   s   |  j  d | d | | S)a9  Sends a PATCH request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        PATCHrl   )rp   )rS   rC   rl   r|   r&   r&   r'   patch  s    zSession.patchc             K   s   |  j  d | |  S)zSends a DELETE request. Returns :class:`Response` object.

        :param url: URL for the new :class:`Request` object.
        :param \*\*kwargs: Optional arguments that ``request`` takes.
        DELETE)rp   )rS   rC   r|   r&   r&   r'   delete  s    zSession.deletec             K   s  | j  d |  j  | j  d |  j  | j  d |  j  | j  d |  j  t | t  sj t d   n  | j d d  } | j	 d  } | j	 d  } | j	 d  } | j	 d  } | j	 d  } | j
 }	 |  j d	 | j  }
 t j   } |
 j | |  } t j   | | _ t d
 |	 | |  } | j rdx- | j D] } t |  j | j | j  q>Wn  t |  j | | j  |  j | | d | d | d | d | d | } | rd d   | D n g  } | r| j d |  | j   } t |  | _ n  | S)zSend a given PreparedRequest.r4   r6   r7   r8   z#You can only send PreparedRequests.r9   Tr5   rC   r)   c             S   s   g  |  ] } |  q Sr&   r&   ).0rT   r&   r&   r'   
<listcomp>  s   	 z Session.send.<locals>.<listcomp>r   )rv   r4   r6   r7   r8   r   r   
ValueErrorpopr*   r`   get_adapterrC   r   utcnowrR   elapsedr   historyr   rP   rp   rO   rZ   inserttuple)rS   rp   r|   r9   r4   r5   r6   r7   r8   r`   adapterstartrrT   genr   r&   r&   r'   rR     s>    		 	zSession.sendc             C   sM   x6 |  j  j   D]% \ } } | j   j |  r | Sq Wt d |   d S)z>Returns the appropriate connnection adapter for the given URL.z*No connection adapters were found for '%s'N)rc   r   lowerrB   r   )rS   rC   prefixr   r&   r&   r'   r     s    zSession.get_adapterc             C   s(   x! |  j  j   D] } | j   q Wd S)z+Closes all adapters and as such the sessionN)rc   valuesr@   )rS   r%   r&   r&   r'   r@     s    zSession.closec                sW   | |  j    <  f d d   |  j  D } x' | D] } |  j  j |  |  j  | <q0 Wd S)zkRegisters a connection adapter to a prefix.

        Adapters are sorted in descending order by key length.c                s.   g  |  ]$ } t  |  t     k  r |  q Sr&   )len)r   r$   )r   r&   r'   r     s   	 z!Session.mount.<locals>.<listcomp>N)rc   r   )rS   r   r   keys_to_movekeyr&   )r   r'   rf     s    zSession.mountc                s    t    f d d     j D  S)Nc             3   s'   |  ] } | t    | d   f Vq d  S)N)getattr)r   attr)rS   r&   r'   	<genexpr>  s    z'Session.__getstate__.<locals>.<genexpr>)dict	__attrs__)rS   r&   )rS   r'   __getstate__  s    zSession.__getstate__c             C   s1   x* | j    D] \ } } t |  | |  q Wd  S)N)r   setattr)rS   stater   valuer&   r&   r'   __setstate__!  s    zSession.__setstate__)r[   r\   r]   __doc__r   rg   rh   rj   rs   rp   r*   r~   r   r   r   r   r   rR   r   r@   rf   r   r   r&   r&   r&   r'   r^      sD   3(S





	>
	r^   c               C   s   t    S)z2Returns a :class:`Session` for context-management.)r^   r&   r&   r&   r'   session&  s    r   )1r   rw   collectionsr   r   compatr   r   r   r   r	   rP   r
   r   r   r   modelsr   r   r`   r   r   utilsr   r   
exceptionsr   r   
structuresr   rc   r   r   r   r   status_codesr   rI   rH   othertemporary_movedr<   re   r(   r-   objectr.   r^   r   r&   r&   r&   r'   <module>
   s0   ("\ v