From 6bbe1625990e81ef3382fdc354118aa3d887bffe Mon Sep 17 00:00:00 2001 From: Michael Dwyer Date: Mon, 15 Jan 2024 11:10:52 -0600 Subject: [PATCH 1/2] Add support for datetimes Modify heart_rate endpoint to use these --- oura_ring.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/oura_ring.py b/oura_ring.py index b426e25..4afe0c5 100644 --- a/oura_ring.py +++ b/oura_ring.py @@ -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 @@ -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. @@ -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. @@ -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( @@ -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) From 6451219fe5f1afb1119df627ba4eceaccb7f7d1b Mon Sep 17 00:00:00 2001 From: Michael Dwyer Date: Mon, 29 Jan 2024 13:33:05 -0600 Subject: [PATCH 2/2] Update documentation --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f8d4beb..d6cfea6 100644 --- a/README.md +++ b/README.md @@ -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: str = )` +**Method**: `get_heart_rate(start_datetime: str = , end_datetime: str = )` **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**: