Permalink
Browse files

improving API unification, adding fallback for session wrapper

  • Loading branch information...
maxcountryman committed Mar 13, 2012
1 parent a6ee2ac commit 45b906544deb0ef7c23afd662ec3d73629d57ee3
Showing with 48 additions and 5 deletions.
  1. +7 −1 setup.py
  2. +18 −0 tests/test_service.py
  3. +1 −1 webauth/__init__.py
  4. +22 −3 webauth/service.py
View
@@ -5,9 +5,15 @@
from setuptools import setup, find_packages
if sys.argv[-1] == 'test':
+ nosetests = 'nosetests -v --with-coverage --cover-package=webauth'
+ try:
+ import yanc
+ nosetests += ' --with-yanc'
+ except ImportError:
+ pass
os.system('pyflakes webauth tests; '
'pep8 webauth tests && '
- 'nosetests --with-coverage --cover-package=webauth')
+ + nosetests)
sys.exit()
setup(
View
@@ -226,6 +226,24 @@ def test_get_access_token_bad_response(self, mock_request):
self.service.get_access_token,
('123', '456', 'GET'))
+ def test_get_authenticated_session(self):
+ auth_session = \
+ self.service.get_authenticated_session(access_token='123',
+ access_token_secret='456')
+ self.assertTrue(auth_session is not None)
+
+ @patch.object(requests.Session, 'request')
+ def test_use_authenticated_session(self, mock_request):
+ mock_request.return_value = self.response
+
+ auth_session = \
+ self.service.get_authenticated_session(access_token='123',
+ access_token_secret='456')
+
+ response = auth_session.get('http://example.com/foobar').content
+ self.assertTrue(response is not None)
+ self.assertEqual('oauth_token=123&oauth_token_secret=456', response)
+
@patch.object(requests.Session, 'request')
def test_request_get(self, mock_request):
mock_request.return_value = self.response
View
@@ -6,4 +6,4 @@
'''
-__version__ = '0.1.6'
+__version__ = '0.1.7'
View
@@ -274,8 +274,22 @@ def get_access_token(self, request_token, request_token_secret,
return _parse_response(response)
+ def get_authenticated_session(self, access_token, access_token_secret,
+ header_auth=False):
+ '''Returns an authenticated Requests session utilizing the hook.
+
+ :param access_token: The access token as returned by
+ :class:`get_access_token`
+ :param access_token_secret: The access token secret as returned by
+ :class:`get_access_token`
+ :param header_auth: Authenication via header, defauls to False.
+ '''
+ return self._construct_session(access_token=access_token,
+ access_token_secret=access_token_secret,
+ header_auth=header_auth)
+
def request(self, http_method, url, access_token, access_token_secret,
- header_auth=False, **params):
+ header_auth=False, params=None, data=None):
'''Makes a request using :class:`_construct_session`.
:param http_method: A string representation of the HTTP method to be
@@ -286,15 +300,20 @@ def request(self, http_method, url, access_token, access_token_secret,
:param access_token_secret: The access token secret as returned by
:class:`get_access_token`.
:param header_auth: Authenication via header, defauls to False.
- :param **params: Additional arguments to be added to the request
+ :param params: Additional arguments to be added to the request
querystring.
+ :param data: Additional data to be included in the request body.
'''
auth_session = \
self._construct_session(access_token=access_token,
access_token_secret=access_token_secret,
header_auth=header_auth)
- response = auth_session.request(http_method, url, params=params)
+ response = auth_session.request(http_method,
+ url,
+ params=params,
+ data=data,
+ allow_redirects=True)
response.raise_for_status()

0 comments on commit 45b9065

Please sign in to comment.