keep auth DRY
authorKenneth Reitz <me@kennethreitz.com>
Mon, 19 Dec 2011 00:14:50 +0000 (19:14 -0500)
committerKenneth Reitz <me@kennethreitz.com>
Mon, 19 Dec 2011 00:14:50 +0000 (19:14 -0500)
requests/auth.py

index 042962d8d912efc9bd638879b5f8ef06ec0eba1a..5b429ed30d0d191d0e5950faf0ad65362fc80c7e 100644 (file)
@@ -16,6 +16,12 @@ from urlparse import urlparse
 from .utils import randombytes, parse_dict_header
 
 
+
+def _basic_auth_str(username, password):
+    """Returns a Basic Auth string."""
+    return 'Basic %s' % b64encode('%s:%s' % (username, password))
+
+
 class AuthBase(object):
     """Base class that all auth implementations derive from"""
 
@@ -30,16 +36,14 @@ class HTTPBasicAuth(AuthBase):
         self.password = str(password)
 
     def __call__(self, r):
-        auth_s = b64encode('%s:%s' % (self.username, self.password))
-        r.headers['Authorization'] = ('Basic %s' % auth_s)
+        r.headers['Authorization'] = _basic_auth_str(self.username, self.password)
         return r
 
 
 class HTTPProxyAuth(HTTPBasicAuth):
     """Attaches HTTP Proxy Authenetication to a given Request object."""
     def __call__(self, r):
-        auth_s = b64encode('%s:%s' % (self.username, self.password))
-        r.headers['Proxy-Authorization'] = ('Basic %s' % auth_s)
+        r.headers['Proxy-Authorization'] = _basic_auth_str(self.username, self.password)
         return r