octocrate_core/
api_config.rs

1use octocrate_types::ExpirableToken;
2use std::sync::Arc;
3
4pub struct APIConfig {
5  pub base_url: String,
6  pub token: Option<Arc<dyn ExpirableToken>>,
7}
8
9pub type SharedAPIConfig = Arc<APIConfig>;
10
11impl APIConfig {
12  pub fn with_token<T: ExpirableToken + 'static>(token: T) -> Self {
13    APIConfig {
14      base_url: "https://api.github.com".to_string(),
15      token: Some(Arc::new(token)),
16    }
17  }
18
19  pub fn with_base_url(base_url: &str) -> Self {
20    APIConfig {
21      base_url: base_url.to_string(),
22      token: None,
23    }
24  }
25
26  pub fn new<T: ExpirableToken + 'static>(base_url: &str, token: T) -> SharedAPIConfig {
27    Arc::new(APIConfig {
28      base_url: base_url.to_string(),
29      token: Some(Arc::new(token)),
30    })
31  }
32
33  pub fn shared(self) -> SharedAPIConfig {
34    Arc::new(self)
35  }
36}
37
38impl Default for APIConfig {
39  fn default() -> Self {
40    APIConfig {
41      base_url: "https://api.github.com".to_string(),
42      token: None,
43    }
44  }
45}