Skip to content

fix: auto-chunk breathing rate and HRV date ranges > 30 days - #48

Draft
veerendra2 wants to merge 3 commits into
mainfrom
fix/breathing-hrv-date-range
Draft

fix: auto-chunk breathing rate and HRV date ranges > 30 days#48
veerendra2 wants to merge 3 commits into
mainfrom
fix/breathing-hrv-date-range

Conversation

@veerendra2

Copy link
Copy Markdown
Owner

Overview

Fixes #35 by automatically splitting date ranges > 30 days into <= 30-day chunks for Breathing Rate and HRV endpoints.

Changes

  • ****: Added private helper that detects ranges exceeding 30 days, splits them into 30-day windows, fetches each chunk sequentially, and aggregates response items ( and ).
  • ****: Added unit test coverage for single dates, <= 30 day ranges, and multi-chunk > 30 day ranges for breathing rate summary, intraday, and HRV summary.

How to Test

  1. Unit tests:
    ============================= test session starts ==============================
    platform linux -- Python 3.13.5, pytest-9.1.1, pluggy-1.6.0
    rootdir: /tmp/fitbit-cli
    configfile: pyproject.toml
    collected 4 items

tests/date_range_test.py .... [100%]

============================== 4 passed in 0.53s ===============================
2. Local execution with relative dates > 30 days:

@veerendra2

Copy link
Copy Markdown
Owner Author

Still failing, here is what I did

pip3 install -e .
...

fitbit-cli -H last-2-months
Traceback (most recent call last):
  File "/Users/veerendra/.local/pipx/venvs/fitbit-cli/lib/python3.14/site-packages/fitbit_cli/fitbit_api.py", line 62, in make_request
    response.raise_for_status()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/veerendra/.local/pipx/venvs/fitbit-cli/lib/python3.14/site-packages/requests/models.py", line 1028, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.fitbit.com/1/user/-/hrv/date/2026-05-28/2026-07-27.json

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/veerendra/.local/bin/fitbit-cli", line 6, in <module>
    sys.exit(main())
             ~~~~^^
  File "/Users/veerendra/.local/pipx/venvs/fitbit-cli/lib/python3.14/site-packages/fitbit_cli/main.py", line 35, in main
    output.table_display(fitbit, args)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/Users/veerendra/.local/pipx/venvs/fitbit-cli/lib/python3.14/site-packages/fitbit_cli/output.py", line 151, in table_display
    fmt.display_hrv(fitbit.get_hrv_summary(*args.hrv))
                    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/Users/veerendra/.local/pipx/venvs/fitbit-cli/lib/python3.14/site-packages/fitbit_cli/fitbit_api.py", line 158, in get_hrv_summary
    response = self.make_request("GET", url)
  File "/Users/veerendra/.local/pipx/venvs/fitbit-cli/lib/python3.14/site-packages/fitbit_cli/fitbit_api.py", line 71, in make_request
    raise FitbitAPIError(f"HTTP error occurred: {response.json()}") from e
fitbit_cli.exceptions.FitbitAPIError: HTTP error occurred: {'error': {'code': 400, 'message': 'Request contains an invalid argument.', 'status': 'INVALID_ARGUMENT'}}

@veerendra2

veerendra2 commented Jul 27, 2026

Copy link
Copy Markdown
Owner Author

@veerendra2 Bumped version to 1.9.0 in fitbit_cli/__init__.py.

Regarding the traceback: line 158 in your log shows:

File ".../fitbit_cli/fitbit_api.py", line 158, in get_hrv_summary
    response = self.make_request("GET", url)

and URL https://api.fitbit.com/1/user/-/hrv/date/2026-05-28/2026-07-27.json. That is the old un-chunked code from main. In PR #48, get_hrv_summary calls _fetch_chunked_data, which splits 2026-05-28/2026-07-27 (60 days) into three <=30 day requests:

  1. 2026-05-28/2026-06-26
  2. 2026-06-27/2026-07-26
  3. 2026-07-27/2026-07-27

To test the PR changes locally, checkout the feature branch first:

git fetch origin
git checkout fix/breathing-hrv-date-range
pip3 install -e .
fitbit-cli -H last-2-months

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

Successfully merging this pull request may close these issues.

Fix breathing and HRV API date range

1 participant