Strava API reference
Strava linking, sync and bulk export import. See Integrations and Importing data.
Routes
Strava routes: account linking, activity and gear sync, and gear CSV import.
import_gear_from_strava_export
import_gear_from_strava_export(request, file, gear_type, token_user_id, db)
Create the gear listed in an uploaded Strava bikes.csv or shoes.csv and report how many.
Source code in backend/app/modules/strava/router.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
strava_begin_state
strava_begin_state(request, token_user_id, oauth_states)
Issue the one-time state that correlates the OAuth callback with the user.
Replaces any state the user issued earlier.
Source code in backend/app/modules/strava/router.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
strava_link
strava_link(request, response, link, token_user_id, oauth_states, sync_states)
Queue the exchange of a Strava authorization code for tokens.
Send the state, code and scope Strava put on the callback. scope is the comma-separated
list of permissions the user granted; a link whose grant lacks read, profile:read_all or
activity:read_all is refused with 400 and urn:endurain:error:provider-scope-insufficient before
the code is exchanged. Poll GET /jobs/{job_id} for the outcome. A second request while one is
pending is answered 409.
Source code in backend/app/modules/strava/router.py
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 | |
strava_retrieve_activities_days
strava_retrieve_activities_days(request, response, start_date, end_date, token_user_id, db, storage)
Queue a Strava 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/strava/router.py
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 | |
strava_retrieve_gear
strava_retrieve_gear(request, response, token_user_id, db, sync_states, rate_limits)
Queue a sync of the user's Strava 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/strava/router.py
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 | |
strava_set_user_client
strava_set_user_client(request, client, token_user_id, verify_step_up, db, response, if_match)
Store the user's Strava API client id and secret, after the user's step-up verification in step_up.
Replacing the client changes which Strava application the account is linked through. It needs a signed-in
session: an API key cannot step up. Send the integrations_version the client read in If-Match.
Source code in backend/app/modules/strava/router.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
strava_unlink
strava_unlink(request, response, body, token_user_id, verify_step_up, db, sync_states, rate_limits)
Queue the disconnection of the user's Strava account after the user's step-up verification; a job runs it.
The job revokes the authorization with Strava and deletes the activities and gear
imported from it. It needs a signed-in session: an API key cannot step up. Poll
GET /jobs/{job_id} for the outcome.
Source code in backend/app/modules/strava/router.py
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 | |
Account linking
Strava account linking: the OAuth state and client, the code exchange and unlinking.
begin_state
begin_state(user_id, oauth_states)
Issue the one-time state that correlates the OAuth callback with the user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The authenticated user. |
required |
oauth_states
|
StravaOAuthStateStore
|
The one-time OAuth state store. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The opaque state, to be sent to Strava and handed back with the authorization code. |
Source code in backend/app/modules/strava/service.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
delete_revoked_data
delete_revoked_data(user_id)
Delete a user's Strava data once the grace period after a revocation has passed.
Runs on a worker. Nothing is asked of Strava: the access is already gone. The user's own Strava export uploads, which carry no Strava activity id, stay, and so do the client id and secret. The revocation mark is cleared, the user told and the deletion audited in the transaction that deletes the gear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose access was revoked. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the data was deleted, False when the user relinked or the grace period has not passed. |
Source code in backend/app/modules/strava/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 | |
linked_user_ids
linked_user_ids(after_id, limit)
Page through the users who have linked Strava, 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/strava/service.py
264 265 266 267 268 269 270 271 272 273 274 275 | |
revoked_user_ids_due
revoked_user_ids_due(after_id, limit)
Page through the users whose Strava data is due for deletion, 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 user ids whose access was revoked longer ago than the grace period, ascending. |
Source code in backend/app/modules/strava/service.py
249 250 251 252 253 254 255 256 257 258 259 260 261 | |
set_client
set_client(user_id, credentials, verify_step_up, db, *, if_match)
Store the user's Strava API client id and secret after the user's step-up, and commit.
The step-up is verified, and committed so a spent backup code stays spent, before the client is touched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The authenticated user. |
required |
credentials
|
StravaClientCredentials
|
The Strava application's client id and secret, and the user's step-up. |
required |
verify_step_up
|
StepUpVerifier
|
Verifies the step-up for the calling user. |
required |
db
|
Session
|
Database session. |
required |
if_match
|
str
|
The |
required |
Returns:
| Type | Description |
|---|---|
int
|
The integrations' new version, which a later write echoes in |
Raises:
| Type | Description |
|---|---|
StepUpFailedError
|
If the step-up does not verify. |
NotFoundError
|
If the user has no integrations. |
PreconditionFailedError
|
If the integrations changed since the client read them. |
Source code in backend/app/modules/strava/service.py
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 | |
unlink_account
unlink_account(user_id, rate_limits)
Disconnect the user's Strava account and delete what was imported from it.
Runs on a worker, never a request. The access token is refreshed first when it has expired or is about to, because Strava refuses to deauthorize with an expired one and the app would stay authorized in the user's Strava settings. Strava is then asked to revoke the authorization, with no database session open. A grant Strava already refused has nothing to revoke, and a refresh or deauthorization that fails otherwise is logged: the account is unlinked locally anyway. A rate-limited refresh is the exception, as it stops the unlink with nothing changed, so the account stays linked and a later attempt can still revoke it. The imported activities are deleted in batches, one transaction each, and the gear and the unlink then commit together last, so an interrupted unlink still finds the account linked and finishes when it runs again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user to unlink. |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True once unlinked, False when Strava was not linked. |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
When the user has no integrations row. |
TooManyRequestsError
|
When Strava's rate limit is active, or another process is refreshing the token, so the unlink is deferred. |
Source code in backend/app/modules/strava/service.py
101 102 103 104 105 106 107 108 109 110 111 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 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
Activity sync
Strava activity sync: fetch a window, parse each activity with its streams and laps, and store it.
ParsedStravaActivity
Bases: TypedDict
A Strava activity converted for storage.
Attributes:
| Name | Type | Description |
|---|---|---|
activity_to_store |
ActivityCore
|
The activity to store. |
stream_data |
list[StravaStreamEntry]
|
Its streams. |
laps |
list[StravaLap] | None
|
Its laps, or |
Source code in backend/app/modules/strava/activity_service.py
111 112 113 114 115 116 117 118 119 120 121 122 | |
StravaLap
Bases: TypedDict
A Strava lap converted to the fields a stored lap has.
Attributes:
| Name | Type | Description |
|---|---|---|
start_time |
str
|
When the lap started, in local time, as |
start_position_lat |
float | None
|
Latitude of the lap's first waypoint. |
start_position_long |
float | None
|
Longitude of the lap's first waypoint. |
end_position_lat |
float | None
|
Latitude of the lap's last waypoint. |
end_position_long |
float | None
|
Longitude of the lap's last waypoint. |
total_elapsed_time |
int | None
|
Elapsed time, in seconds. |
total_timer_time |
int | None
|
Moving time, in seconds. |
total_distance |
float | None
|
Distance, in meters. |
avg_heart_rate |
int | None
|
Average heart rate. |
max_heart_rate |
int | None
|
Maximum heart rate. |
avg_cadence |
int | None
|
Average cadence, from the lap's cadence stream. |
max_cadence |
int | None
|
Maximum cadence, from the lap's cadence stream. |
avg_power |
int | None
|
Average power, from the lap's power stream. |
max_power |
int | None
|
Maximum power, from the lap's power stream. |
total_ascent |
int | None
|
Elevation gain, in meters, from the lap's elevation stream. |
total_descent |
int | None
|
Elevation loss, in meters, from the lap's elevation stream. |
normalized_power |
int | None
|
Normalized power, from the lap's power stream. |
enhanced_avg_pace |
float | None
|
Average pace, in seconds per meter. |
enhanced_avg_speed |
float | None
|
Average speed, in meters per second. |
enhanced_max_pace |
float | None
|
Pace at the maximum speed, in seconds per meter. |
enhanced_max_speed |
float | None
|
Maximum speed, in meters per second. |
Source code in backend/app/modules/strava/activity_service.py
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 | |
fetch_and_process_activities_by_dates
fetch_and_process_activities_by_dates(strava_client, start_date, end_date, user_id, user_integrations, rate_limits)
Fetch a user's Strava activities in a window and store the new ones.
Runs on a worker thread. The Strava round trips happen with no database session open; each activity then opens a short session of its own for the duplicate check, the lookups and the store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strava_client
|
Client
|
A client authenticated as the user. |
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 |
user_integrations
|
UserIntegrationsRead
|
The user's integration settings. |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
list[ActivityRead] | None
|
The activities stored from the window, or |
list[ActivityRead] | None
|
limited. |
Raises:
| Type | Description |
|---|---|
DomainError
|
When Strava rejects the request, or the user or their privacy settings are missing. |
Exception
|
The first failure of an activity, once the rest of the window was processed. |
Source code in backend/app/modules/strava/activity_service.py
125 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 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 | |
fetch_and_process_activity_laps
fetch_and_process_activity_laps(strava_client, strava_activity_id, user_id, stream_data, user_integrations, rate_limits)
Fetch an activity's laps from Strava and derive each lap's metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strava_client
|
Client
|
A client authenticated as the user. |
required |
strava_activity_id
|
int
|
The Strava activity id. |
required |
user_id
|
UUID
|
The user the activity belongs to. |
required |
stream_data
|
list[StravaStreamEntry]
|
|
required |
user_integrations
|
UserIntegrationsRead
|
The user's integration settings. |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
list[StravaLap] | None
|
The converted laps, or |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When Strava's rate limit is active or was just hit. |
DependencyFailedError
|
When Strava cannot return the laps. |
Source code in backend/app/modules/strava/activity_service.py
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 | |
fetch_and_process_activity_streams
fetch_and_process_activity_streams(strava_client, strava_activity_id, user_id, user_integrations, rate_limits)
Fetch an activity's streams from Strava and convert them to waypoint lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strava_client
|
Client
|
A client authenticated as the user. |
required |
strava_activity_id
|
int
|
The Strava activity id. |
required |
user_id
|
UUID
|
The user the activity belongs to. |
required |
user_integrations
|
UserIntegrationsRead
|
The user's integration settings. |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
StravaStreams
|
A flat tuple of each stream's waypoints and whether it is present, ending |
StravaStreams
|
with the pace waypoints. All empty when the activity has no streams. |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When Strava's rate limit is active or was just hit. |
DependencyFailedError
|
When Strava cannot return the streams. |
Source code in backend/app/modules/strava/activity_service.py
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |
get_user_strava_activities_by_dates
get_user_strava_activities_by_dates(user_id, start_date, end_date, rate_limits)
Fetch and store one user's Strava activities recorded in a window.
Refreshes the user's access token first when it is about to expire, so the scheduled per-user refresh also keeps tokens current and no separate pass over every user is needed.
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
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
list[ActivityRead] | None
|
The activities stored from the window, or |
Raises:
| Type | Description |
|---|---|
DomainError
|
When Strava rejects the request or the account is missing. |
Source code in backend/app/modules/strava/activity_service.py
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 | |
parse_activity
parse_activity(activity, user_id, user_privacy_settings, timezone, strava_client, user_integrations, rate_limits)
Fetch one Strava activity in detail and convert it for storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activity
|
SummaryActivity
|
The Strava summary activity. |
required |
user_id
|
UUID
|
The user the activity belongs to. |
required |
user_privacy_settings
|
UserPrivacySettingsRead
|
The user's privacy defaults. |
required |
timezone
|
str
|
The timezone for an activity with no GPS track. |
required |
strava_client
|
Client
|
A client authenticated as the user. |
required |
user_integrations
|
UserIntegrationsRead
|
The user's integration settings. |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
ParsedStravaActivity
|
The activity to store, its stream data and its laps. |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When Strava's rate limit is active or was just hit. |
DependencyFailedError
|
When Strava cannot return the activity, or returns it without an id, start date, sport type or elapsed time. |
Source code in backend/app/modules/strava/activity_service.py
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 254 255 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 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 362 363 364 365 366 367 368 369 370 371 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 414 415 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 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
process_activity
process_activity(activity, user_id, user_privacy_settings, timezone, strava_client, user_integrations, rate_limits)
Parse one Strava activity and store it, unless it is already stored.
The Strava calls inside :func:parse_activity run with no session open. The
duplicate check and the store each use a short session, and the store
publishes activity.created on this worker thread, where its subscribers
run inline without touching the request path. The store is idempotent on the
activity's dedup_key, so two workers racing on one activity store it once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activity
|
SummaryActivity
|
The Strava summary activity. |
required |
user_id
|
UUID
|
The user the activity belongs to. |
required |
user_privacy_settings
|
UserPrivacySettingsRead
|
The user's privacy defaults. |
required |
timezone
|
str
|
The timezone for an activity with no GPS track. |
required |
strava_client
|
Client
|
A client authenticated as the user. |
required |
user_integrations
|
UserIntegrationsRead
|
The user's integration settings. |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
ActivityRead | None
|
The stored activity, or |
Raises:
| Type | Description |
|---|---|
DependencyFailedError
|
When Strava sent the activity without an id. |
Source code in backend/app/modules/strava/activity_service.py
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
save_activity_streams_laps
save_activity_streams_laps(activity, stream_data, laps, db)
Persist a Strava-parsed activity through the canonical ingestion seam.
Strava is a thin adapter: it builds the
format-agnostic :class:~modules.activities.activity.schema.ParsedActivity
from the parse result and delegates persistence + activity.created
publication to
:func:~modules.activities.activity.activity_integration.store_parsed_activity,
instead of calling create_activity and the stream/lap CRUD directly. The
notification, thumbnail, and HR-zone subscribers react to the published event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activity
|
ActivityCore
|
The parsed Strava activity to persist. |
required |
stream_data
|
list[StravaStreamEntry]
|
|
required |
laps
|
list[StravaLap] | None
|
Parsed lap dicts, or |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
ActivityRead
|
The created activity schema. |
Source code in backend/app/modules/strava/activity_service.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | |
Gear sync
Strava gear: syncing the athlete's gear and importing a Strava export's gear CSVs.
get_user_gear
get_user_gear(user_id, rate_limits)
Import the user's Strava gear and assign it to their activities.
Runs on a worker. The Strava round trips happen with no database session open; the new gear, the gear-sync preference and the activity assignments are then written in one short transaction, so a failed sync leaves nothing half done.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose gear to import. |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True once synced, False when Strava is not linked. |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When Strava's rate limit is active or was just hit. |
DependencyFailedError
|
When Strava cannot return the gear, or returns a piece without an id or name. |
Source code in backend/app/modules/strava/gear_service.py
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
import_gear
import_gear(file, user_id, gear_type, db)
Create the bikes or shoes listed in an uploaded Strava gear CSV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
UploadFile
|
The uploaded CSV. |
required |
user_id
|
UUID
|
The user the gear belongs to. |
required |
gear_type
|
GearType
|
Which export file it is: bikes or shoes. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of gear entries handed to the gear module. |
Raises:
| Type | Description |
|---|---|
InvalidInputError
|
When the file cannot be parsed or lacks the required columns. |
Source code in backend/app/modules/strava/gear_service.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | |
match_gear_for_activity
match_gear_for_activity(activity, gears)
Return the local gear ID matching this activity's Strava gear, else None.
Source code in backend/app/modules/strava/gear_service.py
126 127 128 129 130 131 132 133 134 135 136 | |
parse_gear_csv
parse_gear_csv(path, gear_type)
Read a Strava gear CSV and check it has the columns its gear type needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path of the uploaded CSV. |
required |
gear_type
|
GearType
|
Which export file it is: bikes or shoes. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
One dictionary per data row, keyed by column header. |
Raises:
| Type | Description |
|---|---|
InvalidInputError
|
When the file cannot be parsed or lacks the required columns. |
Source code in backend/app/modules/strava/gear_service.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
set_activities_gear
set_activities_gear(user_id, db)
Stage the assignment of the user's Strava-linked activities to the matching local gear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose activities to update. |
required |
db
|
Session
|
Database session; the caller commits. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of activities that were given gear. |
Source code in backend/app/modules/strava/gear_service.py
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 | |
transform_csv_bike_gear_to_schema_gear
transform_csv_bike_gear_to_schema_gear(rows, user_id)
Turn parsed bikes CSV rows into gear to create.
A bike is identified by its name, so the last of several rows with one name wins and a row without a name is skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
list[dict[str, str]]
|
Parsed |
required |
user_id
|
UUID
|
The user the bikes belong to. |
required |
Returns:
| Type | Description |
|---|---|
list[GearCreate]
|
The bikes to create. |
Source code in backend/app/modules/strava/gear_service.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
transform_csv_shoe_gear_to_schema_gear
transform_csv_shoe_gear_to_schema_gear(rows, user_id, db)
Turn parsed shoes CSV rows into gear to create.
Strava allows nameless shoes and Endurain does not, so each one gets the default name plus the next number not taken by the user's gear or an earlier shoe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
list[dict[str, str]]
|
Parsed |
required |
user_id
|
UUID
|
The user the shoes belong to. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
list[GearCreate]
|
The shoes to create. |
Source code in backend/app/modules/strava/gear_service.py
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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
Bulk import
The Strava-export specialisation of the bulk-import source, and its archive layout.
A Strava export is a folder of activity files plus an activities.csv manifest
and a media directory. Three things follow from that, and all three are facts
about Strava rather than about bulk import:
- the manifest supplies names, descriptions and gear the exported files omit,
- it lists a multi-activity
.fitonce per activity inside it, so importing every activity from every listing multiplies them (a 5-activity file becomes 25), - the photos ship next to the files rather than inside them.
Activities knows nothing about Strava: this module registers a
:class:BulkImportFormat that says where the activity files are, where the user
drops them, and how to build the source that answers those three questions.
StravaBulkImportSource
dataclass
Bases: BulkImportSource
A file from a Strava bulk export.
Attributes:
| Name | Type | Description |
|---|---|---|
strava_activities |
StravaActivitiesManifest | None
|
Parsed Strava |
gear_nickname_to_id |
dict[str, list[UUID]] | None
|
Gear nickname -> internal gear id, used to resolve the gear named in the CSV. |
read_member |
ArchiveMemberReader | None
|
Reads a member of the archive by name, to reach the photos. |
Source code in backend/app/modules/strava/bulk_import_service.py
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 414 415 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 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
import_side_artifacts
import_side_artifacts(created_activities, file_base_name, db)
Attach the photos the export ships alongside the activity file.
Even a multi-activity .fit is handled correctly by attaching to the
last created activity — a Strava export's activity directory holds one
imported activity per file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
created_activities
|
list[ActivityRead]
|
Activities persisted from the file, in order. |
required |
file_base_name
|
str
|
The file's original base name (the |
required |
db
|
Session
|
Database session. |
required |
Source code in backend/app/modules/strava/bulk_import_service.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
metadata_for
metadata_for(file_base_name)
Build the supplemental metadata from the matching activities.csv row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_base_name
|
str
|
The original (pre-decompression) base filename, which
is the key into the |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The metadata dict, empty when the import carries no start timestamp. |
Source code in backend/app/modules/strava/bulk_import_service.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
should_import
should_import(activity, activity_metadata, *, activities_in_file)
Whether one activity parsed from a multi-activity file should be imported.
The activities.csv row identifies which activity inside the file this
listing refers to, by start time; the others are duplicates of listings
handled elsewhere in the run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activity
|
ActivityCore
|
The parsed activity under consideration. |
required |
activity_metadata
|
dict[str, Any]
|
Metadata from :meth: |
required |
activities_in_file
|
int
|
How many activities the file yielded. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in backend/app/modules/strava/bulk_import_service.py
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 | |
build_import_dictionary
build_import_dictionary(file_base_name, import_initiated_time, strava_row=None)
Creates the "import_info" dictionary that is added to all activities that are imported from files.
Functions both for Strava imports and generic bulk imports, depending on whether strava_row is given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_base_name
|
str
|
The base filename being processed, named in the log of an invalid Strava activity id. |
required |
import_initiated_time
|
str
|
ISO timestamp of when the bulk import was initiated. |
required |
strava_row
|
dict[str, str] | None
|
The file's |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The import record stored on the activity as its |
Source code in backend/app/modules/strava/bulk_import_service.py
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 | |
build_metadata_dict
build_metadata_dict(file_base_name, strava_activities, import_initiated_time, users_existing_gear_nickname_to_id=None)
Creates a dictionary with metadata information pulled from a Strava activities file.
metadata_found_in_csv tells whether the activities.csv file had an entry for this activity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_base_name
|
str
|
The base filename being processed, also the key into |
required |
strava_activities
|
StravaActivitiesManifest | None
|
The parsed |
required |
import_initiated_time
|
str
|
ISO timestamp of when the bulk import was initiated. |
required |
users_existing_gear_nickname_to_id
|
dict[str, list[UUID]] | None
|
The user's gear ids by nickname, to resolve the gear the CSV names. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The metadata found for the activity, always including its |
Source code in backend/app/modules/strava/bulk_import_service.py
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 | |
create_activity_media_from_strava_bulk_import
create_activity_media_from_strava_bulk_import(activity_id, media_strava_filename, data, db)
Attaches a media file from a Strava export to an activity that was just imported.
The bytes are validated as an image through the unified upload pipeline before they are stored and become servable; a rejected file is logged and skipped so one bad photo does not fail the activity. A database or storage failure is not the photo's, so it propagates to the caller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activity_id
|
UUID
|
The imported activity. |
required |
media_strava_filename
|
str
|
The file's name in the export. |
required |
data
|
bytes
|
The file's bytes. |
required |
db
|
Session
|
Database session. |
required |
Source code in backend/app/modules/strava/bulk_import_service.py
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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
create_gear_dictionary_for_bulk_import
create_gear_dictionary_for_bulk_import(user_id, db)
Creates a dictionary that links gear nicknames in Endurain to their Endurain gear IDs.
The dictionary includes both base Endurain gear nickname and the gear name smoosh ("{brand} {model} {name}") that Strava uses for its shoe gear listing in activities.csv
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The importing user. |
required |
db
|
Session
|
Database session. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[UUID]]
|
Dictionary that uses the gear name as a key to look up the gear ID; empty |
dict[str, list[UUID]]
|
when the user has no gear, which only means no activity is linked to one. |
Source code in backend/app/modules/strava/bulk_import_service.py
123 124 125 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 | |
does_activity_start_time_match_the_data_in_strava_activities_csv
does_activity_start_time_match_the_data_in_strava_activities_csv(activity, activity_metadata_dict)
Strava includes each multi-activity .fit file once for each activity inside the fit file.
Thus, run without filtering, a 5-activity fit file is imported 5 times for 25 activities imported.
So, to find out which activity goes with which file, we check if start time of activity aligns with start time of the activity's strava activities.csv file data. If it does import.
Formatting of times in the two files
Activity start time - from Endurain activity parser: 2023-10-21T07:41:47 Activity start time - from Strava activities.csv: Oct 21, 2023, 8:13:28 AM
Returns: True if the start times match. False if they do not.
Source code in backend/app/modules/strava/bulk_import_service.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
drop_directory_for
drop_directory_for(user_id)
Return the directory one user drops their Strava export in.
Per user, so an import never ingests another user's export and attributes it to whoever started it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The owning user's id. A UUID, so the value can only ever be a single path segment. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Absolute path of that user's Strava-export directory. |
Source code in backend/app/modules/strava/bulk_import_service.py
566 567 568 569 570 571 572 573 574 575 576 577 | |
import_media_from_strava_bulk_export
import_media_from_strava_bulk_export(strava_activities, created_activity, file_base_name, read_member, db)
Import media files associated with a Strava bulk export activity.
Reads the pipe-separated media list from the Strava activities.csv entry
for the given file, then attaches each referenced image found in the export's
media folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strava_activities
|
StravaActivitiesManifest
|
Strava bulk-import metadata dict keyed by filename, then by activities.csv column header. |
required |
created_activity
|
ActivityRead
|
The newly created activity object to which media will be attached. |
required |
file_base_name
|
str
|
Base filename of the activity file, used as the lookup key in strava_activities. |
required |
read_member
|
Callable[[str], bytes | None]
|
Reads a member of the archive being imported by name. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Source code in backend/app/modules/strava/bulk_import_service.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
parse_activities_manifest
parse_activities_manifest(manifest_path)
Parses a Strava activities.csv manifest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest_path
|
str
|
Path of the manifest file. |
required |
Returns:
| Type | Description |
|---|---|
StravaActivitiesManifest | None
|
Dictionary of the manifest's rows keyed by each activity's filename, or |
StravaActivitiesManifest | None
|
|
StravaActivitiesManifest | None
|
without a filename. |
Source code in backend/app/modules/strava/bulk_import_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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
register_bulk_import_format
register_bulk_import_format()
Register the Strava export layout with activity ingestion.
Source code in backend/app/modules/strava/bulk_import_service.py
580 581 582 583 584 585 586 587 588 589 590 | |
Client and token handling
Strava API access: the client, its token refresh and the rate-limit checks.
StravaTokenRejectedError
Bases: DependencyFailedError
Raised when Strava rejected the user's token and the account was unlinked, so a sync must stop.
Source code in backend/app/modules/strava/client_service.py
48 49 | |
call_strava
call_strava(call, strava_client, user_id, user_integrations, rate_limits)
Make one Strava call, confirming a 401 with the token endpoint instead of trusting it.
A 401 can mean the access token expired or that the user withdrew Endurain's access on Strava. So the
stored refresh token is used once, through :func:refresh_user_strava_token, and the call is made again
with the new token. Only Strava refusing the refresh token, or refusing the new access token as well, is a
revocation. A refresh that fails for any other reason leaves the tokens and the data alone and stops the
sync, which tries again on its next run. Failures other than a 401 are the caller's to handle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
call
|
Callable[[Client], T]
|
Makes the Strava call with the client it is given. |
required |
strava_client
|
Client
|
A client authenticated as the user. |
required |
user_id
|
UUID
|
The user the call is for. |
required |
user_integrations
|
UserIntegrationsRead
|
The integration settings |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Returns:
| Type | Description |
|---|---|
T
|
What |
Raises:
| Type | Description |
|---|---|
StravaTokenRejectedError
|
When Strava refused the refresh token or the new access token, so the account was unlinked as revoked; or when the account is no longer linked. |
TooManyRequestsError
|
When Strava's rate limit is active or another process is refreshing the token. |
DependencyFailedError
|
When the refresh failed without Strava refusing the refresh token. |
Source code in backend/app/modules/strava/client_service.py
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |
create_strava_client
create_strava_client(user_integrations)
Build a Strava client from a user's stored tokens.
stravalib's own rate limiting is replaced by a no-op: it would block the thread for minutes or hours when a limit is hit, and the shared rate-limit state decides instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_integrations
|
UserIntegrationsRead
|
The user's integration settings. |
required |
Returns:
| Type | Description |
|---|---|
Client
|
A client authenticated as the user. |
Raises:
| Type | Description |
|---|---|
UndecryptableError
|
When a stored token cannot be decrypted with the configured key. |
Source code in backend/app/modules/strava/client_service.py
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 | |
fetch_user_integrations_and_validate_token
fetch_user_integrations_and_validate_token(user_id, db)
Return a user's integrations when Strava 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 row. |
Source code in backend/app/modules/strava/client_service.py
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 | |
get_client_id
get_client_id(user_integrations)
Get the Strava application (client ID) a user's integration belongs to.
It is the scope of Strava's rate limits, so every call that checks or records one needs it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_integrations
|
UserIntegrationsRead
|
The user's integration settings. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The decrypted client ID. |
Raises:
| Type | Description |
|---|---|
DependencyFailedError
|
When the user has no Strava client ID stored. |
Source code in backend/app/modules/strava/client_service.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
get_client_secret
get_client_secret(user_integrations)
Get the secret of the Strava application a user's integration belongs to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_integrations
|
UserIntegrationsRead
|
The user's integration settings. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The decrypted client secret. |
Raises:
| Type | Description |
|---|---|
DependencyFailedError
|
When the user has no Strava client secret stored. |
Source code in backend/app/modules/strava/client_service.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
handle_rejected_token
handle_rejected_token(user_id, refresh_token, err, *, revoked=False)
Unlink an account whose token Strava rejected during a sync, and tell the user.
The tokens are cleared and the notification row committed together; the live push is
published after. The client id and secret stay, so relinking is one step. Nothing happens
when the stored refresh token is no longer refresh_token: the user relinked, or
another job already unlinked it and told them. When revoked is set the user withdrew Endurain's
access on Strava: the time is stored with the tokens, the data stays for the grace period, and the notice
names the day it is deleted unless the user relinks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose sync was rejected. |
required |
refresh_token
|
str | None
|
The stored (encrypted) refresh token that Strava rejected. |
required |
err
|
Exception
|
The unauthorized error Strava raised. |
required |
revoked
|
bool
|
Whether Strava refused the refresh token, or an access token it had just issued from it. |
False
|
Source code in backend/app/modules/strava/client_service.py
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 248 249 250 251 252 253 254 | |
is_strava_client_error
is_strava_client_error(err)
Check if an exception is Strava refusing a request with a 4xx answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
err
|
Exception
|
The exception to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the error is a response with a 4xx status. |
Source code in backend/app/modules/strava/client_service.py
119 120 121 122 123 124 125 126 127 128 129 | |
is_strava_not_found_error
is_strava_not_found_error(err)
Check if an exception is a Strava 404 Not Found error.
This occurs for activities that have no streams, such as those manually added via Apple Health and imported into Strava.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
err
|
Exception
|
The exception to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the error is a 404 not-found response. |
Source code in backend/app/modules/strava/client_service.py
132 133 134 135 136 137 138 139 140 141 142 143 144 | |
is_strava_rate_limit_error
is_strava_rate_limit_error(err)
Check if an exception is a Strava rate limit error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
err
|
Exception
|
The exception to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the error is a 429 rate limit response. |
Source code in backend/app/modules/strava/client_service.py
107 108 109 110 111 112 113 114 115 116 | |
is_strava_unauthorized_error
is_strava_unauthorized_error(err)
Check if an exception is Strava rejecting the user's access token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
err
|
Exception
|
The exception to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the error is a 401 unauthorized response. |
Source code in backend/app/modules/strava/client_service.py
147 148 149 150 151 152 153 154 155 156 | |
raise_if_rate_limit_active
raise_if_rate_limit_active(client_id, user_id, rate_limits)
Refuse a Strava call while the application's rate limit is active.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
The Strava application (client ID) about to be called. |
required |
user_id
|
UUID
|
The user the call is for, for the log. |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When the application is rate limited. |
Source code in backend/app/modules/strava/client_service.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | |
raise_if_rate_limited
raise_if_rate_limited(err, client_id, rate_limits)
Record a Strava rate limit and refuse the call when err is one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
err
|
Exception
|
The exception a Strava call raised. |
required |
client_id
|
str
|
The Strava application (client ID) that made the call. |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When |
Source code in backend/app/modules/strava/client_service.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
refresh_user_strava_token
refresh_user_strava_token(user_id, rate_limits, *, rejected_refresh_token=None)
Refresh a user's Strava access token when it is about to expire, or when Strava rejected it.
Called at the start of the user's own sync job, so tokens are kept current without a separate pass over every user. Strava rotates the refresh token, so only one process refreshes a user at a time: the claim is taken first, released when the refresh is over, and the integration is read again under it, in case another process just finished. The new tokens are stored only while the refresh token is still the one they were refreshed from. The integration is read in short sessions, the Strava call runs with none open, and the new tokens are written in another. A failure is logged and left to the sync that follows, which will fail visibly if the token really is unusable.
A token that has already expired is never handed to a sync unrefreshed: Strava would answer 401 and the sync would unlink an account that is only waiting for its refresh. Only the token endpoint refusing the refresh token counts as the user revoking access: the account is unlinked as a revocation and the sync stops.
With rejected_refresh_token the refresh is asked for, whatever the token's expiry, because Strava
answered 401 to the access token. Nothing is then left to the sync that follows: when the refresh cannot
be done, or Strava's answer cannot be read as a refusal, an error stops the sync with the tokens kept. A
refresh token that is no longer rejected_refresh_token was replaced by another process, so nothing
is refreshed again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose token to refresh. |
required |
rate_limits
|
StravaRateLimitStore
|
The shared Strava rate-limit state. |
required |
rejected_refresh_token
|
str | None
|
The stored (encrypted) refresh token whose access token Strava rejected, when the refresh is a confirmation of that rejection. |
None
|
Raises:
| Type | Description |
|---|---|
TooManyRequestsError
|
When the token has expired or a refresh was asked for, and another process holds the refresh or the application is rate limited; the work is deferred. |
DependencyFailedError
|
When the token has expired or a refresh was asked for, and Strava could not refresh it. |
StravaTokenRejectedError
|
When Strava refused the refresh token, so the account was unlinked. |
Source code in backend/app/modules/strava/client_service.py
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | |
Rate limits
Strava API shared state: the rate-limit flags and the token-refresh claim.
StravaRateLimitStore
Tracks Strava API rate limit state and token-refresh claims over a platform StateProvider.
The limits are Strava's, per application, so every process and replica has to
see the same answer: a process-local flag would let each worker keep
hammering an API another one was already told to back off from. Every user
brings their own Strava application, so the flags are keyed by client ID: one
application hitting its limit must not pause the others. The flags are TTL keys
in the state provider (in-process under the local profile, Redis when
distributed), so they expire on their own. Strava rotates the refresh token on
every refresh, so two replicas refreshing one user at once would invalidate each
other; a claim lets only one of them refresh. The claim is never released: it expires,
because the state has no owner-checked delete and an early release could free a claim
another replica took after expiry. A refreshed token is no longer due, so nothing waits
on the release, and a failed refresh backs off until the claim expires.
Source code in backend/app/modules/strava/rate_limit_store.py
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 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
__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/strava/rate_limit_store.py
39 40 41 42 43 44 45 46 | |
claim_token_refresh
claim_token_refresh(user_id)
Claim the right to refresh a user's Strava token.
Fails open when the shared state is unreachable: a refresh that races is recoverable by relinking, whereas failing closed would stop every refresh for as long as the state store is down.
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. |
Source code in backend/app/modules/strava/rate_limit_store.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
is_rate_limited
is_rate_limited(client_id)
Check if Strava API calls should be skipped for an application.
Fails open when the shared state is unreachable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
The Strava application (client ID) about to be called. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if currently rate-limited. |
Source code in backend/app/modules/strava/rate_limit_store.py
82 83 84 85 86 87 88 89 90 91 92 93 | |
mark_rate_limited
mark_rate_limited(client_id, is_long_term=False)
Record that a rate limit was hit.
The flag's value is the instant it expires, so a sync that stops here can wait for exactly that long.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
The Strava application (client ID) that hit the limit. |
required |
is_long_term
|
bool
|
True for the daily limit (resets at UTC midnight), False for the 15-minute limit. |
False
|
Returns:
| Type | Description |
|---|---|
datetime
|
When the limit clears. |
Source code in backend/app/modules/strava/rate_limit_store.py
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 | |
rate_limited_until
rate_limited_until(client_id)
Return when an application's active rate limit clears.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
The Strava application (client ID). |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
The later expiry of the active limits, or |
Source code in backend/app/modules/strava/rate_limit_store.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
release_token_refresh
release_token_refresh(user_id)
Release the claim on refreshing a user's Strava token once the refresh is over.
Without it a second refresh, such as one that confirms a 401, would wait for the claim to expire.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
The user whose token was refreshed. |
required |
Source code in backend/app/modules/strava/rate_limit_store.py
132 133 134 135 136 137 138 139 140 141 142 143 | |
Schemas
Strava request and response shapes.
StravaClientCredentials
Bases: BaseModel
A user's Strava API application credentials.
Attributes:
| Name | Type | Description |
|---|---|---|
client_id |
int
|
The Strava application's client id. |
client_secret |
SecretStr
|
The Strava application's client secret. |
step_up |
StepUpVerification
|
The caller's step-up verification, which replacing the client needs. |
Source code in backend/app/modules/strava/schema.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
StravaGearImportResult
Bases: BaseModel
What a Strava gear CSV import created.
Attributes:
| Name | Type | Description |
|---|---|---|
imported |
int
|
How many gear entries the upload produced. |
Source code in backend/app/modules/strava/schema.py
67 68 69 70 71 72 73 74 | |
StravaGearImportType
Bases: StrEnum
The gear types a Strava export file carries; the values are the GearType wire values.
Attributes:
| Name | Type | Description |
|---|---|---|
BIKE |
|
|
SHOES |
|
Source code in backend/app/modules/strava/schema.py
55 56 57 58 59 60 61 62 63 64 | |
StravaLinkRequest
Bases: BaseModel
The OAuth callback values that complete a Strava link.
Attributes:
| Name | Type | Description |
|---|---|---|
state |
str
|
The one-time state stored before the OAuth round-trip. |
code |
str
|
The authorization code Strava returned. |
scope |
str
|
The comma-separated scopes Strava reports as granted. |
Source code in backend/app/modules/strava/schema.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
StravaState
Bases: BaseModel
The one-time state the browser round-trips through Strava.
Attributes:
| Name | Type | Description |
|---|---|---|
state |
str
|
The opaque state, to be sent to Strava and handed back with the authorization code. |
Source code in backend/app/modules/strava/schema.py
45 46 47 48 49 50 51 52 | |
StravaSyncStatus
Bases: JobStatusOpenFailure
Where a queued Strava link, gear sync or unlink is.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['strava_link', 'strava_gear', 'strava_unlink']
|
The job kind: |
Source code in backend/app/modules/strava/schema.py
77 78 79 80 81 82 83 84 | |