
ˀX8                 @   sb   d  Z  d d l m Z m Z d d l m Z m Z Gd d   d e  Z Gd d   d e  Z d S)	z

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

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

    )AuthBaseHTTPBasicAuth)urlparse
urlunparsec               @   s|   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z e d
 d    Z	 d d   Z
 d d   Z d d   Z d S)AuthHandlera  

    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)dict
strategies_make_uniform)selfr    r   @/usr/lib/python3/dist-packages/requests_toolbelt/auth/handler.py__init__6   s    zAuthHandler.__init__c             C   s   |  j  | j  } | |  S)N)get_strategy_forurl)r
   ZrequestZauthr   r   r   __call__:   s    zAuthHandler.__call__c             C   s   d j  |  j  S)Nz<AuthHandler({0!r})>)formatr   )r
   r   r   r   __repr__>   s    zAuthHandler.__repr__c             C   sI   t  |  j j    } i  |  _ x$ | D] \ } } |  j | |  q% Wd  S)N)listr   itemsadd_strategy)r
   Zexisting_strategieskvr   r   r   r	   A   s    	zAuthHandler._make_uniformc             C   s:   t  |   } t | j j   | j j   d d d d f  S)N )r   r   schemelowerZnetloc)r   Zparsedr   r   r   _key_from_urlH   s    zAuthHandler._key_from_urlc             C   s;   t  | t  r t |   } |  j |  } | |  j | <d S)a  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)
isinstancetupler   r   r   )r
   domainZstrategykeyr   r   r   r   O   s    zAuthHandler.add_strategyc             C   s%   |  j  |  } |  j j | t    S)a  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   getNullAuthStrategy)r
   r   r   r   r   r   r   e   s    zAuthHandler.get_strategy_forc             C   s,   |  j  |  } | |  j k r( |  j | =d S)ak  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   r   r   r   remove_strategyw   s    zAuthHandler.remove_strategyN)__name__
__module____qualname____doc__r   r   r   r	   staticmethodr   r   r   r"   r   r   r   r   r      s   %r   c               @   s(   e  Z d  Z d d   Z d d   Z d S)r!   c             C   s   d S)Nz<NullAuthStrategy>r   )r
   r   r   r   r      s    zNullAuthStrategy.__repr__c             C   s   | S)Nr   )r
   rr   r   r   r      s    zNullAuthStrategy.__call__N)r#   r$   r%   r   r   r   r   r   r   r!      s   r!   N)	r&   Zrequests.authr   r   Zrequests.compatr   r   r   r!   r   r   r   r   <module>	   s   z