Skip to content

Update api version #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fitbit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
__copyright__ = 'Copyright 2012-2017 ORCAS'
__license__ = 'Apache 2.0'

__version__ = '0.3.1'
__release__ = '0.3.1'
__version__ = '0.3.2'
__release__ = '0.3.2'

# Module namespace.

Expand Down
26 changes: 16 additions & 10 deletions fitbit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class FitbitOauth2Client(object):
refresh_token_url = request_token_url

def __init__(self, client_id, client_secret, access_token=None,
refresh_token=None, expires_at=None, refresh_cb=None,
redirect_uri=None, *args, **kwargs):
refresh_token=None, expires_at=None, refresh_cb=None,
redirect_uri=None, *args, **kwargs):
"""
Create a FitbitOauth2Client object. Specify the first 7 parameters if
you have them to access user data. Specify just the first 2 parameters
Expand Down Expand Up @@ -85,6 +85,7 @@ def make_request(self, url, data=None, method=None, **kwargs):

https://dev.fitbit.com/docs/oauth2/#authorization-errors
"""
print("making request to:", url)
data = data or {}
method = method or ('POST' if data else 'GET')
response = self._request(
Expand Down Expand Up @@ -210,8 +211,8 @@ class Fitbit(object):
]

def __init__(self, client_id, client_secret, access_token=None,
refresh_token=None, expires_at=None, refresh_cb=None,
redirect_uri=None, system=US, **kwargs):
refresh_token=None, expires_at=None, refresh_cb=None,
redirect_uri=None, system=US, **kwargs):
"""
Fitbit(<id>, <secret>, access_token=<token>, refresh_token=<token>)
"""
Expand Down Expand Up @@ -298,8 +299,10 @@ def user_profile_update(self, data):
url = "{0}/{1}/user/-/profile.json".format(*self._get_common_args())
return self.make_request(url, data)

def _get_common_args(self, user_id=None):
common_args = (self.API_ENDPOINT, self.API_VERSION,)
def _get_common_args(self, user_id=None, api_version=None):
if api_version is None:
api_version = self.API_VERSION
common_args = (self.API_ENDPOINT, api_version,)
if not user_id:
user_id = '-'
common_args += (user_id,)
Expand Down Expand Up @@ -538,9 +541,12 @@ def time_series(self, resource, user_id=None, base_date='today',
raise ValueError("Period must be one of %s"
% ','.join(Fitbit.PERIODS))
end = period

if 'sleep' in resource:
api_version = 1.2
else:
api_version = None
url = "{0}/{1}/user/{2}/{resource}/date/{base_date}/{end}.json".format(
*self._get_common_args(user_id),
*self._get_common_args(user_id, api_version=api_version),
resource=resource,
base_date=self._get_date_string(base_date),
end=end
Expand Down Expand Up @@ -802,7 +808,7 @@ def get_sleep(self, date):
date should be a datetime.date object.
"""
url = "{0}/{1}/user/-/sleep/date/{year}-{month}-{day}.json".format(
*self._get_common_args(),
*self._get_common_args(api_version=1.2),
year=date.year,
month=date.month,
day=date.day
Expand All @@ -819,7 +825,7 @@ def log_sleep(self, start_time, duration):
'duration': duration,
'date': start_time.strftime("%Y-%m-%d"),
}
url = "{0}/{1}/user/-/sleep.json".format(*self._get_common_args())
url = "{0}/{1}/user/-/sleep.json".format(*self._get_common_args(api_version=1.2))
return self.make_request(url, data=data, method="POST")

def activities_list(self):
Expand Down