orcasgit / python-fitbit

Fitbit API Python Client Implementation
Other
623 stars 330 forks source link

TCX endpoint decoding error error #175

Open Joe-Heffer-Shef opened 1 year ago

Joe-Heffer-Shef commented 1 year ago

The Get Activity TCX has a UTF-8 XML response, so it creates an error when called using the API code which seems to expect a JSON response.

Traceback:

DEBUG:requests_oauthlib.oauth2_session:Invoking 0 protected resource request hooks.
DEBUG:requests_oauthlib.oauth2_session:Adding token {'access_token': '********', 'refresh_token': '********''} to request.
DEBUG:requests_oauthlib.oauth2_session:Requesting url https://api.fitbit.com/1/user/-/activities/123456789d.tcx using method GET.
DEBUG:requests_oauthlib.oauth2_session:Supplying headers {'Accept-Language': 'en_US', 'Authorization': 'Bearer ********'} and data {}
DEBUG:requests_oauthlib.oauth2_session:Passing through key word arguments {'params': {}}.
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.fitbit.com:443
DEBUG:urllib3.connectionpool:https://api.fitbit.com:443 "GET /1/user/-/activities/123456789.tcx HTTP/1.1" 200 1025205
Traceback (most recent call last):
  File "C:\Users\my_username\Miniconda3\envs\fitbit\Lib\site-packages\fitbit\api.py", line 266, in make_request
    rep = json.loads(response.content.decode('utf8'))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\my_username\Miniconda3\envs\fitbit\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\my_username\Miniconda3\envs\fitbit\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\my_username\Miniconda3\envs\fitbit\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\my_username\fitbit\scripts\fitbit\get_activity.py", line 143, in <module>
    main()
  File "C:\Users\my_username\fitbit\scripts\fitbit\get_activity.py", line 128, in main
    data = client.activity_tcx(log_id=55044510633)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\my_username\fitbit\scripts\fitbit\get_activity.py", line 84, in get_activity_tcx
    return self.make_request(url, params=params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\my_username\Miniconda3\envs\fitbit\Lib\site-packages\fitbit\api.py", line 268, in make_request
    raise exceptions.BadResponse
fitbit.exceptions.BadResponse

My environment:

This is the code I'm using to access this endpoint, which I bound as a method to the fitbit.Fitbit class.

def get_activity_tcx(self, log_id, user_id: str = None, **params):
    """
    Get Activity TCX
    https://dev.fitbit.com/build/reference/web-api/activity/get-activity-tcx/
    """
    api, ver, user_id = self._get_common_args(user_id=user_id)
    url = f"{api}/{ver}/user/{user_id}/activities/{log_id}.tcx"
    return self.make_request(url, params=params)

def get_activity_log_list(self, user_id: str = None, **params):
    """
    Get Activity Log List
    https://dev.fitbit.com/build/reference/web-api/activity/get-activity-log-list/

    Usage:
    >>> client.activity_log_list(afterDate='2023-01-01', offset=0,
    >>> limit=100, sort='asc')
    """
    api, ver, user_id = self._get_common_args(user_id=user_id)
    url = f"{api}/{ver}/user/{user_id}/activities/list.json"
    return self.make_request(url, params=params)