jx3_api/
lib.rs

1pub mod api;
2pub mod enums;
3pub mod err;
4pub mod model;
5
6use reqwest::Client;
7use reqwest::header::{HeaderMap, HeaderValue};
8
9#[derive(Debug, Clone)]
10pub struct Jx3ApiClient {
11    client: Client,
12    base_url: String,
13    pub token: Option<String>,
14}
15
16impl Jx3ApiClient {
17    pub fn new() -> Self {
18        let client = Client::builder().build().expect("客户端初始化失败!");
19        Self {
20            client,
21            token: None,
22            base_url: String::from(api::DEFAULT_BASE_URL),
23        }
24    }
25
26    pub fn from_token(token: &str) -> Self {
27        let mut header_map = HeaderMap::new();
28        let token_value = HeaderValue::from_str(token).expect("设置token失败!");
29        header_map.insert(api::HEADER_TOKEN_KEY, token_value);
30        let client = Client::builder()
31            .default_headers(header_map)
32            .build()
33            .expect("客户端初始化失败!");
34        Self {
35            client,
36            token: Some(token.to_string()),
37            base_url: String::from(api::DEFAULT_BASE_URL),
38        }
39    }
40
41    pub fn set_base_url(&mut self, base_url: &str) {
42        self.base_url = base_url.to_string();
43    }
44}