debox_open_sdk/
client.rs

1pub struct ClientOptions {
2    pub endpoint: String,
3    pub api_key: String,
4    pub user_agent: Option<String>,
5    pub request_time_out: Option<u32>,
6    // retry_times: Option<u32>,
7    pub auth_version: Option<String>,
8}
9
10pub struct Client {
11    client: reqwest::Client,
12    endpoint: String,
13    api_key: String,
14    user_agent: String,
15    // retry_times: u32, // TODO
16    auth_version: String,
17}
18
19impl Client {
20    pub fn new(opt: &ClientOptions) -> Self {
21        let client = reqwest::Client::builder()
22            .timeout(std::time::Duration::from_secs(
23                opt.request_time_out.unwrap_or(30).into(),
24            ))
25            .build()
26            .unwrap();
27        let user_agent = opt
28            .user_agent
29            .clone()
30            .unwrap_or("@deboxdao/debox-open-sdk".to_string());
31        let auth_version = opt.auth_version.clone().unwrap_or("0.6.0".to_string());
32        // let retry_times = opt.retry_times.unwrap_or(3);
33        Client {
34            client,
35            endpoint: opt.endpoint.clone(),
36            api_key: opt.api_key.clone(),
37            user_agent,
38            // retry_times,
39            auth_version,
40        }
41    }
42
43    pub async fn post(
44        &self,
45        path: &str,
46        body: &serde_json::Value,
47    ) -> Result<serde_json::Value, reqwest::Error> {
48        let echo_json: serde_json::Value = self
49            .client
50            .post(format!("{}/{}", self.endpoint, path))
51            .header(
52                "X-Chat-Bodyrawsize",
53                serde_json::to_string(body).unwrap().len().to_string(),
54            )
55            .header("X-Api-Key", self.api_key.clone())
56            .header("X-Chat-Apiversion", self.auth_version.clone())
57            .header("User-Agent", self.user_agent.clone())
58            .header("Content-Type", "application/json")
59            .header("Accept-Encoding", "deflate")
60            .json(body)
61            .send()
62            .await?
63            .json()
64            .await?;
65        Ok(echo_json)
66    }
67}