[med-svn] [python-fitbit] 01/04: Imported Upstream version 0.2.3

Iain R. Learmonth irl at moszumanska.debian.org
Sun Sep 18 11:39:05 UTC 2016


This is an automated email from the git hooks/post-receive script.

irl pushed a commit to branch master
in repository python-fitbit.

commit 2ec26e537c7694d9c67075206d198d1852996ddc
Author: Iain R. Learmonth <irl at fsfe.org>
Date:   Sat Sep 17 19:15:27 2016 +0100

    Imported Upstream version 0.2.3
---
 CHANGELOG.rst             | 12 ++++++++++++
 TODO                      |  6 ------
 fitbit/__init__.py        |  4 ++--
 fitbit/api.py             | 26 ++++++++++----------------
 fitbit_tests/test_auth.py | 36 +++++++++++++++++++++++-------------
 requirements/base.txt     |  2 +-
 6 files changed, 48 insertions(+), 38 deletions(-)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 49b5c70..d5b1fa8 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,3 +1,15 @@
+0.2.3 (2016-07-06)
+==================
+* Refresh token when it expires
+
+0.2.2 (2016-03-30)
+==================
+* Refresh token bugfixes
+
+0.2.1 (2016-03-28)
+==================
+* Update requirements to use requests-oauthlib>=0.6.1
+
 0.2 (2016-03-23)
 ================
 
diff --git a/TODO b/TODO
deleted file mode 100644
index cc97776..0000000
--- a/TODO
+++ /dev/null
@@ -1,6 +0,0 @@
-TODO
-
-* Public calls only based on consumer_key, (should work, untested)
-* Change Units
-* Docs
-* Tests
diff --git a/fitbit/__init__.py b/fitbit/__init__.py
index aa8cf36..d2b77ca 100644
--- a/fitbit/__init__.py
+++ b/fitbit/__init__.py
@@ -17,8 +17,8 @@ __author_email__ = 'bpitcher at orcasinc.com'
 __copyright__ = 'Copyright 2012-2015 ORCAS'
 __license__ = 'Apache 2.0'
 
-__version__ = '0.2'
-__release__ = '0.2'
+__version__ = '0.2.3'
+__release__ = '0.2.3'
 
 # Module namespace.
 
diff --git a/fitbit/api.py b/fitbit/api.py
index df612e3..d3f8bd5 100644
--- a/fitbit/api.py
+++ b/fitbit/api.py
@@ -1,5 +1,4 @@
 # -*- coding: utf-8 -*-
-import base64
 import datetime
 import json
 import requests
@@ -11,7 +10,7 @@ except ImportError:
     from urllib import urlencode
 
 from requests_oauthlib import OAuth2, OAuth2Session
-from oauthlib.oauth2 import TokenExpiredError
+from oauthlib.oauth2.rfc6749.errors import TokenExpiredError
 from fitbit.exceptions import (BadResponse, DeleteError, HTTPBadRequest,
                                HTTPUnauthorized, HTTPForbidden,
                                HTTPServerError, HTTPConflict, HTTPNotFound,
@@ -68,7 +67,7 @@ class FitbitOauth2Client(object):
         try:
             auth = OAuth2(client_id=self.client_id, token=self.token)
             response = self._request(method, url, data=data, auth=auth, **kwargs)
-        except TokenExpiredError as e:
+        except (HTTPUnauthorized, TokenExpiredError) as e:
             self.refresh_token()
             auth = OAuth2(client_id=self.client_id, token=self.token)
             response = self._request(method, url, data=data, auth=auth, **kwargs)
@@ -79,12 +78,11 @@ class FitbitOauth2Client(object):
         if response.status_code == 401:
             d = json.loads(response.content.decode('utf8'))
             try:
-                if(d['errors'][0]['errorType'] == 'oauth' and
-                    d['errors'][0]['fieldName'] == 'access_token' and
-                    d['errors'][0]['message'].find('Access token invalid or expired:') == 0):
-                            self.refresh_token()
-                            auth = OAuth2(client_id=self.client_id, token=self.token)
-                            response = self._request(method, url, data=data, auth=auth, **kwargs)
+                if(d['errors'][0]['errorType'] == 'expired_token' and
+                    d['errors'][0]['message'].find('Access token expired:') == 0):
+                        self.refresh_token()
+                        auth = OAuth2(client_id=self.client_id, token=self.token)
+                        response = self._request(method, url, data=data, auth=auth, **kwargs)
             except:
                 pass
 
@@ -159,16 +157,12 @@ class FitbitOauth2Client(object):
         obtained in step 2.
         the token is internally saved
         """
-
-        unenc_str = (self.client_id + ':' + self.client_secret).encode('utf8')
-        headers = {
-            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
-            'Authorization': b'Basic ' + base64.b64encode(unenc_str)
-        }
         self.token = self.oauth.refresh_token(
             self.refresh_token_url,
             refresh_token=self.token['refresh_token'],
-            headers=headers)
+            auth=requests.auth.HTTPBasicAuth(self.client_id, self.client_secret)
+        )
+
         return self.token
 
 
diff --git a/fitbit_tests/test_auth.py b/fitbit_tests/test_auth.py
index 6785ca7..be1de74 100644
--- a/fitbit_tests/test_auth.py
+++ b/fitbit_tests/test_auth.py
@@ -1,14 +1,15 @@
 from unittest import TestCase
 from fitbit import Fitbit, FitbitOauth2Client
+from fitbit.exceptions import HTTPUnauthorized
 import mock
 from requests_oauthlib import OAuth2Session
-from oauthlib.oauth2 import TokenExpiredError
 
 
 class Auth2Test(TestCase):
     """Add tests for auth part of API
     mock the oauth library calls to simulate various responses,
-    make sure we call the right oauth calls, respond correctly based on the responses
+    make sure we call the right oauth calls, respond correctly based on the
+    responses
     """
     client_kwargs = {
         'client_id': 'fake_id',
@@ -60,8 +61,8 @@ class Auth2Test(TestCase):
         self.assertEqual("fake_return_refresh_token", retval['refresh_token'])
 
     def test_auto_refresh_token_exception(self):
-        # test of auto_refresh with tokenExpired exception
-        # 1. first call to _request causes a TokenExpired
+        """Test of auto_refresh with Unauthorized exception"""
+        # 1. first call to _request causes a HTTPUnauthorized
         # 2. the token_refresh call is faked
         # 3. the second call to _request returns a valid value
         kwargs = self.client_kwargs
@@ -70,7 +71,10 @@ class Auth2Test(TestCase):
 
         fb = Fitbit(**kwargs)
         with mock.patch.object(FitbitOauth2Client, '_request') as r:
-            r.side_effect = [TokenExpiredError, fake_response(200, 'correct_response')]
+            r.side_effect = [
+                HTTPUnauthorized(fake_response(401, b'correct_response')),
+                fake_response(200, 'correct_response')
+            ]
             with mock.patch.object(OAuth2Session, 'refresh_token') as rt:
                 rt.return_value = {
                     'access_token': 'fake_return_access_token',
@@ -78,13 +82,15 @@ class Auth2Test(TestCase):
                 }
                 retval = fb.client.make_request(Fitbit.API_ENDPOINT + '/1/user/-/profile.json')
         self.assertEqual("correct_response", retval.text)
-        self.assertEqual("fake_return_access_token", fb.client.token['access_token'])
-        self.assertEqual("fake_return_refresh_token", fb.client.token['refresh_token'])
+        self.assertEqual(
+            "fake_return_access_token", fb.client.token['access_token'])
+        self.assertEqual(
+            "fake_return_refresh_token", fb.client.token['refresh_token'])
         self.assertEqual(1, rt.call_count)
         self.assertEqual(2, r.call_count)
 
-    def test_auto_refresh_token_nonException(self):
-        # test of auto_refersh when the exception doesn't fire
+    def test_auto_refresh_token_non_exception(self):
+        """Test of auto_refersh when the exception doesn't fire"""
         # 1. first call to _request causes a 401 expired token response
         # 2. the token_refresh call is faked
         # 3. the second call to _request returns a valid value
@@ -94,8 +100,10 @@ class Auth2Test(TestCase):
 
         fb = Fitbit(**kwargs)
         with mock.patch.object(FitbitOauth2Client, '_request') as r:
-            r.side_effect = [fake_response(401, b'{"errors": [{"message": "Access token invalid or expired: some_token_goes_here", "errorType": "oauth", "fieldName": "access_token"}]}'),
-                             fake_response(200, 'correct_response')]
+            r.side_effect = [
+                fake_response(401, b'{"errors": [{"message": "Access token expired: some_token_goes_here", "errorType": "expired_token", "fieldName": "access_token"}]}'),
+                fake_response(200, 'correct_response')
+            ]
             with mock.patch.object(OAuth2Session, 'refresh_token') as rt:
                 rt.return_value = {
                     'access_token': 'fake_return_access_token',
@@ -103,8 +111,10 @@ class Auth2Test(TestCase):
                 }
                 retval = fb.client.make_request(Fitbit.API_ENDPOINT + '/1/user/-/profile.json')
         self.assertEqual("correct_response", retval.text)
-        self.assertEqual("fake_return_access_token", fb.client.token['access_token'])
-        self.assertEqual("fake_return_refresh_token", fb.client.token['refresh_token'])
+        self.assertEqual(
+            "fake_return_access_token", fb.client.token['access_token'])
+        self.assertEqual(
+            "fake_return_refresh_token", fb.client.token['refresh_token'])
         self.assertEqual(1, rt.call_count)
         self.assertEqual(2, r.call_count)
 
diff --git a/requirements/base.txt b/requirements/base.txt
index faab5be..90630aa 100644
--- a/requirements/base.txt
+++ b/requirements/base.txt
@@ -1,2 +1,2 @@
 python-dateutil>=1.5,<2.5
-requests-oauthlib>=0.4,<1.1
+requests-oauthlib>=0.6.1,<1.1

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/python-fitbit.git



More information about the debian-med-commit mailing list