Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions garminconnect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def _fmt_ts(dt: datetime) -> str:
# Use ms precision to match server expectations
return dt.replace(tzinfo=None).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]

def _validate_json_exists(response: requests.Response) -> dict[str, Any] | None:
if response.status_code == 204:
return None
return response.json()


class Garmin:
"""Class for fetching data from Garmin Connect."""
Expand Down Expand Up @@ -681,7 +686,7 @@ def add_body_composition(

def add_weigh_in(
self, weight: int | float, unitKey: str = "kg", timestamp: str = ""
) -> dict[str, Any]:
) -> dict[str, Any] | None:
"""Add a weigh-in (default to kg)"""

# Validate inputs
Expand All @@ -707,16 +712,15 @@ def add_weigh_in(
"value": weight,
}
logger.debug("Adding weigh-in")

return self.garth.post("connectapi", url, json=payload).json()
return _validate_json_exists(self.garth.post("connectapi", url, json=payload))

def add_weigh_in_with_timestamps(
self,
weight: int | float,
unitKey: str = "kg",
dateTimestamp: str = "",
gmtTimestamp: str = "",
) -> dict[str, Any]:
) -> dict[str, Any] | None:
"""Add a weigh-in with explicit timestamps (default to kg)"""

url = f"{self.garmin_connect_weight_url}/user-weight"
Expand Down Expand Up @@ -753,7 +757,7 @@ def add_weigh_in_with_timestamps(
logger.debug("Adding weigh-in with explicit timestamps: %s", payload)

# Make the POST request
return self.garth.post("connectapi", url, json=payload).json()
return _validate_json_exists(self.garth.post("connectapi", url, json=payload))

def get_weigh_ins(self, startdate: str, enddate: str) -> dict[str, Any]:
"""Get weigh-ins between startdate and enddate using format 'YYYY-MM-DD'."""
Expand Down