Make OAuth2Crdentials.get_access_token() method return a

collections.namedtuple rather than a dictionary.
This commit is contained in:
Orest Bolohan
2014-06-02 17:19:54 -07:00
parent 50bcae88b7
commit dff7414cb7
5 changed files with 22 additions and 17 deletions

View File

@@ -31,6 +31,7 @@ import time
import urllib
import urlparse
from collections import namedtuple
from oauth2client import GOOGLE_AUTH_URI
from oauth2client import GOOGLE_REVOKE_URI
from oauth2client import GOOGLE_TOKEN_URI
@@ -75,6 +76,9 @@ SERVICE_ACCOUNT = 'service_account'
# The environment variable pointing the file with local Default Credentials.
GOOGLE_CREDENTIALS_DEFAULT = 'GOOGLE_CREDENTIALS_DEFAULT'
# The access token along with the seconds in which it expires.
AccessTokenInfo = namedtuple('AccessTokenInfo', ['access_token', 'expires_in'])
class Error(Exception):
"""Base error for this module."""
@@ -609,7 +613,8 @@ class OAuth2Credentials(Credentials):
if not http:
http = httplib2.Http()
self.refresh(http)
return {'access_token': self.access_token, 'expires_in': self._expires_in()}
return AccessTokenInfo(access_token=self.access_token,
expires_in=self._expires_in())
def set_store(self, store):
"""Set the Storage for the credential.

View File

@@ -245,8 +245,8 @@ class TestAppAssertionCredentials(unittest.TestCase):
credentials = AppAssertionCredentials(['dummy_scope'])
token = credentials.get_access_token()
self.assertEqual('a_token_123', token['access_token'])
self.assertEqual(None, token['expires_in'])
self.assertEqual('a_token_123', token.access_token)
self.assertEqual(None, token.expires_in)
class TestFlowModel(db.Model):

View File

@@ -126,8 +126,8 @@ class AssertionCredentialsTests(unittest.TestCase):
http.request = httplib2_request
token = credentials.get_access_token(http=http)
self.assertEqual('this-is-a-token', token['access_token'])
self.assertEqual(None, token['expires_in'])
self.assertEqual('this-is-a-token', token.access_token)
self.assertEqual(None, token.expires_in)
m.UnsetStubs()
m.VerifyAll()

View File

@@ -591,14 +591,14 @@ class BasicCredentialsTests(unittest.TestCase):
])
token = self.credentials.get_access_token(http=http)
self.assertEqual('first_token', token['access_token'])
self.assertEqual(1, token['expires_in'])
self.assertEqual('first_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_first, self.credentials.token_response)
token = self.credentials.get_access_token(http=http)
self.assertEqual('first_token', token['access_token'])
self.assertEqual(1, token['expires_in'])
self.assertEqual('first_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_first, self.credentials.token_response)
@@ -606,8 +606,8 @@ class BasicCredentialsTests(unittest.TestCase):
self.assertTrue(self.credentials.access_token_expired)
token = self.credentials.get_access_token(http=http)
self.assertEqual('second_token', token['access_token'])
self.assertEqual(1, token['expires_in'])
self.assertEqual('second_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_second, self.credentials.token_response)

View File

@@ -102,14 +102,14 @@ class ServiceAccountCredentialsTests(unittest.TestCase):
])
token = self.credentials.get_access_token(http=http)
self.assertEqual('first_token', token['access_token'])
self.assertEqual(1, token['expires_in'])
self.assertEqual('first_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_first, self.credentials.token_response)
token = self.credentials.get_access_token(http=http)
self.assertEqual('first_token', token['access_token'])
self.assertEqual(1, token['expires_in'])
self.assertEqual('first_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_first, self.credentials.token_response)
@@ -117,7 +117,7 @@ class ServiceAccountCredentialsTests(unittest.TestCase):
self.assertTrue(self.credentials.access_token_expired)
token = self.credentials.get_access_token(http=http)
self.assertEqual('second_token', token['access_token'])
self.assertEqual(1, token['expires_in'])
self.assertEqual('second_token', token.access_token)
self.assertEqual(1, token.expires_in)
self.assertFalse(self.credentials.access_token_expired)
self.assertEqual(token_response_second, self.credentials.token_response)