Skip to content

Commit 078ff8e

Browse files
committed
Only return json if response is not 204 for add_weight_in
1 parent 749c248 commit 078ff8e

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."""
@@ -681,7 +686,7 @@ def add_body_composition(
681686

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

687692
# Validate inputs
@@ -707,16 +712,15 @@ def add_weigh_in(
707712
"value": weight,
708713
}
709714
logger.debug("Adding weigh-in")
710-
711-
return self.garth.post("connectapi", url, json=payload).json()
715+
return _validate_json_exists(self.garth.post("connectapi", url, json=payload))
712716

713717
def add_weigh_in_with_timestamps(
714718
self,
715719
weight: int | float,
716720
unitKey: str = "kg",
717721
dateTimestamp: str = "",
718722
gmtTimestamp: str = "",
719-
) -> dict[str, Any]:
723+
) -> dict[str, Any] | None:
720724
"""Add a weigh-in with explicit timestamps (default to kg)"""
721725

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

755759
# Make the POST request
756-
return self.garth.post("connectapi", url, json=payload).json()
760+
return _validate_json_exists(self.garth.post("connectapi", url, json=payload))
757761

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

0 commit comments

Comments
 (0)