Skip to main content

debox_open_sdk/
api_client.rs

1use serde_json::json;
2
3use crate::client::Client;
4
5// result schema { code: 200, data: null, message: 'success', success: true }
6#[derive(Clone, Debug)]
7pub struct ApiResult {
8    pub code: u32,
9    pub data: serde_json::Value,
10    pub message: String,
11    pub success: bool,
12}
13
14// body schema { "url": "http://xxx.com", "http_method": "POST" }
15#[derive(Clone, Debug)]
16pub struct RegisterCallbackUrlBody {
17    pub url: String,
18    pub http_method: String,
19}
20
21// body schema { groupId: '111',toUserId: 'DeBox.Love',message: 'Hello World',}
22pub struct SendChatMsgBody {
23    pub group_id: String,
24    pub to_user_id: String,
25    pub message: String,
26}
27
28impl Client {
29    pub async fn register_callbak_url(
30        &self,
31        body: &RegisterCallbackUrlBody,
32    ) -> Result<ApiResult, reqwest::Error> {
33        let url: &str = "openapi/register_callbak_url";
34        let send_body = json!({
35          "url": body.url.clone(),
36          "http_method": body.http_method.clone(),
37        });
38        let res_value = self.post(&url, &send_body).await?;
39        let res = ApiResult {
40            code: res_value["code"].as_u64().unwrap() as u32,
41            data: res_value["data"].clone(),
42            message: res_value["message"].as_str().unwrap().to_string(),
43            success: res_value["success"].as_bool().unwrap_or_default(),
44        };
45        Ok(res)
46    }
47
48    pub async fn send_chat_msg(&self, body: &SendChatMsgBody) -> Result<ApiResult, reqwest::Error> {
49        let send_body = json!({
50          "group_id": body.group_id.clone(),
51          "to_user_id": body.to_user_id.clone(),
52          "message": body.message.clone(),
53        });
54        let url: &str = "openapi/send_chat_message";
55        let res_value = self.post(&url, &send_body).await?;
56        let res = ApiResult {
57            code: res_value["code"].as_u64().unwrap() as u32,
58            data: res_value["data"].clone(),
59            message: res_value["message"].as_str().unwrap().to_string(),
60            success: res_value["success"].as_bool().unwrap_or_default(),
61        };
62        Ok(res)
63    }
64}