Skip to content

Commit c4075f6

Browse files
committed
Fix add_weight_in bug that would display an error when it succesfully added a weigh in
1 parent c11bd0e commit c4075f6

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

garminconnect/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ def _fmt_ts(dt: datetime) -> str:
9090
# Use ms precision to match server expectations
9191
return dt.replace(tzinfo=None).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
9292

93+
def _validate_json_exists(response: requests.Response) -> dict[str, Any] | None:
94+
if response.status_code == 204:
95+
return None
96+
return response.json()
97+
9398

9499
class Garmin:
95100
"""Class for fetching data from Garmin Connect."""
@@ -675,7 +680,7 @@ def add_body_composition(
675680

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

681686
# Validate inputs
@@ -701,16 +706,15 @@ def add_weigh_in(
701706
"value": weight,
702707
}
703708
logger.debug("Adding weigh-in")
704-
705-
return self.garth.post("connectapi", url, json=payload).json()
709+
return _validate_json_exists(self.garth.post("connectapi", url, json=payload))
706710

707711
def add_weigh_in_with_timestamps(
708712
self,
709713
weight: int | float,
710714
unitKey: str = "kg",
711715
dateTimestamp: str = "",
712716
gmtTimestamp: str = "",
713-
) -> dict[str, Any]:
717+
) -> dict[str, Any] | None:
714718
"""Add a weigh-in with explicit timestamps (default to kg)"""
715719

716720
url = f"{self.garmin_connect_weight_url}/user-weight"
@@ -747,7 +751,7 @@ def add_weigh_in_with_timestamps(
747751
logger.debug("Adding weigh-in with explicit timestamps: %s", payload)
748752

749753
# Make the POST request
750-
return self.garth.post("connectapi", url, json=payload).json()
754+
return _validate_json_exists(self.garth.post("connectapi", url, json=payload))
751755

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

0 commit comments

Comments
 (0)