3 P\8@sHdZddlmZmZddlmZmZGdddeZGdddeZdS) z requests_toolbelt.auth.handler ============================== This holds all of the implementation details of the Authentication Handler. )AuthBase HTTPBasicAuth)urlparse urlunparsec@sTeZdZdZddZddZddZdd Zed d Z d d Z ddZ ddZ dS) 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) # => r = requests.get('https://example.com/some/path', auth=auth) # => s = requests.Session() s.auth = auth r = s.get('https://api.github.com/user') # => .. 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. cCst||_|jdS)N)dict strategies _make_uniform)selfrr /usr/lib/python3.6/handler.py__init__6s zAuthHandler.__init__cCs|j|j}||S)N)get_strategy_forurl)r ZrequestZauthr r r __call__:s zAuthHandler.__call__cCs dj|jS)Nz)formatr)r r r r __repr__>szAuthHandler.__repr__cCs6t|jj}i|_x|D]\}}|j||qWdS)N)listritems add_strategy)r Zexisting_strategieskvr r r r AszAuthHandler._make_uniformcCs(t|}t|jj|jjddddfS)N)rrschemelowerZnetloc)rZparsedr r r _key_from_urlHs zAuthHandler._key_from_urlcCs*t|trt|}|j|}||j|<dS)aAdd 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) isinstancetuplerrr)r domainZstrategykeyr r r rOs  zAuthHandler.add_strategycCs|j|}|jj|tS)aRetrieve 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) )rrgetNullAuthStrategy)r rrr r r res zAuthHandler.get_strategy_forcCs |j|}||jkr|j|=dS)akRemove 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)rr)r rrr r r remove_strategyws  zAuthHandler.remove_strategyN) __name__ __module__ __qualname____doc__r rrr staticmethodrrrr"r r r r rs% rc@seZdZddZddZdS)r!cCsdS)Nzr )r r r r rszNullAuthStrategy.__repr__cCs|S)Nr )r rr r r rszNullAuthStrategy.__call__N)r#r$r%rrr r r r r!sr!N) r&Z requests.authrrZrequests.compatrrrr!r r r r  sz