octocrate_core/
api_config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
use octocrate_types::ExpirableToken;
use std::sync::Arc;

pub struct APIConfig {
  pub base_url: String,
  pub token: Option<Arc<dyn ExpirableToken>>,
}

pub type SharedAPIConfig = Arc<APIConfig>;

impl APIConfig {
  pub fn with_token<T: ExpirableToken + 'static>(token: T) -> Self {
    APIConfig {
      base_url: "https://api.github.com".to_string(),
      token: Some(Arc::new(token)),
    }
  }

  pub fn with_base_url(base_url: &str) -> Self {
    APIConfig {
      base_url: base_url.to_string(),
      token: None,
    }
  }

  pub fn new<T: ExpirableToken + 'static>(base_url: &str, token: T) -> SharedAPIConfig {
    Arc::new(APIConfig {
      base_url: base_url.to_string(),
      token: Some(Arc::new(token)),
    })
  }

  pub fn shared(self) -> SharedAPIConfig {
    Arc::new(self)
  }
}

impl Default for APIConfig {
  fn default() -> Self {
    APIConfig {
      base_url: "https://api.github.com".to_string(),
      token: None,
    }
  }
}