1use crate::endpoints::{
2 ActivitiesEndpoint, AthleteEndpoint, AthletesEndpoint, ClubsEndpoint, GearEndpoint,
3 RoutesEndpoint, SegmentsEndpoint, StreamsEndpoint, UploadsEndpoint,
4};
5use crate::query::{last_rate_limit, RateLimit};
6use std::sync::{Arc, RwLock};
7
8#[derive(Clone)]
15pub struct StravaAPI {
16 url: String,
17 token: Arc<RwLock<String>>,
18}
19
20impl StravaAPI {
21 pub fn new(url: &str, token: impl Into<String>) -> Self {
22 Self {
23 url: url.into(),
24 token: Arc::new(RwLock::new(token.into())),
25 }
26 }
27
28 pub fn token(&self) -> String {
30 self.token.read().expect("token lock poisoned").clone()
31 }
32
33 pub fn set_token(&self, token: impl Into<String>) {
37 *self.token.write().expect("token lock poisoned") = token.into();
38 }
39
40 pub fn rate_limit(&self) -> Option<RateLimit> {
47 last_rate_limit()
48 }
49
50 pub fn activities(&self) -> ActivitiesEndpoint {
51 ActivitiesEndpoint::new(&self.url, self.token())
52 }
53
54 pub fn athlete(&self) -> AthleteEndpoint {
55 AthleteEndpoint::new(&self.url, self.token())
56 }
57
58 pub fn athletes(&self) -> AthletesEndpoint {
59 AthletesEndpoint::new(&self.url, self.token())
60 }
61
62 pub fn clubs(&self) -> ClubsEndpoint {
63 ClubsEndpoint::new(&self.url, self.token())
64 }
65
66 pub fn gear(&self) -> GearEndpoint {
67 GearEndpoint::new(&self.url, self.token())
68 }
69
70 pub fn routes(&self) -> RoutesEndpoint {
71 RoutesEndpoint::new(&self.url, self.token())
72 }
73
74 pub fn segments(&self) -> SegmentsEndpoint {
75 SegmentsEndpoint::new(&self.url, self.token())
76 }
77
78 pub fn streams(&self) -> StreamsEndpoint {
79 StreamsEndpoint::new(&self.url, self.token())
80 }
81
82 pub fn uploads(&self) -> UploadsEndpoint {
83 UploadsEndpoint::new(&self.url, self.token())
84 }
85}