open-lark 0.13.0

Enterprise-grade Lark/Feishu Open API SDK with comprehensive Chinese documentation and advanced error handling
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use crate::{
    core::{
        api_req::ApiRequest, api_resp::ApiResponseTrait, config::Config,
        constants::AccessTokenType, http::Transport, req_option::RequestOption,
        standard_response::StandardResponse, trait_system::executable_builder::ExecutableBuilder,
        SDKResult,
    },
    service::contact::models::*,
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};

/// 用户管理服务
///
/// 提供完整的用户生命周期管理功能,包括:
/// - 创建、更新、删除用户
/// - 获取用户信息(单个/批量)
/// - 搜索用户
/// - 部门用户查询
/// - 恢复已删除用户
pub struct UserService {
    config: Config,
}

impl UserService {
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// 创建用户
    pub async fn create(
        &self,
        req: &CreateUserRequest,
    ) -> crate::core::SDKResult<CreateUserResponse> {
        let api_req = ApiRequest {
            http_method: reqwest::Method::POST,
            api_path: "/open-apis/contact/v3/users".to_string(),
            supported_access_token_types: vec![AccessTokenType::Tenant],
            body: serde_json::to_vec(req)?,
            ..Default::default()
        };

        let resp = Transport::<CreateUserResponse>::request(api_req, &self.config, None).await?;
        resp.into_result()
    }

    /// 修改用户部分信息
    pub async fn patch(
        &self,
        user_id: &str,
        req: &PatchUserRequest,
    ) -> crate::core::SDKResult<PatchUserResponse> {
        let api_req = ApiRequest {
            http_method: reqwest::Method::PATCH,
            api_path: format!("/open-apis/contact/v3/users/{user_id}"),
            supported_access_token_types: vec![AccessTokenType::Tenant],
            body: serde_json::to_vec(req)?,
            ..Default::default()
        };

        let resp = Transport::<PatchUserResponse>::request(api_req, &self.config, None).await?;
        resp.into_result()
    }

    /// 更新用户 ID
    pub async fn update_user_id(
        &self,
        user_id: &str,
        req: &UpdateUserIdRequest,
    ) -> crate::core::SDKResult<UpdateUserIdResponse> {
        let api_req = ApiRequest {
            http_method: reqwest::Method::PATCH,
            api_path: format!("/open-apis/contact/v3/users/{user_id}/update_user_id"),
            supported_access_token_types: vec![AccessTokenType::Tenant],
            body: serde_json::to_vec(req)?,
            ..Default::default()
        };

        let resp = Transport::<UpdateUserIdResponse>::request(api_req, &self.config, None).await?;
        resp.into_result()
    }

    /// 获取单个用户信息
    pub async fn get(
        &self,
        user_id: &str,
        _req: &GetUserRequest,
    ) -> crate::core::SDKResult<GetUserResponse> {
        let api_req = ApiRequest {
            http_method: reqwest::Method::GET,
            api_path: format!("/open-apis/contact/v3/users/{user_id}"),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            body: Vec::new(),
            query_params: std::collections::HashMap::new(),
            ..Default::default()
        };

        let resp = Transport::<GetUserResponse>::request(api_req, &self.config, None).await?;
        resp.into_result()
    }

    /// 批量获取用户信息
    pub async fn batch(
        &self,
        req: &BatchGetUsersRequest,
    ) -> crate::core::SDKResult<BatchGetUsersResponse> {
        let api_req = ApiRequest {
            http_method: reqwest::Method::POST,
            api_path: "/open-apis/contact/v3/users/batch".to_string(),
            supported_access_token_types: vec![AccessTokenType::Tenant],
            body: serde_json::to_vec(req)?,
            ..Default::default()
        };

        let resp = Transport::<BatchGetUsersResponse>::request(api_req, &self.config, None).await?;
        resp.into_result()
    }

    /// 获取部门直属用户列表
    pub async fn find_by_department(
        &self,
        _req: &FindUsersByDepartmentRequest,
    ) -> crate::core::SDKResult<FindUsersByDepartmentResponse> {
        let api_req = ApiRequest {
            http_method: reqwest::Method::GET,
            api_path: "/open-apis/contact/v3/users/find_by_department".to_string(),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            body: Vec::new(),
            query_params: std::collections::HashMap::new(),
            ..Default::default()
        };

        let resp = Transport::<FindUsersByDepartmentResponse>::request(api_req, &self.config, None)
            .await?;
        resp.into_result()
    }

    /// 通过手机号或邮箱获取用户 ID
    pub async fn batch_get_id(
        &self,
        req: &BatchGetUserIdRequest,
    ) -> crate::core::SDKResult<BatchGetUserIdResponse> {
        let api_req = ApiRequest {
            http_method: reqwest::Method::POST,
            api_path: "/open-apis/contact/v3/users/batch_get_id".to_string(),
            supported_access_token_types: vec![AccessTokenType::Tenant],
            body: serde_json::to_vec(req)?,
            ..Default::default()
        };

        let resp =
            Transport::<BatchGetUserIdResponse>::request(api_req, &self.config, None).await?;
        resp.into_result()
    }

    /// 搜索用户
    pub async fn search(
        &self,
        req: &SearchUsersRequest,
    ) -> crate::core::SDKResult<SearchUsersResponse> {
        let api_req = ApiRequest {
            http_method: reqwest::Method::POST,
            api_path: "/open-apis/contact/v3/users/search".to_string(),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            body: serde_json::to_vec(req)?,
            ..Default::default()
        };

        let resp = Transport::<SearchUsersResponse>::request(api_req, &self.config, None).await?;
        resp.into_result()
    }

    /// 删除用户
    pub async fn delete(
        &self,
        user_id: &str,
        _req: &DeleteUserRequest,
    ) -> crate::core::SDKResult<DeleteUserResponse> {
        let api_req = ApiRequest {
            http_method: reqwest::Method::DELETE,
            api_path: format!("/open-apis/contact/v3/users/{user_id}"),
            supported_access_token_types: vec![AccessTokenType::Tenant],
            body: Vec::new(),
            query_params: std::collections::HashMap::new(),
            ..Default::default()
        };

        let resp = Transport::<DeleteUserResponse>::request(api_req, &self.config, None).await?;
        resp.into_result()
    }

    /// 恢复已删除用户
    pub async fn resurrect(
        &self,
        user_id: &str,
        req: &ResurrectUserRequest,
    ) -> crate::core::SDKResult<ResurrectUserResponse> {
        let api_req = ApiRequest {
            http_method: reqwest::Method::POST,
            api_path: format!("/open-apis/contact/v3/users/{user_id}/resurrect"),
            supported_access_token_types: vec![AccessTokenType::Tenant],
            body: serde_json::to_vec(req)?,
            ..Default::default()
        };

        let resp = Transport::<ResurrectUserResponse>::request(api_req, &self.config, None).await?;
        resp.into_result()
    }

    /// 获取用户列表
    pub async fn list(&self, req: &ListUsersRequest) -> crate::core::SDKResult<ListUsersResponse> {
        let mut query_params = std::collections::HashMap::new();

        if let Some(page_size) = req.page_size {
            query_params.insert("page_size".to_string(), page_size.to_string());
        }
        if let Some(page_token) = &req.page_token {
            query_params.insert("page_token".to_string(), page_token.clone());
        }
        if let Some(user_id_type) = &req.user_id_type {
            query_params.insert("user_id_type".to_string(), user_id_type.clone());
        }
        if let Some(department_id_type) = &req.department_id_type {
            query_params.insert("department_id_type".to_string(), department_id_type.clone());
        }

        let api_req = ApiRequest {
            http_method: reqwest::Method::GET,
            api_path: "/open-apis/contact/v3/users".to_string(),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            body: Vec::new(),
            query_params,
            ..Default::default()
        };

        let resp = Transport::<ListUsersResponse>::request(api_req, &self.config, None).await?;
        resp.into_result()
    }

    /// 创建用户 - Builder模式 (推荐)
    ///
    /// 提供更现代化的Builder接口,支持链式调用和统一的执行模式
    pub fn create_user_builder(&self) -> CreateUserBuilder {
        CreateUserBuilder::new()
    }
}

/// 创建用户的Builder
#[derive(Default)]
pub struct CreateUserBuilder {
    user: Option<User>,
    user_id_type: Option<String>,
    department_id_type: Option<String>,
}

impl CreateUserBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    /// 设置用户信息
    pub fn user(mut self, user: User) -> Self {
        self.user = Some(user);
        self
    }

    /// 设置用户ID类型
    pub fn user_id_type(mut self, user_id_type: impl ToString) -> Self {
        self.user_id_type = Some(user_id_type.to_string());
        self
    }

    /// 设置部门ID类型
    pub fn department_id_type(mut self, department_id_type: impl ToString) -> Self {
        self.department_id_type = Some(department_id_type.to_string());
        self
    }

    pub fn build(self) -> CreateUserRequest {
        CreateUserRequest {
            user: self.user.unwrap_or_default(),
            user_id_type: self.user_id_type,
            department_id_type: self.department_id_type,
        }
    }
}

#[async_trait]
impl ExecutableBuilder<UserService, CreateUserRequest, CreateUserResponse> for CreateUserBuilder {
    fn build(self) -> CreateUserRequest {
        self.build()
    }

    async fn execute(self, service: &UserService) -> SDKResult<CreateUserResponse> {
        let req = self.build();
        service.create(&req).await
    }

    async fn execute_with_options(
        self,
        service: &UserService,
        _option: RequestOption,
    ) -> SDKResult<CreateUserResponse> {
        // 目前简单实现,后续可以支持传递option到service方法
        let req = self.build();
        service.create(&req).await
    }
}

// 请求/响应结构体定义

/// 创建用户请求
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateUserRequest {
    /// 用户信息
    pub user: User,
    /// 用户 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id_type: Option<String>,
    /// 部门 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department_id_type: Option<String>,
}

/// 创建用户响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CreateUserResponse {
    /// 用户信息
    pub user: User,
}

impl ApiResponseTrait for CreateUserResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}

/// 修改用户请求
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatchUserRequest {
    /// 用户信息
    pub user: User,
    /// 用户 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id_type: Option<String>,
    /// 部门 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department_id_type: Option<String>,
}

/// 修改用户响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PatchUserResponse {
    /// 用户信息
    pub user: User,
}

impl ApiResponseTrait for PatchUserResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}

/// 更新用户ID请求
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateUserIdRequest {
    /// 新的用户ID
    pub new_user_id: String,
    /// 用户 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id_type: Option<String>,
}

/// 更新用户ID响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UpdateUserIdResponse {}

impl ApiResponseTrait for UpdateUserIdResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}

/// 获取用户请求
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GetUserRequest {
    /// 用户 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id_type: Option<String>,
    /// 部门 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department_id_type: Option<String>,
}

/// 获取用户响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GetUserResponse {
    /// 用户信息
    pub user: User,
}

impl ApiResponseTrait for GetUserResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}

/// 批量获取用户请求
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchGetUsersRequest {
    /// 用户ID列表
    pub user_ids: Vec<String>,
    /// 用户 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id_type: Option<String>,
    /// 部门 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department_id_type: Option<String>,
}

/// 批量获取用户响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BatchGetUsersResponse {
    /// 用户列表
    pub items: Vec<User>,
}

impl ApiResponseTrait for BatchGetUsersResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}

/// 按部门查找用户请求
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FindUsersByDepartmentRequest {
    /// 部门ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department_id: Option<String>,
    /// 用户 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id_type: Option<String>,
    /// 部门 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department_id_type: Option<String>,
    /// 分页大小
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i32>,
    /// 分页标记
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
}

/// 按部门查找用户响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FindUsersByDepartmentResponse {
    /// 用户列表
    pub items: Vec<User>,
    /// 是否还有更多项目
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_more: Option<bool>,
    /// 分页标记
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
}

impl ApiResponseTrait for FindUsersByDepartmentResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}

/// 批量获取用户ID请求
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchGetUserIdRequest {
    /// 邮箱列表
    #[serde(skip_serializing_if = "Option::is_none")]
    pub emails: Option<Vec<String>>,
    /// 手机号列表
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mobiles: Option<Vec<String>>,
    /// 包含已离职用户
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_resigned: Option<bool>,
}

/// 批量获取用户ID响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BatchGetUserIdResponse {
    /// 用户列表
    pub user_list: Vec<UserIdInfo>,
}

impl ApiResponseTrait for BatchGetUserIdResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}

/// 用户ID信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserIdInfo {
    /// 用户ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id: Option<String>,
    /// 邮箱
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    /// 手机号
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mobile: Option<String>,
}

/// 搜索用户请求
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchUsersRequest {
    /// 搜索关键词
    pub query: String,
    /// 分页大小
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i32>,
    /// 分页标记
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
    /// 用户 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id_type: Option<String>,
    /// 部门 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department_id_type: Option<String>,
}

/// 搜索用户响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SearchUsersResponse {
    /// 用户列表
    pub items: Vec<User>,
    /// 是否还有更多项目
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_more: Option<bool>,
    /// 分页标记
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
}

impl ApiResponseTrait for SearchUsersResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}

/// 删除用户请求
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DeleteUserRequest {
    /// 用户 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id_type: Option<String>,
    /// 部门 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department_id_type: Option<String>,
}

/// 删除用户响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DeleteUserResponse {}

impl ApiResponseTrait for DeleteUserResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}

/// 恢复用户请求
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResurrectUserRequest {
    /// 用户 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id_type: Option<String>,
    /// 部门 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department_id_type: Option<String>,
}

/// 恢复用户响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ResurrectUserResponse {
    /// 用户信息
    pub user: User,
}

impl ApiResponseTrait for ResurrectUserResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}

/// 获取用户列表请求
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ListUsersRequest {
    /// 分页大小
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_size: Option<i32>,
    /// 分页标记
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
    /// 用户 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id_type: Option<String>,
    /// 部门 ID 类型
    #[serde(skip_serializing_if = "Option::is_none")]
    pub department_id_type: Option<String>,
}

/// 获取用户列表响应
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ListUsersResponse {
    /// 用户列表
    pub items: Vec<User>,
    /// 是否还有更多项目
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_more: Option<bool>,
    /// 分页标记
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
}

impl ApiResponseTrait for ListUsersResponse {
    fn data_format() -> crate::core::api_resp::ResponseFormat {
        crate::core::api_resp::ResponseFormat::Data
    }
}