Permalink
Browse files

rollingback to 7d1368a to address session persistence issues

  • Loading branch information...
maxcountryman committed Jun 1, 2012
1 parent 105b98f commit 9198ad9a4027a73acc3127083922c63f4c18f765
Showing with 37 additions and 47 deletions.
  1. BIN docs/_build/html/docs.zip
  2. +1 −1 rauth/__init__.py
  3. +32 −38 rauth/service.py
  4. +4 −8 tests/test_service.py
View
Binary file not shown.
View
@@ -6,4 +6,4 @@
'''
-__version__ = '0.4.3'
+__version__ = '0.4.4'
View
@@ -379,51 +379,33 @@ def __init__(self, name, consumer_key, consumer_secret, request_token_url,
# set to True to use header authentication for this service
self.header_auth = header_auth
- self.auth_session = None
-
def _construct_session(self, **kwargs):
'''Construct the request session, supplying the consumer key and
secret.
:param \*\*kwargs: Extra keyworded arguments to be passed to the
OAuth1Hook constructor.
'''
- self.hook = OAuth1Hook(consumer_key=self.consumer_key,
- consumer_secret=self.consumer_secret,
- **kwargs)
- return requests.session(hooks={'pre_request': self.hook})
-
- def _get_session(self, **kwargs):
- '''Get the request session, update attributes on the hook instance.
-
- :param \*\*kwargs: Updates for access_token, access_token_secret,
- header_auth.'''
- access_token = kwargs.get('access_token')
- access_token_secret = kwargs.get('access_token_secret')
- header_auth = kwargs.get('header_auth')
-
- if self.auth_session is None:
- self.auth_session = \
- self._construct_session(header_auth=self.header_auth)
-
- if access_token is not None:
- self.hook.access_token = access_token
- if access_token_secret is not None:
- self.hook.access_token_secret = access_token_secret
- self.hook.header_auth = header_auth or self.header_auth
- return self.auth_session
+ hook = OAuth1Hook(consumer_key=self.consumer_key,
+ consumer_secret=self.consumer_secret,
+ **kwargs)
+ return requests.session(hooks={'pre_request': hook})
def get_request_token(self, method='GET', **kwargs):
'''Gets a request token from the request token endpoint.
:param method: A string representation of the HTTP method to be used.
:param \*\*kwargs: Optional arguments. Same as Requests.
'''
- auth_session = self._get_session()
+ auth_session = \
+ self._construct_session(header_auth=self.header_auth)
+
response = auth_session.request(method,
self.request_token_url,
**kwargs)
+
response.raise_for_status()
+
data = dict(parse_qsl(response.content))
return data['oauth_token'], data['oauth_token_secret']
@@ -454,17 +436,31 @@ def get_access_token(self, method='GET', **kwargs):
request_token = kwargs.pop('request_token')
request_token_secret = kwargs.pop('request_token_secret')
- auth_session = \
- self._get_session(access_token=request_token,
- access_token_secret=request_token_secret,
- header_auth=self.header_auth)
+ auth_session = self._construct_session(
+ access_token=request_token,
+ access_token_secret=request_token_secret,
+ header_auth=self.header_auth)
response = auth_session.request(method,
self.access_token_url,
**kwargs)
return 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, defaults to False.
+ '''
+ return self._construct_session(access_token=access_token,
+ access_token_secret=access_token_secret,
+ header_auth=header_auth)
+
def request(self, method, url, **kwargs):
'''Makes a request using :class:`_construct_session`.
@@ -475,20 +471,18 @@ def request(self, method, url, **kwargs):
: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, defaults to None.
+ :param header_auth: Authenication via header, defaults to False.
:param allow_redirects: Allows a request to redirect, defaults to True.
:param \*\*kwargs: Optional arguments. Same as Requests.
'''
access_token = kwargs.pop('access_token')
access_token_secret = kwargs.pop('access_token_secret')
- header_auth = kwargs.pop('header_auth', None)
+ header_auth = kwargs.pop('header_auth', False)
allow_redirects = kwargs.pop('allow_redirects', True)
-
- # grab session constructed on initialization
auth_session = \
- self._get_session(access_token=access_token,
- access_token_secret=access_token_secret,
- header_auth=header_auth)
+ self._construct_session(access_token=access_token,
+ access_token_secret=access_token_secret,
+ header_auth=header_auth)
response = auth_session.request(method,
url,
View
@@ -328,25 +328,21 @@ def test_get_access_token_bad_response(self, mock_request):
def test_get_authenticated_session(self):
auth_session = \
- self.service._get_session(access_token='123',
- access_token_secret='456')
+ self.service.get_authenticated_session(access_token='123',
+ access_token_secret='456')
self.assertIsNotNone(auth_session)
@patch.object(requests.Session, 'request')
def test_use_authenticated_session(self, mock_request):
mock_request.return_value = self.response
auth_session = \
- self.service._get_session(access_token='123',
- access_token_secret='456')
+ self.service.get_authenticated_session(access_token='123',
+ access_token_secret='456')
response = auth_session.get('http://example.com/foobar').content
self.assertIsNotNone(response)
self.assertEqual('oauth_token=123&oauth_token_secret=456', response)
- self.assertIsNotNone(self.service.hook.access_token)
- self.assertIsNotNone(self.service.hook.access_token_secret)
- self.assertEqual(self.service.hook.access_token, '123')
- self.assertEqual(self.service.hook.access_token_secret, '456')
@patch.object(requests.Session, 'request')
def test_request(self, mock_request):

0 comments on commit 9198ad9

Please sign in to comment.