use crate::endpoints::{
ActivitiesEndpoint, AthleteEndpoint, AthletesEndpoint, ClubsEndpoint, GearEndpoint,
RoutesEndpoint, SegmentsEndpoint, StreamsEndpoint, UploadsEndpoint,
};
use crate::query::{last_rate_limit, RateLimit};
use std::sync::{Arc, RwLock};
#[derive(Clone)]
pub struct StravaAPI {
url: String,
token: Arc<RwLock<String>>,
}
impl StravaAPI {
pub fn new(url: &str, token: impl Into<String>) -> Self {
Self {
url: url.into(),
token: Arc::new(RwLock::new(token.into())),
}
}
pub fn token(&self) -> String {
self.token.read().expect("token lock poisoned").clone()
}
pub fn set_token(&self, token: impl Into<String>) {
*self.token.write().expect("token lock poisoned") = token.into();
}
pub fn rate_limit(&self) -> Option<RateLimit> {
last_rate_limit()
}
pub fn activities(&self) -> ActivitiesEndpoint {
ActivitiesEndpoint::new(&self.url, self.token())
}
pub fn athlete(&self) -> AthleteEndpoint {
AthleteEndpoint::new(&self.url, self.token())
}
pub fn athletes(&self) -> AthletesEndpoint {
AthletesEndpoint::new(&self.url, self.token())
}
pub fn clubs(&self) -> ClubsEndpoint {
ClubsEndpoint::new(&self.url, self.token())
}
pub fn gear(&self) -> GearEndpoint {
GearEndpoint::new(&self.url, self.token())
}
pub fn routes(&self) -> RoutesEndpoint {
RoutesEndpoint::new(&self.url, self.token())
}
pub fn segments(&self) -> SegmentsEndpoint {
SegmentsEndpoint::new(&self.url, self.token())
}
pub fn streams(&self) -> StreamsEndpoint {
StreamsEndpoint::new(&self.url, self.token())
}
pub fn uploads(&self) -> UploadsEndpoint {
UploadsEndpoint::new(&self.url, self.token())
}
}