Skip to content

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

Description

@DanWebster

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```

Activity

  1. brad commented on Feb 21, 2018

    @brad
    Member

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

  2. DanWebster commented on Feb 21, 2018

    @DanWebster
    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.

  3. brad commented on Feb 21, 2018

    @brad
    Member

    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

  4. brad commented on Feb 21, 2018

    @brad
    Member

    @DanWebster Thanks for checking that 👍

  5. DanWebster commented on Feb 21, 2018

    @DanWebster
    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}}},```
    
    
  6. brad commented on Feb 21, 2018

    @brad
    Member

    @DanWebster Awesome, thank you for the testing!

  7. Dannie1G commented on Apr 28, 2018

    @Dannie1G

    @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?

  8. DanWebster commented on Apr 29, 2018

    @DanWebster
    Author

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

  9. aanchan commented on May 9, 2018

    @aanchan

    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.

  10. Dannie1G commented on May 9, 2018

    @Dannie1G

    @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.

  11. aanchan commented on May 9, 2018

    @aanchan

    @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.

  12. Dannie1G commented on May 9, 2018

    @Dannie1G

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

  13. frejonb commented on Jun 5, 2018

    @frejonb

    is it necessary to hardcode the API_VERSION? why not do

    authd_client.API_VERSION = 1.2

  14. kcinnick commented on Sep 15, 2020

    @kcinnick

    @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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions