Skip to content

Commit 2cf259f

Browse files
committed
Adjustment based on CodeRabbit
1 parent df5cfd0 commit 2cf259f

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The Garmin Connect API library comes with two examples:
44

55
- **`example.py`** - Simple getting-started example showing authentication, token storage, and basic API calls
6-
- **`demo.py`** - Comprehensive demo providing access to **100+ API methods** organized into **11 categories** for easy navigation
6+
- **`demo.py`** - Comprehensive demo providing access to **100+ API methods** organized into **12 categories** for easy navigation
77

88
Note: The demo menu is generated dynamically; exact options may change between releases.
99

@@ -24,7 +24,7 @@ Select a category:
2424
[9] 🎽 Gear & Equipment
2525
[0] 💧 Hydration & Wellness
2626
[a] 🔧 System & Export
27-
[b] 📅 Training plans
27+
[b] 📅 Training plans
2828

2929
[q] Exit program
3030

demo.py

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,32 +1771,60 @@ def get_activity_exercise_sets_data(api: Garmin) -> None:
17711771

17721772

17731773
def get_training_plan_by_id_data(api: Garmin) -> None:
1774-
"""Get training plan by ID. adaptive plans are not supported. use get_adaptive_training_plan_by_id instead"""
1775-
try:
1776-
training_plans = api.get_training_plans()["trainingPlanList"]
1777-
if training_plans:
1778-
plan_id = training_plans[-1]["trainingPlanId"]
1779-
plan_name = training_plans[-1]["name"]
1780-
plan_category = training_plans[-1]["trainingPlanCategory"]
1774+
"""Get training plan details by ID (routes FBT_ADAPTIVE plans to the adaptive endpoint)."""
1775+
resp = api.get_training_plans() or {}
1776+
training_plans = resp.get("trainingPlanList") or []
1777+
if not training_plans:
1778+
print("ℹ️ No training plans found")
1779+
return
17811780

1782-
if plan_category == "FBT_ADAPTIVE":
1783-
call_and_display(
1784-
api.get_adaptive_training_plan_by_id,
1785-
plan_id,
1786-
method_name="get_adaptive_training_plan_by_id",
1787-
api_call_desc=f"api.get_adaptive_training_plan_by_id({plan_id}) - {plan_name}",
1781+
user_input = input("Enter training plan ID (press Enter for most recent): ").strip()
1782+
selected = None
1783+
if user_input:
1784+
try:
1785+
wanted_id = int(user_input)
1786+
selected = next(
1787+
(
1788+
p
1789+
for p in training_plans
1790+
if int(p.get("trainingPlanId", 0)) == wanted_id
1791+
),
1792+
None,
1793+
)
1794+
if not selected:
1795+
print(
1796+
f"ℹ️ Plan ID {wanted_id} not found in your plans; attempting fetch anyway"
17881797
)
1798+
plan_id = wanted_id
1799+
plan_name = f"Plan {wanted_id}"
1800+
plan_category = None
17891801
else:
1790-
call_and_display(
1791-
api.get_training_plan_by_id,
1792-
plan_id,
1793-
method_name="get_training_plan_by_id",
1794-
api_call_desc=f"api.get_training_plan_by_id({plan_id}) - {plan_name}",
1795-
)
1796-
else:
1797-
print("ℹ️ No training plans found")
1798-
except Exception as e:
1799-
print(f"❌ Error getting plan by ID: {e}")
1802+
plan_id = int(selected["trainingPlanId"])
1803+
plan_name = selected.get("name", str(plan_id))
1804+
plan_category = selected.get("trainingPlanCategory")
1805+
except ValueError:
1806+
print("❌ Invalid plan ID")
1807+
return
1808+
else:
1809+
selected = training_plans[-1]
1810+
plan_id = int(selected["trainingPlanId"])
1811+
plan_name = selected.get("name", str(plan_id))
1812+
plan_category = selected.get("trainingPlanCategory")
1813+
1814+
if plan_category == "FBT_ADAPTIVE":
1815+
call_and_display(
1816+
api.get_adaptive_training_plan_by_id,
1817+
plan_id,
1818+
method_name="get_adaptive_training_plan_by_id",
1819+
api_call_desc=f"api.get_adaptive_training_plan_by_id({plan_id}) - {plan_name}",
1820+
)
1821+
else:
1822+
call_and_display(
1823+
api.get_training_plan_by_id,
1824+
plan_id,
1825+
method_name="get_training_plan_by_id",
1826+
api_call_desc=f"api.get_training_plan_by_id({plan_id}) - {plan_name}",
1827+
)
18001828

18011829

18021830
def get_workout_by_id_data(api: Garmin) -> None:

garminconnect/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def __init__(
266266

267267
self.garmin_graphql_endpoint = "graphql-gateway/graphql"
268268

269-
self.garmin_training_plan_url = "/trainingplan-service/trainingplan"
269+
self.garmin_connect_training_plan_url = "/trainingplan-service/trainingplan"
270270

271271
self.garth = garth.Client(
272272
domain="garmin.cn" if is_cn else "garmin.com",
@@ -2168,7 +2168,7 @@ def logout(self) -> None:
21682168
def get_training_plans(self) -> dict[str, Any]:
21692169
"""Return all available training plans."""
21702170

2171-
url = f"{self.garmin_training_plan_url}/plans"
2171+
url = f"{self.garmin_connect_training_plan_url}/plans"
21722172
logger.debug("Requesting training plans.")
21732173
return self.connectapi(url)
21742174

@@ -2177,15 +2177,15 @@ def get_training_plan_by_id(self, plan_id: int | str) -> dict[str, Any]:
21772177

21782178
plan_id = _validate_positive_integer(int(plan_id), "plan_id")
21792179

2180-
url = f"{self.garmin_training_plan_url}/plans/{plan_id}"
2180+
url = f"{self.garmin_connect_training_plan_url}/plans/{plan_id}"
21812181
logger.debug("Requesting training plan details for %s", plan_id)
21822182
return self.connectapi(url)
21832183

21842184
def get_adaptive_training_plan_by_id(self, plan_id: int | str) -> dict[str, Any]:
21852185
"""Return details for a specific adaptive training plan."""
21862186

21872187
plan_id = _validate_positive_integer(int(plan_id), "plan_id")
2188-
url = f"{self.garmin_training_plan_url}/fbt-adaptive/{plan_id}"
2188+
url = f"{self.garmin_connect_training_plan_url}/fbt-adaptive/{plan_id}"
21892189

21902190
logger.debug("Requesting adaptive training plan details for %s", plan_id)
21912191
return self.connectapi(url)

0 commit comments

Comments
 (0)