rusty_box/
config.rs

1//! Configuration for the Box API client.
2/// Configuration structure for the Box API.
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub struct Config {
5    base_api_url: String,
6    upload_url: String,
7    pub oauth2_api_url: String,
8    pub oauth2_authorize_url: String,
9    pub api_version: String,
10    pub max_retry_attempts: u8,
11    pub chunk_upload_threads: u8,
12    pub user_agent: String,
13}
14
15//     BASE_API_URL = 'https://api.box.com/2.0'
16//     UPLOAD_URL = 'https://upload.box.com/api/2.0'
17//     OAUTH2_API_URL = 'https://api.box.com/oauth2'
18//     OAUTH2_AUTHORIZE_URL = 'https://account.box.com/api/oauth2/authorize'
19//     MAX_RETRY_ATTEMPTS = 5
20//     CHUNK_UPLOAD_THREADS = 5
21
22impl Default for Config {
23    fn default() -> Self {
24        Config {
25            base_api_url: String::from("https://api.box.com"),
26            upload_url: String::from("https://upload.box.com/api"),
27            oauth2_api_url: String::from("https://api.box.com/oauth2"),
28            oauth2_authorize_url: String::from("https://account.box.com/api/oauth2/authorize"),
29            api_version: String::from("2.0"),
30            max_retry_attempts: 5,
31            chunk_upload_threads: 5,
32            user_agent: "box-rust-sdk/rusty-box".to_owned(),
33        }
34    }
35}
36
37impl Config {
38    pub fn new() -> Self {
39        Config::default()
40    }
41    pub fn base_api_url(&self) -> String {
42        format!("{}/{}", self.base_api_url, self.api_version)
43    }
44    // pub fn set_base_api_url(&mut self, base_api_url: String) {
45    //     self.base_api_url = base_api_url;
46    // }
47    pub fn upload_url(&self) -> String {
48        format!("{}/{}", self.upload_url, self.api_version)
49    }
50    pub fn set_upload_url(&mut self, upload_url: String) {
51        self.upload_url = upload_url;
52    }
53
54    pub fn user_agent(&self) -> String {
55        self.user_agent.clone()
56    }
57    // pub fn oauth2_api_url(&self) -> String {
58    //     self.oauth2_api_url.clone()
59    // }
60    // pub fn oauth2_authorize_url(&self) -> String {
61    //     self.oauth2_authorize_url.clone()
62    // }
63    // pub fn max_retry_attempts(&self) -> u8 {
64    //     self.max_retry_attempts
65    // }
66    // pub fn set_max_retry_attempts(&mut self, max_retry_attempts: u8) {
67    //     self.max_retry_attempts = max_retry_attempts;
68    // }
69    // pub fn chunk_upload_threads(&self) -> u8 {
70    //     self.chunk_upload_threads
71    // }
72    // pub fn set_chunk_upload_threads(&mut self, chunk_upload_threads: u8) {
73    //     self.chunk_upload_threads = chunk_upload_threads;
74    // }
75    // pub fn api_version(&self) -> String {
76    //     self.api_version.clone()
77    // }
78    // pub fn set_api_version(&mut self, api_version: String) {
79    //     self.api_version = api_version;
80    // }
81}
82
83#[cfg(test)]
84mod tests {
85    use super::Config;
86
87    #[test]
88    fn test_default_config_values() {
89        let config = Config::default();
90
91        assert_eq!(config.base_api_url, "https://api.box.com");
92        assert_eq!(config.base_api_url(), "https://api.box.com/2.0");
93
94        assert_eq!(config.upload_url, "https://upload.box.com/api");
95        assert_eq!(config.upload_url(), "https://upload.box.com/api/2.0");
96
97        assert_eq!(config.oauth2_api_url, "https://api.box.com/oauth2");
98        assert_eq!(
99            config.oauth2_authorize_url,
100            "https://account.box.com/api/oauth2/authorize"
101        );
102        assert_eq!(config.max_retry_attempts, 5);
103        assert_eq!(config.chunk_upload_threads, 5);
104        assert_eq!(config.api_version, "2.0");
105        assert_eq!(config.user_agent, "box-rust-sdk/rusty-box".to_owned());
106    }
107
108    #[test]
109    fn test_config_values_v3() {
110        let config = Config {
111            api_version: String::from("3.0"),
112            ..Default::default()
113        };
114        assert_eq!(config.base_api_url(), "https://api.box.com/3.0");
115        assert_eq!(config.upload_url(), "https://upload.box.com/api/3.0");
116    }
117}