-
Notifications
You must be signed in to change notification settings - Fork 72
Add Badge service support #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Bishop
wants to merge
11
commits into
matin:main
Choose a base branch
from
Bishop:badges
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4813084
Add Badge service support
Bishop 524b94d
Fix linters
Bishop 5459ff3
Better spec coverage
Bishop 35cdc0b
Remove duplication in specs
Bishop 42f9512
Fix review comments
Bishop 1a8562f
Add docstrings
Bishop 0d3a029
Typo
Bishop 8db01a2
Address review comments
Bishop 6ec7257
Extract duplicated check to method
Bishop a4ba2bb
Add golf category
Bishop b041439
Address review comments
Bishop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| from datetime import datetime | ||
|
|
||
| from pydantic.dataclasses import dataclass | ||
| from typing_extensions import Self | ||
|
|
||
| from .. import http | ||
| from ..utils import camel_to_snake_dict | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Badge: | ||
| """Garmin Connect badges data. | ||
|
|
||
| Retrieve badges by ID or full list. | ||
|
|
||
| Example: | ||
| >>> badge = Badge.get(55, client=authed_client) | ||
| >>> badge.badge_name | ||
| 'Strong Start' | ||
| >>> badge.earned_by_me | ||
| True | ||
| """ | ||
|
|
||
| badge_id: int | ||
| badge_key: str | ||
| badge_name: str | ||
| badge_category_id: int | ||
| badge_difficulty_id: int | ||
| badge_points: int | ||
| badge_type_ids: tuple[int] | ||
| premium: bool | ||
| earned_by_me: bool | ||
| badge_assoc_type_id: int | ||
| badge_assoc_type: str | ||
| user_profile_id: int | None = None | ||
| full_name: str | None = None | ||
| display_name: str | None = None | ||
| badge_is_viewed: bool | None = None | ||
| badge_uuid: str | None = None | ||
| badge_series_id: int | None = None | ||
| badge_start_date: datetime | None = None | ||
| badge_end_date: datetime | None = None | ||
| badge_earned_date: datetime | None = None | ||
| badge_earned_number: int | None = None | ||
| badge_limit_count: int | None = None | ||
| badge_progress_value: int | float | None = None | ||
| badge_target_value: int | float | None = None | ||
| badge_unit_id: int | None = None | ||
| badge_assoc_data_id: str | None = None | ||
| badge_assoc_data_name: str | None = None | ||
| create_date: datetime | None = None | ||
|
|
||
| CATEGORY_ACTIVITIES = 1 | ||
| CATEGORY_RUNNING = 2 | ||
| CATEGORY_CYCLING = 3 | ||
| CATEGORY_CHALLENGES = 4 | ||
| CATEGORY_STEPS = 5 | ||
| CATEGORY_CONNECT_FEATURES = 6 | ||
| CATEGORY_HEALTH = 7 | ||
| CATEGORY_TACX_MULTI_STAGE = 8 | ||
| CATEGORY_DIVING = 9 | ||
| CATEGORY_GOLF = 10 | ||
|
|
||
| TYPE_ONE_TIME = 1 | ||
| TYPE_TRAINING_CLASS = 2 | ||
| TYPE_REPEATABLE = 3 | ||
| TYPE_CUMULATIVE = 4 | ||
| TYPE_LIMITED_ANNUAL = 5 | ||
| TYPE_LIMITED_SINGLE = 6 | ||
| TYPE_SERIES_EVENTS = 7 | ||
|
|
||
| DIFFICULTY_EASY = 1 | ||
| DIFFICULTY_MEDIUM = 2 | ||
| DIFFICULTY_HARD = 3 | ||
| DIFFICULTY_ELITE = 4 | ||
|
|
||
| ASSOC_TYPE_ACTIVITY = 1 | ||
| ASSOC_TYPE_GROUP_CHALLENGE = 2 | ||
| ASSOC_TYPE_ADHOC_CHALLENGE = 3 | ||
| ASSOC_TYPE_DAY = 4 | ||
| ASSOC_TYPE_NO_LINK = 5 | ||
| ASSOC_TYPE_ACTIVITY_DAY = 6 | ||
| ASSOC_TYPE_VIVOFITJR_CHALLENGE = 7 | ||
| ASSOC_TYPE_VIVOFITJR_TEAM_CHALLENGE = 8 | ||
| ASSOC_TYPE_BADGE_CHALLENGE = 9 | ||
| ASSOC_TYPE_EVENT = 10 | ||
| ASSOC_TYPE_SCORECARD = 11 | ||
|
|
||
| UNIT_MI_KM = 1 | ||
| UNIT_FT_M = 2 | ||
| UNIT_ACTIVITIES = 3 | ||
| UNIT_DAYS = 4 | ||
| UNIT_STEPS = 5 | ||
| UNIT_MI = 6 | ||
| UNIT_SECONDS = 7 | ||
| UNIT_CHALLENGES = 8 | ||
| UNIT_KILOCALORIES = 9 | ||
| UNIT_WEEKS = 10 | ||
| UNIT_LIKES = 11 | ||
|
|
||
| @property | ||
| def limited_time(self) -> bool: | ||
| return Badge.TYPE_LIMITED_SINGLE in self.badge_type_ids | ||
|
|
||
| @property | ||
| def annual(self) -> bool: | ||
| return Badge.TYPE_LIMITED_ANNUAL in self.badge_type_ids | ||
|
|
||
| @property | ||
| def repeatable(self) -> bool: | ||
| return Badge.TYPE_REPEATABLE in self.badge_type_ids | ||
|
|
||
| @property | ||
| def cumulative(self) -> bool: | ||
| return Badge.TYPE_CUMULATIVE in self.badge_type_ids | ||
|
|
||
| @property | ||
| def month_challenge(self) -> bool: | ||
| return ( | ||
| self.badge_category_id == Badge.CATEGORY_CHALLENGES | ||
| and self.limited_time | ||
| ) | ||
|
|
||
| @property | ||
| def expedition(self) -> bool: | ||
| return self.badge_assoc_type_id == Badge.ASSOC_TYPE_BADGE_CHALLENGE | ||
|
|
||
| def reload(self, client: http.Client | None = None) -> Self: | ||
| """Get actual data for Badge | ||
| Useful to retrieve actual information for repeatable badges from list response | ||
| """ | ||
| return type(self).get(self.badge_id, client or http.client) | ||
|
|
||
| @classmethod | ||
| def get(cls, badge_id: int, client: http.Client | None = None) -> Self: | ||
| """Get badge by ID. | ||
|
|
||
| Args: | ||
| badge_id: The Garmin badge ID | ||
| client: Optional HTTP client (uses default if not provided) | ||
|
|
||
| Returns: | ||
| Badge instance with full details | ||
| """ | ||
| client = client or http.client | ||
| path = f"/badge-service/badge/detail/v2/{badge_id}" | ||
| data = client.connectapi(path) | ||
| if data is None: | ||
| raise ValueError(f"No data returned from {path}") | ||
| if not isinstance(data, dict): | ||
| raise TypeError( | ||
| f"Expected dict from {path}, got {type(data).__name__}" | ||
| ) | ||
|
|
||
| data = camel_to_snake_dict(data) | ||
| return cls(**data) | ||
|
|
||
| @classmethod | ||
| def list( | ||
| cls, | ||
| client: http.Client | None = None, | ||
| ) -> list[Self]: | ||
| """List of badges, combines earned and available lists. | ||
| Earned and repeatable badges contain data for the first receiving | ||
| For actual progress they should be loaded directly by get or reload methods | ||
|
|
||
| Returns: | ||
| List of Badge instances | ||
| """ | ||
| client = client or http.client | ||
|
|
||
| path = "/badge-service/badge/earned" | ||
| earned = client.connectapi(path) | ||
| cls._require_type(earned, list, path) | ||
|
|
||
| path = "/badge-service/badge/available?showExclusiveBadge=true" | ||
| available = client.connectapi(path) | ||
| cls._require_type(available, list, path) | ||
|
|
||
| data = earned + available | ||
| if not all(isinstance(item, dict) for item in data): | ||
| raise TypeError("Badge list payload contains non-dict entries") | ||
|
|
||
| return [cls(**camel_to_snake_dict(item)) for item in data] | ||
|
|
||
| @staticmethod | ||
| def _require_type(payload: object, expected: type, path: str) -> None: | ||
| if not isinstance(payload, expected): | ||
| raise TypeError( | ||
| f"Expected {expected.__name__} from {path}, got {type(payload).__name__}" | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.