
o
\c           @   sb   d  Z  d d l m Z m Z d d l m Z m Z d e f d     YZ d e f d     YZ d S(	   s   

requests_toolbelt.auth.handler
==============================

This holds all of the implementation details of the Authentication Handler.

i(   t   AuthBaset   HTTPBasicAuth(   t   urlparset
   urlunparset   AuthHandlerc           B   s\   e  Z d  Z d   Z d   Z d   Z d   Z e d    Z d   Z	 d   Z
 d   Z RS(	   s  

    The ``AuthHandler`` object takes a dictionary of domains paired with
    authentication strategies and will use this to determine which credentials
    to use when making a request. For example, you could do the following:

    .. code-block:: python

        from requests import HTTPDigestAuth
        from requests_toolbelt.auth.handler import AuthHandler

        import requests

        auth = AuthHandler({
            'https://api.github.com': ('sigmavirus24', 'fakepassword'),
            'https://example.com': HTTPDigestAuth('username', 'password')
        })

        r = requests.get('https://api.github.com/user', auth=auth)
        # => <Response [200]>
        r = requests.get('https://example.com/some/path', auth=auth)
        # => <Response [200]>

        s = requests.Session()
        s.auth = auth
        r = s.get('https://api.github.com/user')
        # => <Response [200]>

    .. warning::

        :class:`requests.auth.HTTPDigestAuth` is not yet thread-safe. If you
        use :class:`AuthHandler` across multiple threads you should
        instantiate a new AuthHandler for each thread with a new
        HTTPDigestAuth instance for each thread.

    c         C   s   t  |  |  _ |  j   d  S(   N(   t   dictt
   strategiest   _make_uniform(   t   selfR   (    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyt   __init__6   s    c         C   s   |  j  | j  } | |  S(   N(   t   get_strategy_fort   url(   R   t   requestt   auth(    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyt   __call__:   s    c         C   s   d j  |  j  S(   Ns   <AuthHandler({0!r})>(   t   formatR   (   R   (    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyt   __repr__>   s    c         C   sI   t  |  j j    } i  |  _ x$ | D] \ } } |  j | |  q% Wd  S(   N(   t   listR   t   itemst   add_strategy(   R   t   existing_strategiest   kt   v(    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyR   A   s    	c         C   s:   t  |   } t | j j   | j j   d d d d f  S(   Nt    (   R   R   t   schemet   lowert   netloc(   R   t   parsed(    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyt   _key_from_urlH   s    c         C   s>   t  | t  r t |   } n  |  j |  } | |  j | <d S(   s  Add a new domain and authentication strategy.

        :param str domain: The domain you wish to match against. For example:
            ``'https://api.github.com'``
        :param str strategy: The authentication strategy you wish to use for
            that domain. For example: ``('username', 'password')`` or
            ``requests.HTTPDigestAuth('username', 'password')``

        .. code-block:: python

            a = AuthHandler({})
            a.add_strategy('https://api.github.com', ('username', 'password'))

        N(   t
   isinstancet   tupleR   R   R   (   R   t   domaint   strategyt   key(    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyR   O   s    c         C   s%   |  j  |  } |  j j | t    S(   s  Retrieve the authentication strategy for a specified URL.

        :param str url: The full URL you will be making a request against. For
            example, ``'https://api.github.com/user'``
        :returns: Callable that adds authentication to a request.

        .. code-block:: python

            import requests
            a = AuthHandler({'example.com', ('foo', 'bar')})
            strategy = a.get_strategy_for('http://example.com/example')
            assert isinstance(strategy, requests.auth.HTTPBasicAuth)

        (   R   R   t   gett   NullAuthStrategy(   R   R   R!   (    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyR
   e   s    c         C   s/   |  j  |  } | |  j k r+ |  j | =n  d S(   sk  Remove the domain and strategy from the collection of strategies.

        :param str domain: The domain you wish remove. For example,
            ``'https://api.github.com'``.

        .. code-block:: python

            a = AuthHandler({'example.com', ('foo', 'bar')})
            a.remove_strategy('example.com')
            assert a.strategies == {}

        N(   R   R   (   R   R   R!   (    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyt   remove_strategyw   s    (   t   __name__t
   __module__t   __doc__R	   R   R   R   t   staticmethodR   R   R
   R$   (    (    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyR      s   %						R#   c           B   s   e  Z d    Z d   Z RS(   c         C   s   d S(   Ns   <NullAuthStrategy>(    (   R   (    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyR      s    c         C   s   | S(   N(    (   R   t   r(    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyR      s    (   R%   R&   R   R   (    (    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyR#      s   	N(	   R'   t   requests.authR    R   t   requests.compatR   R   R   R#   (    (    (    sS   /data/av2000/b2b/venv/lib/python2.7/site-packages/requests_toolbelt/auth/handler.pyt   <module>	   s   z