Skip to content
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

Sleep stages data may require v1.2 API endpoint #128

Open
DanWebster opened this issue Feb 21, 2018 · 14 comments
Open

Sleep stages data may require v1.2 API endpoint #128

DanWebster opened this issue Feb 21, 2018 · 14 comments

Comments

@DanWebster
Copy link

I have noticed that the data coming back from the fitbit.sleep() doesn't return the same format as specified in the current fitbit docs.

In looking through, it looks like most of the endoints are drawing from the Version 1 of the API (GET calls using 1 before the user as per below)
GET https://api.fitbit.com/1/user/[user-id]/activities/date/[date].json

But for the newest Sleep data, they look to be going from a 1.2 version
GET https://api.fitbit.com/1.2/user/[user-id]/sleep/date/[date].json

api.py specifies API_VERSION=1; I haven't been using fitbit data until a few days ago so I am unsure whether this has a historical precedent for API differences between types and keeping backwards compatibility or not.

    API_ENDPOINT = "https://api.fitbit.com"
    AUTHORIZE_ENDPOINT = "https://www.fitbit.com"
    API_VERSION = 1```
@brad
Copy link
Member

brad commented Feb 21, 2018

🤔 @DanWebster Maybe we should make the version specifiable so people could use the version they want?

@DanWebster
Copy link
Author

Would it be better to make this set-able when initializing the OAuth2 or expose this for the different endpoints that feed into make_request?

I will check today if making a 1.2 call breaks a few of the more canonical datasets and respond back here. Thanks for the quick response, btw.

@brad
Copy link
Member

brad commented Feb 21, 2018

Oh that's interesting, so you could specify a different version depending upon the endpoint. Maybe you could specify a default version to use for all requests when you create the fitbit client, but then you could also override it for a specific request

@brad
Copy link
Member

brad commented Feb 21, 2018

@DanWebster Thanks for checking that 👍

@DanWebster
Copy link
Author

Can confirm that hardcoding in API_VERSION = 1.2 (line 23, line 190 in api.py) does update the intraday sleep data (see example output below) but still maintains other data type integrity (I checked intraday HR data but did not do an exhaustive test of all API calls).

I think that this means being able to specify API version as a parameter when setting up the oauth client (rather than in each specific make_request call) would be perfect and provide flexibility as the API versions update. Thanks!!

     {'dateTime': '2018-02-18T06:00:00.000', 'level': 'wake', 'seconds': 30},
     {'dateTime': '2018-02-18T06:50:30.000', 'level': 'wake', 'seconds': 30},
     {'dateTime': '2018-02-18T07:14:00.000', 'level': 'wake', 'seconds': 60}],
    'summary': {'deep': {'count': 4, 'minutes': 42, 'thirtyDayAvgMinutes': 44},
     'light': {'count': 22, 'minutes': 331, 'thirtyDayAvgMinutes': 229},
     'rem': {'count': 7, 'minutes': 68, 'thirtyDayAvgMinutes': 84},
     'wake': {'count': 24, 'minutes': 57, 'thirtyDayAvgMinutes': 47}}},```

@brad
Copy link
Member

brad commented Feb 21, 2018

@DanWebster Awesome, thank you for the testing!

@Dannie1G
Copy link

@DanWebster Thanks for that. I have hard coded the api version as you mentioned to get detailed sleep stages. Have you noticed any breaks on the code from that change?

@DanWebster
Copy link
Author

@Dannie1G Seems to work for the endpoints I am looking at (admittedly not exhaustive) with no breaks.

@aanchan
Copy link

aanchan commented May 9, 2018

I'm able to get the sleep data assuming that I know the date. I had to change the hardcoded API version just the same as this issue. Regarding additional endpoints, I am trying to get a list of nights the user has slept : https://dev.fitbit.com/build/reference/web-api/sleep/ - Get Sleep Logs List using the GET https://api.fitbit.com/1.2/user/-/sleep/list.json endpoint. I am not able to figure out from the documentation if getting a list of nights is supported by orcas-fitbit.

@Dannie1G
Copy link

Dannie1G commented May 9, 2018

@ANNCHAN not sure what kind of list you want. I’ve written a little script and posted as jupyter notebook (https://github.com/Dannie1G/python-fitbit/blob/master/Fitbit_APIv12DataCollection_gh.ipynb). In one I collect my sleep stages for each night as separate csv, and in another I collect sleep stats for dates that fall in a specific period of time. Have a look and let me know if you need help following it.

@aanchan
Copy link

aanchan commented May 9, 2018

@Dannie1G Your code assumes that you know the last date at which you want to stop from the current date.
Example : >> last_record = datetime.date(2018, 4, 14) --> In [10].
Fitbit allows you to do this in v1.2 :
GET https://api.fitbit.com/1.2/user/-/sleep/list.json?beforeDate=2017-03-27&sort=desc&offset=0&limit=1
which basically gives you all sleep records before a certain date. For example if you needed all the sleep records before today you could just do a datetime.now() in your code and construct the above string. python-fitbit does not yet support https://api.fitbit.com/1.2/user/-/sleep/list.json endpoint. It should be simple. I'm just making a method like get_sleep(date=date) and instead calling it get_sleep_before(date=date) to support this end-point. Not sure if I should submit a PR once I do this.

@Dannie1G
Copy link

Dannie1G commented May 9, 2018

@aanchan I see.. yeah that makes sense, it could be handy for others PR sounds reasonable

@frejonb
Copy link

frejonb commented Jun 5, 2018

is it necessary to hardcode the API_VERSION? why not do

authd_client.API_VERSION = 1.2

@kcinnick
Copy link

@aanchan did you ever decide to submit a PR for this? I just ran into this issue and it seems a bit more pertinent now that Fitbit has deprecated the 1.0 endpoints: https://dev.fitbit.com/build/reference/web-api/sleep-v1/

dwoffinden added a commit to dwoffinden/fitbit-googlefit that referenced this issue Mar 14, 2021
- Fix pre-midnight sleeps being shifted forwards a day, in the new API
  the point includes the full date and time, avoiding the confusion.
- Fix stages being uncategoried, the old code checked for 1/2/3 as ints
  when they were actually strings, the new API includes more types, and
  uses strings.
- I think the new API is also more granular (down to 30s) but less dense
  (it returns one point per stage, and tells you how long it lasted).

Old: https://dev.fitbit.com/build/reference/web-api/sleep-v1
New: https://dev.fitbit.com/build/reference/web-api/sleep

Workaround to enable it:
orcasgit/python-fitbit#128

TODO: switch to the new GFit sleep segment API:
https://developers.google.com/android/reference/com/google/android/gms/fitness/data/SleepStages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants