rtxmail 0.1.3

腾讯企业邮箱rust版本sdk
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
pub use crate::{dto::*, models::*};
use crate::{
    errs::{new_api_error, Result},
    utils::http::{do_http, PostParameters},
};
use async_trait::async_trait;
use reqwest::Method;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::{json, Value};

const BASE_URL: &str = "https://api.exmail.qq.com";

#[derive(Debug)]
pub struct Client {
    pub(crate) corp_id: String,
    pub(crate) corp_secret: String,
}

#[derive(Debug, Deserialize, Serialize)]
struct Token {
    access_token: String,
    expires_in: u32,
}

impl Client {
    pub fn new(corp_id: String, corp_secret: String) -> Client {
        Client {
            corp_id,
            corp_secret,
        }
    }

    async fn access_token(&self) -> Result<String> {
        let query_body = json!({
            "corpid": self.corp_id,
            "corpsecret": self.corp_secret,
        });

        let resp = do_http(
            Method::GET,
            &format!("{}/cgi-bin/gettoken", BASE_URL),
            None,
            Some(query_body),
            None,
        )
        .await?;

        let data = resp.json::<Token>().await?;

        Ok(data.access_token)
    }

    // http 请求
    async fn request<R: Responser + DeserializeOwned>(
        &self,
        method: Method,
        url: &str,
        body: Option<Value>,
    ) -> Result<R> {
        let body = if let Some(data) = body {
            Some(PostParameters::json(data))
        } else {
            None
        };
        let resp = do_http(method, url, None, None, body)
            .await?
            .json::<R>()
            // .text()
            .await?;

        if resp.error_code() != 0 {
            return Err(new_api_error(resp.error_code(), resp.error_message()));
        }

        // println!("{resp}");

        Ok(resp)
        // Ok(Response::default())
    }
}

#[async_trait]
pub trait Exmailer {
    /// 创建部门,成功返回创建后的部门ID
    async fn create_department(&self, params: ParamsCreateDepartment) -> Result<u64>;
    /// 更新部门
    async fn update_department(&self, params: ParamsUpdateDepartment) -> Result<()>;
    /// 删除部门
    async fn delete_department(&self, id: u64) -> Result<()>;
    /// 获取部门列表, ID为1时获取根部门下的子部门
    async fn list_department(&self, id: Option<u64>) -> Result<Vec<Department>>;
    /// 查找部门
    async fn search_department(&self, params: ParamsSerchDepartment) -> Result<Vec<Department>>;
    /// 创建用户
    async fn create_user(&self, params: ParamsCreateUser) -> Result<()>;
    /// 更新用户
    async fn update_user(&self, params: ParamsUpdateUser) -> Result<()>;
    /// 删除用户
    async fn delete_user(&self, user_id: &str) -> Result<()>;
    /// 获取用户
    async fn get_user(&self, user_id: &str) -> Result<User>;
    /// 获取部门用户
    async fn get_department_user(
        &self,
        department_id: u64,
        fetch_child: Option<bool>,
    ) -> Result<Vec<User>>;
    /// 批量检查账户
    async fn batchcheck_user(&self, userids: &[&str]) -> Result<Vec<UserCheck>>;
    /// 创建群组
    async fn create_group(&self, params: ParamsCreateGroup) -> Result<()>;
    /// 更新群组
    async fn update_group(&self, params: ParamsUpdateGroup) -> Result<()>;
    /// 删除群组
    async fn delete_group(&self, group_id: &str) -> Result<()>;
    /// 获取群组信息
    async fn get_group(&self, group_id: &str) -> Result<Group>;
}

trait Responser {
    fn error_code(&self) -> u64;
    fn error_message(&self) -> String;
}

#[derive(Debug, Deserialize, Serialize, Default)]
struct Response {
    #[serde(rename = "errcode")]
    error_code: u64,
    #[serde(rename = "errmsg")]
    error_message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    department: Option<Vec<Department>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<u64>,
    #[serde(rename = "userlist", skip_serializing_if = "Option::is_none")]
    user_list: Option<Vec<User>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    list: Option<Vec<UserCheck>>,
}

impl Responser for Response {
    fn error_code(&self) -> u64 {
        self.error_code
    }
    fn error_message(&self) -> String {
        self.error_message.to_owned()
    }
}

#[derive(Debug, Deserialize, Serialize, Default)]
struct GetResponse<T> {
    #[serde(rename = "errcode")]
    error_code: u64,
    #[serde(rename = "errmsg")]
    error_message: String,
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    data: Option<T>,
}

impl<T> Responser for GetResponse<T> {
    fn error_code(&self) -> u64 {
        self.error_code
    }
    fn error_message(&self) -> String {
        self.error_message.to_owned()
    }
}

#[async_trait]
impl Exmailer for Client {
    /// 参考接口说明:https://service.rtxmail.net/api/267.html
    async fn create_department(&self, params: ParamsCreateDepartment) -> Result<u64> {
        let token = self.access_token().await?;
        let resp = self
            .request::<Response>(
                Method::POST,
                &format!("{BASE_URL}/cgi-bin/department/create?access_token={token}"),
                Some(serde_json::to_value(params)?),
            )
            .await?;

        Ok(resp.id.unwrap())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/268.html
    async fn update_department(&self, params: ParamsUpdateDepartment) -> Result<()> {
        let token = self.access_token().await?;
        self.request::<Response>(
            Method::POST,
            &format!("{BASE_URL}/cgi-bin/department/update?access_token={token}"),
            Some(serde_json::to_value(params)?),
        )
        .await?;

        Ok(())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/269.html
    async fn delete_department(&self, id: u64) -> Result<()> {
        let token = self.access_token().await?;
        self.request::<Response>(
            Method::GET,
            &format!("{BASE_URL}/cgi-bin/department/delete?access_token={token}&id={id}"),
            None,
        )
        .await?;

        Ok(())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/270.html
    async fn list_department(&self, id: Option<u64>) -> Result<Vec<Department>> {
        let token = self.access_token().await?;
        let id = id.unwrap_or(1);

        let resp: Response = self
            .request(
                Method::GET,
                &format!("{BASE_URL}/cgi-bin/department/list?access_token={token}&id={id}"),
                None,
            )
            .await?;

        Ok(resp.department.unwrap())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/271.html
    async fn search_department(&self, params: ParamsSerchDepartment) -> Result<Vec<Department>> {
        let token = self.access_token().await?;
        let resp: Response = self
            .request(
                Method::POST,
                &format!("{BASE_URL}/cgi-bin/department/search?access_token={token}"),
                Some(serde_json::to_value(&params)?),
            )
            .await?;

        Ok(resp.department.unwrap())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/272.html
    async fn create_user(&self, params: ParamsCreateUser) -> Result<()> {
        let token = self.access_token().await?;
        self.request::<Response>(
            Method::POST,
            &format!("{BASE_URL}/cgi-bin/user/create?access_token={token}"),
            Some(serde_json::to_value(params)?),
        )
        .await?;

        Ok(())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/273.html
    async fn update_user(&self, params: ParamsUpdateUser) -> Result<()> {
        let token = self.access_token().await?;
        self.request::<Response>(
            Method::POST,
            &format!("{BASE_URL}/cgi-bin/user/update?access_token={token}"),
            Some(serde_json::to_value(params)?),
        )
        .await?;

        Ok(())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/274.html
    async fn delete_user(&self, user_id: &str) -> Result<()> {
        let token = self.access_token().await?;
        self.request::<Response>(
            Method::GET,
            &format!("{BASE_URL}/cgi-bin/user/delete?access_token={token}&userid={user_id}"),
            None,
        )
        .await?;

        Ok(())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/275.html
    async fn get_user(&self, user_id: &str) -> Result<User> {
        let token = self.access_token().await?;
        let resp = self
            .request::<GetResponse<User>>(
                Method::GET,
                &format!("{BASE_URL}/cgi-bin/user/get?access_token={token}&userid={user_id}"),
                None,
            )
            .await?;

        Ok(resp.data.unwrap())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/277.html
    async fn get_department_user(
        &self,
        department_id: u64,
        fetch_child: Option<bool>,
    ) -> Result<Vec<User>> {
        let token = self.access_token().await?;
        let fetch_child = fetch_child
            .map(|x| if x { 1 } else { 0 })
            .unwrap_or_default();
        let resp = self
            .request::<Response>(
                Method::GET,
                &format!("{BASE_URL}/cgi-bin/user/list?access_token={token}&department_id={department_id}&fetch_child={fetch_child}"),
                None,
            )
            .await?;

        Ok(resp.user_list.unwrap())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/278.html
    async fn batchcheck_user(&self, userids: &[&str]) -> Result<Vec<UserCheck>> {
        let token = self.access_token().await?;
        let resp = self
            .request::<Response>(
                Method::POST,
                &format!("{BASE_URL}/cgi-bin/user/batchcheck?access_token={token}"),
                Some(serde_json::json!({
                    "userlist": userids,
                })),
            )
            .await?;
        Ok(resp.list.unwrap())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/279.html
    async fn create_group(&self, params: ParamsCreateGroup) -> Result<()> {
        let token = self.access_token().await?;
        self.request::<Response>(
            Method::POST,
            &format!("{BASE_URL}/cgi-bin/group/create?access_token={token}"),
            Some(serde_json::to_value(params)?),
        )
        .await?;

        Ok(())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/280.html
    async fn update_group(&self, params: ParamsUpdateGroup) -> Result<()> {
        let token = self.access_token().await?;
        self.request::<Response>(
            Method::POST,
            &format!("{BASE_URL}/cgi-bin/group/update?access_token={token}"),
            Some(serde_json::to_value(params)?),
        )
        .await?;

        Ok(())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/281.html
    async fn delete_group(&self, group_id: &str) -> Result<()> {
        let token = self.access_token().await?;
        self.request::<Response>(
            Method::GET,
            &format!("{BASE_URL}/cgi-bin/group/delete?access_token={token}&groupid={group_id}"),
            None,
        )
        .await?;

        Ok(())
    }

    /// 参考接口说明:https://service.rtxmail.net/api/282.html
    async fn get_group(&self, group_id: &str) -> Result<Group> {
        let token = self.access_token().await?;
        let resp = self
            .request::<GetResponse<Group>>(
                Method::GET,
                &format!("{BASE_URL}/cgi-bin/group/get?access_token={token}&userid={group_id}"),
                None,
            )
            .await?;

        Ok(resp.data.unwrap())
    }
}

#[cfg(test)]
pub mod tests {

    use serde::{Deserialize, Serialize};

    #[derive(Debug, Deserialize, Serialize)]
    struct Rr {
        errcode: u64,
        errmsg: String,
        #[serde(rename = "userid")]
        user_id: String,
        name: String,
        department: Vec<u64>,
        position: String,
        mobile: String,
        gender: String,
        enable: u8,
        slaves: Vec<String>,
        cpwd_login: Option<u8>,
    }

    #[test]
    fn test() {
        let json_str = r##"{
            "errcode":0,
            "errmsg":"ok",
            "userid":"shenshouer2955@ipalfish.com",
            "name":"沈首二",
            "department":[7065571265906742219,6726112443993748595],
            "position":"",
            "mobile":"",
            "tel":"",
            "extid":"",
            "gender":"1",
            "slaves":[],
            "enable":1
        }"##;

        // let resp = serde_json::from_str::<Rr>(json_str);
        let resp = serde_json::from_str::<serde_json::Value>(json_str);
        println!("{:?}", resp);
        if let Ok(v) = resp {
            let r = serde_json::from_value::<super::GetResponse<super::User>>(v);
            println!("{:?}", r);
        }
    }
}