From 8207ed074e2f02bd5d77e7583807039a25fa116b Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 18 Dec 2011 19:14:50 -0500 Subject: [PATCH] keep auth DRY --- requests/auth.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/requests/auth.py b/requests/auth.py index 042962d..5b429ed 100644 --- a/requests/auth.py +++ b/requests/auth.py @@ -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 -- 2.34.1