Skip to content

Commit 0e1a6f5

Browse files
committed
Add available and in-progress badges methods, fix typos and method signature
1 parent 74bd4a9 commit 0e1a6f5

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

example.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"x": f"Get Heart Rate Variability data (HRV) for '{today.isoformat()}'",
120120
"z": f"Get progress summary from '{startdate.isoformat()}' to '{today.isoformat()}' for all metrics",
121121
"A": "Get gear, the defaults, activity types and statistics",
122-
"B": f"Get weight-ins from '{startdate.isoformat()}' to '{today.isoformat()}'",
122+
"B": f"Get weigh-ins from '{startdate.isoformat()}' to '{today.isoformat()}'",
123123
"C": f"Get daily weigh-ins for '{today.isoformat()}'",
124124
"D": f"Delete all weigh-ins for '{today.isoformat()}'",
125125
"E": f"Add a weigh-in of {weight}{weightunit} on '{today.isoformat()}'",
@@ -437,6 +437,8 @@ def switch(api, i):
437437
elif i == "i":
438438
# Get earned badges for user
439439
display_json("api.get_earned_badges()", api.get_earned_badges())
440+
# display_json("api.get_available_badges()", api.get_available_badges())
441+
# display_json("api.get_in_progress_badges()", api.get_in_progress_badges())
440442
elif i == "j":
441443
# Get adhoc challenges data from start and limit
442444
display_json(
@@ -708,7 +710,7 @@ def switch(api, i):
708710
f"api.get_gear_stats({uuid}) / {name}", api.get_gear_stats(uuid)
709711
)
710712

711-
# WEIGHT-INS
713+
# WEIGH-INS
712714
elif i == "B":
713715
# Get weigh-ins data
714716
display_json(

garminconnect/__init__.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def __init__(
6767
"/personalrecord-service/personalrecord/prs"
6868
)
6969
self.garmin_connect_earned_badges_url = "/badge-service/badge/earned"
70-
self.garmin_connect_adhoc_challenges_url = (
71-
"/adhocchallenge-service/adHocChallenge/historical"
70+
self.garmin_connect_available_badges_url = (
71+
"/badge-service/badge/available?showExclusiveBadge=true"
7272
)
7373
self.garmin_connect_adhoc_challenges_url = (
7474
"/adhocchallenge-service/adHocChallenge/historical"
@@ -716,14 +716,56 @@ def get_personal_record(self) -> Dict[str, Any]:
716716

717717
return self.connectapi(url)
718718

719-
def get_earned_badges(self) -> Dict[str, Any]:
719+
def get_earned_badges(self) -> List[Dict[str, Any]]:
720720
"""Return earned badges for current user."""
721721

722722
url = self.garmin_connect_earned_badges_url
723723
logger.debug("Requesting earned badges for user")
724724

725725
return self.connectapi(url)
726726

727+
def get_available_badges(self) -> List[Dict[str, Any]]:
728+
"""Return available badges for current user."""
729+
730+
url = self.garmin_connect_available_badges_url
731+
logger.debug("Requesting available badges for user")
732+
733+
return self.connectapi(url)
734+
735+
def get_in_progress_badges(self) -> List[Dict[str, Any]]:
736+
"""Return in progress badges for current user."""
737+
738+
logger.debug("Requesting in progress badges for user")
739+
740+
earned_badges = self.get_earned_badges()
741+
available_badges = self.get_available_badges()
742+
743+
# Filter out badges that are not in progress
744+
def badge_in_progress(badge):
745+
"""Return True if the badge is in progress."""
746+
if "badgeProgressValue" not in badge:
747+
return False
748+
if badge["badgeProgressValue"] is None:
749+
return False
750+
if badge["badgeProgressValue"] == 0:
751+
return False
752+
if badge["badgeProgressValue"] == badge["badgeTargetValue"]:
753+
if (
754+
"badgeLimitCount" not in badge
755+
or badge["badgeLimitCount"] is None
756+
):
757+
return False
758+
return badge["badgeEarnedNumber"] < badge["badgeLimitCount"]
759+
return True
760+
761+
earned_in_progress_badges = list(
762+
filter(badge_in_progress, earned_badges)
763+
)
764+
available_in_progress_badges = list(
765+
filter(badge_in_progress, available_badges)
766+
)
767+
return earned_in_progress_badges + available_in_progress_badges
768+
727769
def get_adhoc_challenges(self, start, limit) -> Dict[str, Any]:
728770
"""Return adhoc challenges for current user."""
729771

0 commit comments

Comments
 (0)