Garmin Connect API reference
Garmin Connect linking and sync. See Integrations for how it behaves for users.
Routes
Garmin Connect routes: account linking, MFA, and activity, gear and health sync.
garminconnect_link
garminconnect_link(request, response, garmin_user, token_user_id, mfa_codes, link_states)
Start linking a Garmin Connect account and return at once.
The login runs in the background. Poll GET /jobs/{job_id}: when it reports
mfa_required, submit the code to POST /garmin/mfa and keep polling until it
reports succeeded or failed. A second request while one is running is answered 409 with a
Content-Location naming that job.
Source code in backend/app/modules/garmin/router.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
garminconnect_mfa_code
garminconnect_mfa_code(request, mfa_request, token_user_id, mfa_codes, link_states)
Hand the MFA code to the Garmin Connect login waiting for it.
Source code in backend/app/modules/garmin/router.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
garminconnect_retrieve_activities_days
garminconnect_retrieve_activities_days(request, response, start_date, end_date, token_user_id, db, storage)
Queue a Garmin Connect sync of an explicit date range.
Returns 202 with a job handle; poll GET /jobs/{job_id}
for the outcome. The range is fetched by a background job, never on this
request or an API worker thread.
Source code in backend/app/modules/garmin/router.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
garminconnect_retrieve_gear
garminconnect_retrieve_gear(request, response, token_user_id, db, sync_states, rate_limits)
Queue a sync of the user's Garmin Connect gear; a background job runs it.
Poll GET /jobs/{job_id} for the outcome. A second request while one is
pending is answered 409.
Source code in backend/app/modules/garmin/router.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
garminconnect_retrieve_health_days
garminconnect_retrieve_health_days(request, response, start_date, end_date, token_user_id, db, sync_states, rate_limits)
Queue a sync of the user's Garmin Connect health data for a date range.
The range must not exceed 31 days; a background job runs it. Poll
GET /jobs/{job_id} for the outcome. A second request while one is pending is
answered 409.
Source code in backend/app/modules/garmin/router.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
garminconnect_unlink
garminconnect_unlink(request, body, token_user_id, verify_step_up, db)
Unlink the user's Garmin Connect account after the user's step-up verification.
It needs a signed-in session: an API key cannot step up.
Source code in backend/app/modules/garmin/router.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
Account linking
Linking a Garmin Connect account: the background login, the MFA hand-off and the link job.
Owns the dedicated login threads. Unlinking and the sessions a linked account's syncs use live in
:mod:modules.garmin.service.
link_garminconnect
link_garminconnect(user_id, job_id, email, password, mfa_codes, link_states, is_cn=False)
Log in to Garmin Connect, store the token and record the outcome in the link state.
Blocking; call it through :func:start_link. It never raises: every outcome, including a
failure, ends as the recorded state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user linking Garmin Connect. |
required |
job_id
|
UUID
|
The link job this run records its state against. |
required |
email
|
str
|
Garmin Connect username. |
required |
password
|
str
|
Garmin Connect password. |
required |
mfa_codes
|
GarminMFACodeStore
|
Shared store the submitted MFA code lands in. |
required |
link_states
|
GarminLinkStateStore
|
Shared store of the link attempt's state. |
required |
is_cn
|
bool
|
Whether to authenticate against Garmin's China region. |
False
|
Source code in backend/app/modules/garmin/link_service.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
shutdown_link_pool
shutdown_link_pool(*, wait=False)
Stop accepting link attempts and release the login threads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wait
|
bool
|
Whether to wait for logins still running. |
False
|
Source code in backend/app/modules/garmin/link_service.py
38 39 40 41 42 43 44 | |
start_link
start_link(user_id, login, mfa_codes, link_states)
Start linking a Garmin Connect account on a dedicated worker thread and return at once.
The outcome is recorded in the shared link state. The credentials stay in this process's memory and are never stored, which is why this is not a durable job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user linking Garmin Connect. |
required |
login
|
GarminLogin
|
The Garmin Connect credentials. |
required |
mfa_codes
|
GarminMFACodeStore
|
Shared store the submitted MFA code lands in. |
required |
link_states
|
GarminLinkStateStore
|
Shared store of the link attempt's state. |
required |
Returns:
| Type | Description |
|---|---|
GarminLinkStatus
|
The attempt's initial status. |
Raises:
| Type | Description |
|---|---|
ConflictError
|
When an attempt for this user is already running. |
PoolFullError
|
When every login thread of this process is busy. |
Source code in backend/app/modules/garmin/link_service.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
submit_mfa_code
submit_mfa_code(user_id, mfa_code, mfa_codes, link_states)
Hand the user's MFA code to the login waiting for it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user linking Garmin Connect. |
required |
mfa_code
|
str
|
The code the user entered. |
required |
mfa_codes
|
GarminMFACodeStore
|
Shared store the code lands in. |
required |
link_states
|
GarminLinkStateStore
|
Shared store of the link attempt's state. |
required |
Raises:
| Type | Description |
|---|---|
ConflictError
|
When no login is waiting for a code. |
Source code in backend/app/modules/garmin/link_service.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
wait_for_mfa_code
wait_for_mfa_code(user_id, job_id, mfa_codes, link_states)
Ask for an MFA code through the shared link state and wait for the user to submit it.
The prompt is the mfa_required state the browser polls, so it reaches the user on
whichever replica answers the poll. The code arrives through the shared code store the
same way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user linking Garmin Connect. |
required |
job_id
|
UUID
|
The link job waiting for the code. |
required |
mfa_codes
|
GarminMFACodeStore
|
Shared store the submitted code lands in. |
required |
link_states
|
GarminLinkStateStore
|
Shared store of the link attempt's state. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The submitted code. |
Raises:
| Type | Description |
|---|---|
_MfaCodeTimeoutError
|
When no code arrived within |
Source code in backend/app/modules/garmin/link_service.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
Sync requests
Queue Garmin Connect activity, gear and health syncs so they never run on an API thread.
On the durable profile (JOBS_ENABLED) the request stages a garmin.sync_requested event
and a worker runs it, surviving a restart. Otherwise the same body runs on a small in-process
pool that is not the request threadpool. Either way a gear or health sync is recorded in the
shared sync state, which the client polls and which stops a user queueing the same sync twice.
close_dead_lettered_sync
close_dead_lettered_sync(payload, states)
Close a sync the runner gave up on, so the user can queue another.
Runs however the last attempt ended, including a lease that expired or a worker that died, where
:func:run_sync never got to record it. A sync that already finished, or whose state has expired,
is left as it is, and so is a newer sync that took its claim.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
SyncRequestedPayload
|
What was to be synced. |
required |
states
|
GarminSyncStateStore
|
Shared store of the sync's state. |
required |
Source code in backend/app/modules/garmin/sync_service.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
request_activities_sync
request_activities_sync(user_id, start_date, end_date, storage, db)
Queue a Garmin Connect sync of an explicit date range.
The range is fetched by a background job, never on the request or an API worker thread. It is capped, so a first sync that reaches back years is made in several requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user to sync. |
required |
start_date
|
date
|
First day of the range. |
required |
end_date
|
date
|
Last day of the range. |
required |
storage
|
StorageProvider
|
The platform storage provider. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
ActivityIngestionJobRead
|
The queued job's handle. |
Raises:
| Type | Description |
|---|---|
InvalidInputError
|
When |
Source code in backend/app/modules/garmin/sync_service.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
request_gear_sync
request_gear_sync(user_id, states, rate_limits, db)
Queue a sync of the user's Garmin Connect gear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user to sync. |
required |
states
|
GarminSyncStateStore
|
Shared store of the sync's state. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
GarminSyncStatus
|
The sync's initial status. |
Source code in backend/app/modules/garmin/sync_service.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
request_health_sync
request_health_sync(user_id, start_date, end_date, states, rate_limits, db)
Queue a sync of the user's Garmin Connect health data for a range of days.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user to sync. |
required |
start_date
|
date
|
First day of the range. |
required |
end_date
|
date
|
Last day of the range. |
required |
states
|
GarminSyncStateStore
|
Shared store of the sync's state. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
GarminSyncStatus
|
The sync's initial status. |
Raises:
| Type | Description |
|---|---|
InvalidInputError
|
When |
Source code in backend/app/modules/garmin/sync_service.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
run_sync
run_sync(payload, states, rate_limits, *, will_retry=False)
Run one queued sync and record its outcome. Blocking; call it from a worker, never a request.
A sync that could not run because the account is not linked, Garmin Connect rejected its token or Garmin Connect is rate limiting the account is recorded as failed and not raised: a retry would meet the same answer.
A sync that raised and will be retried by a durable worker stays QUEUED with its claim held:
releasing the claim would let a second sync start beside the retry. A sync nothing will retry,
because there is no worker or this was the last attempt, is FAILED and its claim is released.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
SyncRequestedPayload
|
What to sync. |
required |
states
|
GarminSyncStateStore
|
Shared store of the sync's state. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
will_retry
|
bool
|
Whether a durable worker runs this again if it raises. |
False
|
Raises:
| Type | Description |
|---|---|
Exception
|
Whatever the sync raised, after recording its state, so a durable worker retries it. |
Source code in backend/app/modules/garmin/sync_service.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
shutdown_sync_pool
shutdown_sync_pool(*, wait=False)
Stop accepting syncs and release the pool threads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wait
|
bool
|
Whether to wait for syncs still running. |
False
|
Source code in backend/app/modules/garmin/sync_service.py
44 45 46 47 48 49 50 | |
sync_recent_health
sync_recent_health(states, rate_limits)
Sync the last day of Garmin Connect health stats for every linked user.
Each user's sync is queued, for a worker with durable jobs or for the in-process pool without them, so users are isolated and never run on this thread. Linked users are read a page at a time in short sessions, and one user failing does not stop the rest. Without durable jobs a full pool is waited on; if it stays full the pass stops, and the next run starts over.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
GarminSyncStateStore
|
Shared store of the syncs' state. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
Source code in backend/app/modules/garmin/sync_service.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
Activity sync
Garmin Connect activity sync: fetch the window, download each file and hand it to ingestion.
fetch_and_process_activities_by_dates
fetch_and_process_activities_by_dates(session, start_date, end_date, user_id, rate_limits)
Fetch a user's Garmin Connect activities in a window and store the new ones.
Runs on a worker thread. Garmin round trips run with no session open; the duplicate check and the store each open a short one. An activity failing does not stop the rest of the window, but the first failure is raised once the window is done, so the refresh job fails and is retried, as for any provider. A file ingestion refuses is logged by ingestion and skipped, and does not fail the window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
GarminSession
|
The user's authenticated Garmin Connect session. |
required |
start_date
|
datetime
|
Start of the window. |
required |
end_date
|
datetime
|
End of the window. |
required |
user_id
|
UUID
|
The user the activities belong to. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
list[ActivityRead] | None
|
The stored activities, or |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When Garmin Connect rate limits the window; the refresh is deferred. |
Exception
|
The first failure of the window fetch or of an activity. |
Source code in backend/app/modules/garmin/activity_service.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
get_user_garminconnect_activities_by_dates
get_user_garminconnect_activities_by_dates(user_id, start_date, end_date, rate_limits)
Fetch and store one user's Garmin Connect activities recorded in a window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user to sync. |
required |
start_date
|
datetime
|
Start of the window. |
required |
end_date
|
datetime
|
End of the window. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
list[ActivityRead] | None
|
The stored activities, or |
list[ActivityRead] | None
|
nothing new came back. |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When Garmin Connect rate limits the account. |
Exception
|
Whatever the sync raised, so the refresh job fails and is retried. |
Source code in backend/app/modules/garmin/activity_service.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
Gear sync
Garmin Connect gear sync: store the user's gear and assign it to their activities.
fetch_and_process_gear
fetch_and_process_gear(garminconnect_client, user_id)
Fetch the user's Garmin Connect gear and store the pieces not stored yet.
Garmin is called with no session open; the gear is stored in one short one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
garminconnect_client
|
Garmin
|
A client authenticated as the user. |
required |
user_id
|
UUID
|
The gear's owner. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of gear pieces that were not stored yet. |
Source code in backend/app/modules/garmin/gear_service.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
gear_list
gear_list(answer)
Return the gear Garmin Connect answered with.
garminconnect types its gear answers as a JSON object, but Garmin answers with a list of gear pieces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
answer
|
object
|
What garminconnect returned for a gear request. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
The gear pieces. |
Raises:
| Type | Description |
|---|---|
DependencyFailedError
|
When Garmin Connect answered with something other than a list. |
Source code in backend/app/modules/garmin/gear_service.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
get_user_gear
get_user_gear(user_id, rate_limits)
Fetch a user's Garmin Connect gear, store it and assign it to their activities.
An expired token unlinks the account and notifies the user. Gear sync is switched on only once the gear was fetched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user to sync. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the gear was synced, False when the account is not linked, was unlinked, or |
bool
|
Garmin Connect rejected its token. |
Source code in backend/app/modules/garmin/gear_service.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
match_gear_for_activity
match_gear_for_activity(activity, gears)
Return the local gear ID matching this activity's Garmin gear, else None.
Source code in backend/app/modules/garmin/gear_service.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
process_gear
process_gear(gear, user_id, db)
Build the local gear for a Garmin Connect gear piece.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gear
|
dict[str, Any]
|
The Garmin Connect gear payload. |
required |
user_id
|
UUID
|
The gear's owner. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
GearCreate | None
|
The gear to create, or |
Source code in backend/app/modules/garmin/gear_service.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
resolve_synced_gear_id
resolve_synced_gear_id(user_id, garminconnect_gear, db)
Resolve a downloaded activity's Garmin gear to a local gear ID.
Resolved here, before the file reaches ingestion, so the ingestion seam never has to ask a provider module whether gear sync is on or which local gear a Garmin UUID maps to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
Owner user ID. |
required |
garminconnect_gear
|
list[dict[str, Any]] | None
|
Garmin gear metadata ( |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
UUID | None
|
The local gear ID, or None when gear sync is off, the account's token is |
UUID | None
|
invalid, the activity has no gear, or no local gear matches. |
Source code in backend/app/modules/garmin/gear_service.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
set_activities_gear
set_activities_gear(user_id, db)
Assign local gear to the user's activities that carry a Garmin Connect gear id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The activities' owner. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of activities whose gear was set. |
Source code in backend/app/modules/garmin/gear_service.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
Health sync
Garmin Connect health sync: body composition, daily steps and sleep.
fetch_and_process_bc_by_dates
fetch_and_process_bc_by_dates(garminconnect_client, start_date, end_date, user_id)
Fetch and store body composition from Garmin Connect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
garminconnect_client
|
Garmin
|
Authenticated Garmin Connect client. |
required |
start_date
|
datetime
|
Start date for the retrieval. |
required |
end_date
|
datetime
|
End date for the retrieval. |
required |
user_id
|
UUID
|
The records' owner. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of body composition records stored; a day whose record another source wrote is skipped. |
Source code in backend/app/modules/garmin/health_service.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
fetch_and_process_ds_by_dates
fetch_and_process_ds_by_dates(garminconnect_client, start_date, end_date, user_id)
Fetch and store daily steps from Garmin Connect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
garminconnect_client
|
Garmin
|
Authenticated Garmin Connect client. |
required |
start_date
|
datetime
|
Start date for the retrieval. |
required |
end_date
|
datetime
|
End date for the retrieval. |
required |
user_id
|
UUID
|
The records' owner. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of daily steps records stored; a day whose record another source wrote is skipped. |
Source code in backend/app/modules/garmin/health_service.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
fetch_and_process_sleep_by_dates
fetch_and_process_sleep_by_dates(garminconnect_client, start_date, end_date, user_id)
Fetch and process sleep data from Garmin Connect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
garminconnect_client
|
Garmin
|
Authenticated Garmin Connect client. |
required |
start_date
|
datetime
|
Start date for sleep data retrieval. |
required |
end_date
|
datetime
|
End date for sleep data retrieval. |
required |
user_id
|
UUID
|
ID of the user to process sleep data for. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of sleep records processed. |
Source code in backend/app/modules/garmin/health_service.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
get_user_garminconnect_health_by_dates
get_user_garminconnect_health_by_dates(start_date, end_date, user_id, rate_limits)
Fetch and store one user's Garmin Connect health data for a window.
No session is open while Garmin is being called; each record is stored in its own short one. An expired token unlinks the account and notifies the user. Any other failure propagates, so a durable worker retries the sync.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime
|
Start of the window. |
required |
end_date
|
datetime
|
End of the window. |
required |
user_id
|
UUID
|
The user to sync. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the data was synced, False when the account is not linked or Garmin Connect |
bool
|
rejected its token. |
Source code in backend/app/modules/garmin/health_service.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
sync_health_for_dates
sync_health_for_dates(start_date, end_date, user_id, rate_limits)
Fetch and store one user's Garmin Connect health data for whole days.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
date
|
First day to fetch. |
required |
end_date
|
date
|
Last day to fetch, inclusive. |
required |
user_id
|
UUID
|
The user to sync. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the data was synced, False when the account is not linked or was rejected. |
Source code in backend/app/modules/garmin/health_service.py
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
Session and account
Unlinking a Garmin Connect account and the authenticated sessions its syncs use.
Linking lives in :mod:modules.garmin.link_service. The activity, gear and health syncs live in
:mod:modules.garmin.activity_service, :mod:modules.garmin.gear_service and
:mod:modules.garmin.health_service.
GarminSession
Bases: NamedTuple
A linked user's authenticated client and the stored token it was restored from.
Attributes:
| Name | Type | Description |
|---|---|---|
client |
Garmin
|
The authenticated Garmin Connect client. |
token |
dict[str, Any]
|
The token as stored when the client was restored, so a rejection can be told apart from a later re-link. |
Source code in backend/app/modules/garmin/service.py
99 100 101 102 103 104 105 106 107 108 109 | |
fresh_session
fresh_session(user_id, session, claims)
Return a session whose token is not about to expire, refreshing it under the user's shared claim.
The client would otherwise refresh the token itself in the middle of a call, and two syncs of one user doing so at once can invalidate each other's refresh token. Only the holder of the claim refreshes; a caller that finds it held waits for the holder, then restores the token it stored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user the session belongs to. |
required |
session
|
GarminSession
|
The session as restored from the stored token. |
required |
claims
|
GarminRateLimitStore
|
The shared Garmin Connect state holding the refresh claim. |
required |
Returns:
| Type | Description |
|---|---|
GarminSession | None
|
A session that needs no refresh, or |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When another process still holds the refresh claim after the wait. |
Source code in backend/app/modules/garmin/service.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
get_linked_user_integrations
get_linked_user_integrations(user_id, db)
Return a user's integrations when Garmin Connect is linked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user to look up. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
UserIntegrationsRead | None
|
The user's integrations, or |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
When the user has no integrations record. |
Source code in backend/app/modules/garmin/service.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
get_user_garminconnect_session
get_user_garminconnect_session(user_id, db)
Restore the session of a linked user from the token stored for them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose Garmin Connect account to use. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
GarminSession | None
|
The session, or |
Raises:
| Type | Description |
|---|---|
UndecryptableError
|
When the stored token cannot be decrypted with the configured key. The account stays linked: the fault is the key, not the token. |
Source code in backend/app/modules/garmin/service.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
handle_rejected_token
handle_rejected_token(user_id, session, err)
Unlink an account whose token Garmin Connect rejected during a sync, and tell the user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose sync was rejected. |
required |
session
|
GarminSession
|
The session the sync used. |
required |
err
|
Exception
|
The authentication error Garmin Connect raised. |
required |
Source code in backend/app/modules/garmin/service.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | |
linked_session
linked_session(user_id, rate_limits)
Yield a linked user's session with no database session open, then store its refreshed token.
The token is stored whether the body succeeds or raises, so the next sync restores a live one. Rejection by Garmin Connect is the body's to handle, as what follows it differs per sync.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose Garmin Connect account to use. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state, checked before the session is restored. |
required |
Yields:
| Type | Description |
|---|---|
Generator[GarminSession | None]
|
The session, or |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When the account is rate limited, or another process keeps refreshing the token. |
Source code in backend/app/modules/garmin/service.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
linked_user_ids
linked_user_ids(after_id, limit)
Page through the users who have linked Garmin Connect, in a short session of its own.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
after_id
|
UUID | None
|
Return only users with an id greater than this. |
required |
limit
|
int
|
Maximum number of user ids to return. |
required |
Returns:
| Type | Description |
|---|---|
list[UUID]
|
The linked user ids, ascending. |
Source code in backend/app/modules/garmin/service.py
315 316 317 318 319 320 321 322 323 324 325 326 | |
raise_if_rate_limited
raise_if_rate_limited(user_id, rate_limits)
Refuse a Garmin Connect call while the user's account is rate limited.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user about to be synced. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When the account is rate limited, carrying when the limit clears. |
Source code in backend/app/modules/garmin/service.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
rate_limit_error
rate_limit_error(err, user_id, rate_limits)
Record that Garmin Connect refused a call as rate limited, and build the error for it.
Garmin Connect reports no reset time, so the account is left alone for one cooldown, in every process, and the refresh waits that long before it tries again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
err
|
Exception
|
The rate-limit error Garmin Connect raised. |
required |
user_id
|
UUID
|
The user whose account was limited. |
required |
rate_limits
|
GarminRateLimitStore
|
The shared Garmin Connect rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
TooManyRequestsError
|
The error to raise, carrying when the refresh may run again. |
Source code in backend/app/modules/garmin/service.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
save_refreshed_token
save_refreshed_token(user_id, session)
Store the token a sync's client refreshed, so the next sync restores a live one.
Garmin Connect may rotate the refresh token, in which case the stored one stops working. A failure is logged, not raised: the sync itself has already finished.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user the session belongs to. |
required |
session
|
GarminSession
|
The session the sync used. |
required |
Source code in backend/app/modules/garmin/service.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
unlink_account
unlink_account(user_id, step_up, verify_step_up, db)
Unlink a user's Garmin Connect account after the user's step-up.
The step-up is verified before anything is changed, and committed with the unlink.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user unlinking Garmin Connect. |
required |
step_up
|
StepUpVerification
|
The user's step-up verification. |
required |
verify_step_up
|
StepUpVerifier
|
Verifies the step-up for the calling user. |
required |
db
|
Session
|
Database session. |
required |
Raises:
| Type | Description |
|---|---|
StepUpFailedError
|
If the step-up does not verify. |
Source code in backend/app/modules/garmin/service.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
unlink_expired_account
unlink_expired_account(user_id, token, db)
Unlink an account whose Garmin Connect token expired and tell the user.
The unlink and the notification row commit together; the live push is published after.
Nothing happens when the stored token is no longer token: the user re-linked, or
another job already unlinked it and told them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose account to unlink. |
required |
token
|
dict[str, Any]
|
The stored token that Garmin Connect rejected. |
required |
db
|
Session
|
The caller's session, which this commits. |
required |
Source code in backend/app/modules/garmin/service.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
Rate limits
Garmin Connect's shared rate-limit state: a per-user flag over the platform StateProvider.
Garmin Connect throttles an account, not an application, and reports no reset time, so a limit is
recorded for one cooldown. Every process and replica reads the same flag, so a sync that was refused
is not repeated by another replica's scheduled pull. The same state holds the claim on refreshing a
user's token. See :class:core.provider_rate_limit.ProviderRateLimitStore.
GarminRateLimitStore
Garmin Connect rate-limit flags and token-refresh claims, keyed by user.
Attributes:
| Name | Type | Description |
|---|---|---|
_limits |
The shared flag storage. |
|
_state |
The platform state provider, which holds the refresh claims. |
Source code in backend/app/modules/garmin/rate_limit_store.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
__init__
__init__(state)
Bind the store to a state provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
StateProvider
|
The platform state provider. |
required |
Source code in backend/app/modules/garmin/rate_limit_store.py
27 28 29 30 31 32 33 34 | |
claim_token_refresh
claim_token_refresh(user_id)
Claim the right to refresh a user's Garmin Connect token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose token is about to be refreshed. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when this caller may refresh, False when another already is. |
Raises:
| Type | Description |
|---|---|
StateBackendUnavailableError
|
When the shared state is unreachable; the caller decides whether to refresh anyway. |
Source code in backend/app/modules/garmin/rate_limit_store.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
mark_rate_limited
mark_rate_limited(user_id)
Record that Garmin Connect rate limited a user's account, for one cooldown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose account was limited. |
required |
Returns:
| Type | Description |
|---|---|
datetime
|
When the limit is taken to clear. |
Source code in backend/app/modules/garmin/rate_limit_store.py
36 37 38 39 40 41 42 43 44 45 46 47 | |
rate_limited_until
rate_limited_until(user_id)
Return when a user's active rate limit clears.
Fails open when the shared state is unreachable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user about to be synced. |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
When the limit clears, or |
Source code in backend/app/modules/garmin/rate_limit_store.py
49 50 51 52 53 54 55 56 57 58 59 60 | |
release_token_refresh
release_token_refresh(user_id)
Release the claim on refreshing a user's token once the refresh is over.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose token was refreshed. |
required |
Raises:
| Type | Description |
|---|---|
StateBackendUnavailableError
|
When the shared state is unreachable; the claim then expires on its own. |
Source code in backend/app/modules/garmin/rate_limit_store.py
79 80 81 82 83 84 85 86 87 88 | |
Schemas
Garmin Connect request and response shapes.
GarminLinkStatus
Bases: JobStatus
Where a Garmin Connect link attempt is.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['garmin_link']
|
Always |
mfa_required |
bool
|
|
error_code |
LinkFailure | None
|
Why the attempt failed; set only when |
Source code in backend/app/modules/garmin/schema.py
37 38 39 40 41 42 43 44 45 46 47 48 49 | |
GarminLogin
Bases: BaseModel
Credentials a user submits to link Garmin Connect.
Source code in backend/app/modules/garmin/schema.py
11 12 13 14 15 16 17 18 | |
GarminSyncStatus
Bases: JobStatusOpenFailure
Where a queued Garmin Connect gear or health sync is.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['garmin_sync_gear', 'garmin_sync_health']
|
What the sync pulls, as its job kind. |
Source code in backend/app/modules/garmin/schema.py
52 53 54 55 56 57 58 59 | |
MFARequest
Bases: BaseModel
The MFA code a user submits to finish a link attempt.
Source code in backend/app/modules/garmin/schema.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |