1212
1313from requests_oauthlib import OAuth1 , OAuth1Session , OAuth2 , OAuth2Session
1414from oauthlib .oauth2 import TokenExpiredError
15- from oauthlib .common import urldecode
1615from fitbit .exceptions import (BadResponse , DeleteError , HTTPBadRequest ,
1716 HTTPUnauthorized , HTTPForbidden ,
1817 HTTPServerError , HTTPConflict , HTTPNotFound ,
@@ -154,9 +153,9 @@ class FitbitOauth2Client(object):
154153 access_token_url = request_token_url
155154 refresh_token_url = request_token_url
156155
157- def __init__ (self , client_id , client_secret ,
158- access_token = None , refresh_token = None ,
159- * args , ** kwargs ):
156+ def __init__ (self , client_id , client_secret ,
157+ access_token = None , refresh_token = None ,
158+ * args , ** kwargs ):
160159 """
161160 Create a FitbitOauth2Client object. Specify the first 7 parameters if
162161 you have them to access user data. Specify just the first 2 parameters
@@ -169,8 +168,10 @@ def __init__(self, client_id , client_secret,
169168 self .session = requests .Session ()
170169 self .client_id = client_id
171170 self .client_secret = client_secret
172- self .token = {'access_token' : access_token ,
173- 'refresh_token' : refresh_token }
171+ self .token = {
172+ 'access_token' : access_token ,
173+ 'refresh_token' : refresh_token
174+ }
174175 self .oauth = OAuth2Session (client_id )
175176
176177 def _request (self , method , url , ** kwargs ):
@@ -196,14 +197,14 @@ def make_request(self, url, data={}, method=None, **kwargs):
196197 auth = OAuth2 (client_id = self .client_id , token = self .token )
197198 response = self ._request (method , url , data = data , auth = auth , ** kwargs )
198199
199- #yet another token expiration check
200- #(the above try/except only applies if the expired token was obtained
201- #using the current instance of the class this is a a general case)
200+ # yet another token expiration check
201+ # (the above try/except only applies if the expired token was obtained
202+ # using the current instance of the class this is a a general case)
202203 if response .status_code == 401 :
203204 d = json .loads (response .content .decode ('utf8' ))
204205 try :
205- if (d ['errors' ][0 ]['errorType' ]== 'oauth' and
206- d ['errors' ][0 ]['fieldName' ]== 'access_token' and
206+ if (d ['errors' ][0 ]['errorType' ] == 'oauth' and
207+ d ['errors' ][0 ]['fieldName' ] == 'access_token' and
207208 d ['errors' ][0 ]['message' ].find ('Access token invalid or expired:' )== 0 ):
208209 self .refresh_token ()
209210 auth = OAuth2 (client_id = self .client_id , token = self .token )
@@ -241,19 +242,21 @@ def authorize_token_url(self, scope=None, redirect_uri=None, **kwargs):
241242 for more info see https://wiki.fitbit.com/display/API/OAuth+2.0
242243 """
243244
244- # the scope parameter is caussing some issues when refreshing tokens
245- # so not saving it
246- old_scope = self .oauth .scope ;
247- old_redirect = self .oauth .redirect_uri ;
245+ # the scope parameter is caussing some issues when refreshing tokens
246+ # so not saving it
247+ old_scope = self .oauth .scope
248+ old_redirect = self .oauth .redirect_uri
248249 if scope :
249- self .oauth .scope = scope
250+ self .oauth .scope = scope
250251 else :
251- self .oauth .scope = ["activity" , "nutrition" ,"heartrate" ,"location" , "nutrition" ,"profile" ,"settings" ,"sleep" ,"social" ,"weight" ]
252+ self .oauth .scope = [
253+ "activity" , "nutrition" , "heartrate" , "location" , "nutrition" ,
254+ "profile" , "settings" , "sleep" , "social" , "weight"
255+ ]
252256
253257 if redirect_uri :
254258 self .oauth .redirect_uri = redirect_uri
255259
256-
257260 out = self .oauth .authorization_url (self .authorization_url , ** kwargs )
258261 self .oauth .scope = old_scope
259262 self .oauth .redirect_uri = old_redirect
@@ -293,8 +296,6 @@ def refresh_token(self):
293296 return self .token
294297
295298
296-
297-
298299class Fitbit (object ):
299300 US = 'en_US'
300301 METRIC = 'en_UK'
@@ -352,7 +353,7 @@ def __init__(self, client_key, client_secret, oauth2=False, system=US, **kwargs)
352353 qualifier = qualifier ))
353354
354355 def make_request (self , * args , ** kwargs ):
355- ##@ This should handle data level errors, improper requests, and bad
356+ # This should handle data level errors, improper requests, and bad
356357 # serialization
357358 headers = kwargs .get ('headers' , {})
358359 headers .update ({'Accept-Language' : self .system })
@@ -525,8 +526,11 @@ def body_weight_goal(self, start_date=None, start_weight=None, weight=None):
525526 * ``start_weight`` -- Weight goal start weight; in the format X.XX
526527 * ``weight`` -- Weight goal target weight; in the format X.XX
527528 """
528- data = self ._filter_nones (
529- {'startDate' : start_date , 'startWeight' : start_weight , 'weight' : weight })
529+ data = self ._filter_nones ({
530+ 'startDate' : start_date ,
531+ 'startWeight' : start_weight ,
532+ 'weight' : weight
533+ })
530534 if data and not ('startDate' in data and 'startWeight' in data ):
531535 raise ValueError ('start_date and start_weight are both required' )
532536 return self ._resource_goal ('body/log/weight' , data )
@@ -550,9 +554,13 @@ def activities_daily_goal(self, calories_out=None, active_minutes=None,
550554 * ``distance`` -- New goal value; in the format X.XX or integer
551555 * ``steps`` -- New goal value; in an integer format
552556 """
553- data = self ._filter_nones (
554- {'caloriesOut' : calories_out , 'activeMinutes' : active_minutes ,
555- 'floors' : floors , 'distance' : distance , 'steps' : steps })
557+ data = self ._filter_nones ({
558+ 'caloriesOut' : calories_out ,
559+ 'activeMinutes' : active_minutes ,
560+ 'floors' : floors ,
561+ 'distance' : distance ,
562+ 'steps' : steps
563+ })
556564 return self ._resource_goal ('activities' , data , period = 'daily' )
557565
558566 def activities_weekly_goal (self , distance = None , floors = None , steps = None ):
@@ -747,7 +755,7 @@ def log_activity(self, data):
747755 https://wiki.fitbit.com/display/API/API-Log-Activity
748756 """
749757 url = "{0}/{1}/user/-/activities.json" .format (* self ._get_common_args ())
750- return self .make_request (url , data = data )
758+ return self .make_request (url , data = data )
751759
752760 def delete_favorite_activity (self , activity_id ):
753761 """
@@ -810,8 +818,9 @@ def get_alarms(self, device_id):
810818 )
811819 return self .make_request (url )
812820
813- def add_alarm (self , device_id , alarm_time , week_days , recurring = False , enabled = True , label = None ,
814- snooze_length = None , snooze_count = None , vibe = 'DEFAULT' ):
821+ def add_alarm (self , device_id , alarm_time , week_days , recurring = False ,
822+ enabled = True , label = None , snooze_length = None ,
823+ snooze_count = None , vibe = 'DEFAULT' ):
815824 """
816825 https://wiki.fitbit.com/display/API/API-Devices-Add-Alarm
817826 alarm_time should be a timezone aware datetime object.
0 commit comments