Skip to content

Commit

Permalink
Merge pull request #4 from kalifg/fix-heartrate-datetime
Browse files Browse the repository at this point in the history
Add support for datetimes, fix heart_rate endpoint
  • Loading branch information
hedgertronic authored Feb 6, 2024
2 parents 152cb83 + 6451219 commit c6772c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ There are nine different API requests that `OuraClient` can make. Full Oura API

### Get Heart Rate

**Method**: `get_heart_rate(start_date: str = <end_date - 1 day>, end_date: str = <today's date>)`
**Method**: `get_heart_rate(start_datetime: str = <end_date - 1 day>, end_datetime: str = <today's date>)`

**Payload**:

- `start_date`: The earliest date for which to get data. Expected in ISO 8601 format (YYYY-MM-DD). Defaults to one day before the `end_date` parameter.
- `end_date`: The latest date for which to get data. Expected in ISO 8601 format (YYYY-MM-DD). Defaults to today's date.
- `start_datetime`: The earliest date for which to get data. Expected in ISO 8601 format (YYYY-MM-DDThh:mm:ss). Defaults to one day before the `end_datetime` parameter.
- `end_datetime`: The latest date for which to get data. Expected in ISO 8601 format (YYYY-MM-DDThh:mm:ss). Defaults to today's date.

**Example Response**:

Expand Down
36 changes: 26 additions & 10 deletions oura_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

from __future__ import annotations

from datetime import date, timedelta
from datetime import date, datetime, timedelta
from typing import Any

import requests
Expand Down Expand Up @@ -298,8 +298,8 @@ def get_daily_readiness(

def get_heart_rate(
self,
start_date: str | None = None,
end_date: str | None = None,
start_datetime: str | None = None,
end_datetime: str | None = None,
) -> list[dict[str, Any]]:
"""Make request to Get Heart Rate endpoint.
Expand All @@ -311,11 +311,14 @@ def get_heart_rate(
data recorded from a Session, see Sessions endpoint.
Args:
start_date (str, optional): The earliest date for which to get data.
Expected in ISO 8601 format (`YYYY-MM-DD`). Defaults to one day
before `end_date`.
end_date (str, optional): The latest date for which to get data. Expected
in ISO 8601 format (`YYYY-MM-DD`). Defaults to today's date.
start_datetime (str, optional): The earliest date for which to get data.
Expected in ISO 8601 format (`YYYY-MM-DDThh:mm:ss`). Time is optional,
will default to 00:00:00. Time zone is also supported. Defaults to
one day before `end_date`.
end_datetime (str, optional): The latest date for which to get data. Expected
in ISO 8601 format (`YYYY-MM-DDThh:mm:ss`). Time is optional, will
default to 00:00:00. Time zone is also supported. Defaults to today's
date.
Returns:
list[dict[str, Any]]: Response JSON data loaded into an object.
Expand All @@ -329,12 +332,12 @@ def get_heart_rate(
...
]
"""
start, end = self._format_dates(start_date, end_date)
start, end = self._format_datetimes(start_datetime, end_datetime)

return self._make_paginated_request(
method="GET",
url_slug="v2/usercollection/heartrate",
params={"start_date": start, "end_date": end},
params={"start_datetime": start, "end_datetime": end},
)

def get_sleep_periods(
Expand Down Expand Up @@ -611,3 +614,16 @@ def _format_dates(
raise ValueError(f"Start date greater than end date: {start} > {end}")

return str(start), str(end)

def _format_datetimes(
self, start_datetime: str | None, end_datetime: str | None
) -> tuple[str, str]:
end = datetime.fromisoformat(end_datetime) if end_datetime else datetime.today()
start = (
datetime.fromisoformat(start_datetime) if start_datetime else end - timedelta(days=1)
)

if start > end:
raise ValueError(f"Start datetime greater than end datetime: {start} > {end}")

return str(start), str(end)

0 comments on commit c6772c1

Please sign in to comment.