mochow_sdk_rust/mochow/config.rs
1/*
2 * Copyright 2024 Baidu, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11 * either express or implied. See the License for the specific language governing permissions
12 * and limitations under the License.
13 */
14use derive_builder::Builder;
15use reqwest::header::{HeaderValue, USER_AGENT};
16
17/// configuration for the client, account, api_key and endpoint are required
18/// ```rust
19/// use mochow_rust_sdk::mochow::config::ClientConfigurationBuilder;
20/// let config = ClientConfigurationBuilder::default()
21/// .account("your account")
22/// .api_key("your api key")
23/// .endpoint("http://127.0.0.1:5287")
24/// .build().unwrap();
25/// ```
26#[derive(Debug, Clone, Builder)]
27pub struct ClientConfiguration {
28 #[builder(setter(into))]
29 pub account: String,
30 #[builder(setter(into))]
31 pub api_key: String,
32 #[builder(setter(into))]
33 pub endpoint: String,
34
35 /// version of the API, default is v1, only support v1 now
36 #[builder(default = r#""v1".into()"#, setter(skip))]
37 pub version: String,
38
39 /// time out in seconds, default is 30 seconds
40 #[builder(default = "30", setter(into))]
41 pub time_out_seconds: u64,
42
43 /// number of retries, default is 3
44 #[builder(default = "3", setter(into))]
45 pub max_retries: u32,
46
47 #[builder(default, setter(into))]
48 pub user_agent: String,
49}
50
51impl ClientConfiguration {
52 pub fn get_request_headers(&self) -> Vec<(reqwest::header::HeaderName, HeaderValue)> {
53 let mut headers = Vec::new();
54 let user_agent = if self.user_agent.is_empty() {
55 "mochow-sdk-rust".to_string()
56 } else {
57 format!("mochow-sdk-rust/{}", self.user_agent)
58 };
59 headers.push((USER_AGENT, HeaderValue::from_str(&user_agent).unwrap()));
60 headers
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::ClientConfigurationBuilder;
67
68 #[test]
69 fn t_cofig() {
70 let conf = ClientConfigurationBuilder::default().build().unwrap();
71 println!("{:?}", conf.get_request_headers())
72 }
73}