open-lark 0.14.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
//! 通讯录(Contact)服务
//!
//! 提供企业通讯录的完整管理功能,支持用户、部门、组织架构等核心要素的
//! 生命周期管理。这是企业人员和组织管理的核心服务模块。
//!
//! # 核心功能
//!
//! ## 用户管理
//! - 👤 用户信息的增删改查
//! - 🔄 用户状态和生命周期管理
//! - 📧 用户身份验证和邮箱管理
//! - 🏷️ 用户标签和分组管理
//!
//! ## 组织架构
//! - 🏢 部门层级结构管理
//! - 👥 部门成员和负责人设置
//! - 📊 组织架构调整和优化
//! - 🔄 部门合并和拆分操作
//!
//! ## 权限和角色
//! - 🔐 权限范围管理和控制
//! - 👑 用户组和角色分配
//! - 🎯 精细化权限控制
//! - 🔒 安全策略和访问控制
//!
//! ## 职级职务
//! - 🎖️ 职级体系管理
//! - 💼 职务分配和调整
//! - 📈 晋升和调岗流程
//! - 📋 职位描述和要求
//!
//! # 使用示例
//!
//! ```rust
//! use open_lark::prelude::*;
//!
//! let client = LarkClient::builder("app_id", "app_secret")
//!     .with_app_type(AppType::SelfBuild)
//!     .build();
//!
//! // 获取通讯录服务
//! let contact = &client.contact;
//!
//! // 使用v3版本API
//! // let users = contact.v3.user.list(request, None).await?;
//! // let departments = contact.v3.department.list(request, None).await?;
//! ```
//!
//! # API版本
//!
//! 当前支持v3版本,提供最新的功能和最佳性能。v3版本包含:
//! - 增强的用户管理功能
//! - 灵活的组织架构操作
//! - 完善的权限控制机制
//! - 高效的批量操作支持

/// 通讯录数据模型定义
pub mod models;
/// 通讯录服务 v3 版本
pub mod v3;

pub use models::*;
pub use v3::*;

use crate::core::config::Config;

/// 通讯录服务
///
/// 企业通讯录的统一管理入口,提供完整的人员和组织管理功能。
/// 支持企业级的用户生命周期管理、组织架构调整和权限控制。
///
/// # 服务架构
///
/// - **v3**: 最新版本API,提供完整功能集
/// - **models**: 数据模型和结构定义
///
/// # 核心特性
///
/// - 🚀 高性能批量操作
/// - 🔐 企业级安全控制
/// - 📊 灵活的组织架构
/// - 🎯 精细化权限管理
/// - 🔄 完整的生命周期支持
///
/// # 适用场景
///
/// - 企业人力资源管理
/// - 组织架构调整和优化
/// - 权限和访问控制
/// - 用户身份管理
/// - 通讯录同步和集成
///
/// # 最佳实践
///
/// - 定期同步和更新用户信息
/// - 合理设计组织架构层级
/// - 遵循最小权限原则
/// - 建立完善的审计机制
pub struct ContactService {
    /// v3版本API服务
    pub v3: v3::V3,
}

impl ContactService {
    /// 创建新的通讯录服务实例
    ///
    /// # 参数
    /// - `config`: 客户端配置,包含认证信息和API设置
    ///
    /// # 返回值
    /// 配置完成的通讯录服务实例
    pub fn new(config: Config) -> Self {
        Self {
            v3: v3::V3::new(config),
        }
    }

    /// 使用共享配置创建服务(实验性)
    pub fn new_from_shared(shared: std::sync::Arc<Config>) -> Self {
        Self {
            v3: v3::V3::new(shared.as_ref().clone()),
        }
    }
}

#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
    use super::*;
    use crate::core::config::Config;

    fn create_test_config() -> Config {
        Config::default()
    }

    #[test]
    fn test_contact_service_creation() {
        let config = create_test_config();
        let _contact_service = ContactService::new(config);

        // Verify service structure
    }

    #[test]
    fn test_contact_service_with_custom_config() {
        let config = Config::builder()
            .app_id("contact_app")
            .app_secret("contact_secret")
            .req_timeout(std::time::Duration::from_millis(8000))
            .base_url("https://contact.api.com")
            .build();

        let _contact_service = ContactService::new(config);

        // Verify service creation with custom config
    }

    #[test]
    fn test_contact_service_multiple_instances() {
        let configs = vec![
            Config::builder()
                .app_id("app1")
                .app_secret("secret1")
                .build(),
            Config::builder()
                .app_id("app2")
                .app_secret("secret2")
                .req_timeout(std::time::Duration::from_millis(12000))
                .build(),
            Config::builder()
                .app_id("app3")
                .app_secret("secret3")
                .base_url("https://custom.contact.com")
                .build(),
        ];

        let mut services = Vec::new();
        for config in configs {
            let service = ContactService::new(config);
            services.push(service);
        }

        // All services should be created successfully
        assert_eq!(services.len(), 3);
        for _service in &services {}

        // Services should be independent instances
        for (i, service1) in services.iter().enumerate() {
            for service2 in services.iter().skip(i + 1) {
                let ptr1 = std::ptr::addr_of!(*service1) as *const u8;
                let ptr2 = std::ptr::addr_of!(*service2) as *const u8;
                assert_ne!(ptr1, ptr2, "Services should be independent instances");
            }
        }
    }

    #[test]
    fn test_contact_service_config_cloning() {
        let config = create_test_config();

        // Test that the service works with cloned configs
        let contact_service1 = ContactService::new(config.clone());
        let contact_service2 = ContactService::new(config);

        // Both should work independently

        // But should be different service instances
        let service1_ptr = std::ptr::addr_of!(contact_service1) as *const u8;
        let service2_ptr = std::ptr::addr_of!(contact_service2) as *const u8;
        assert_ne!(service1_ptr, service2_ptr);
    }

    #[test]
    fn test_contact_service_v3_api_access() {
        let config = create_test_config();
        let contact_service = ContactService::new(config);

        // Verify that v3 API is accessible
        let v3_ptr = std::ptr::addr_of!(contact_service.v3) as *const u8;
        assert!(
            !v3_ptr.is_null(),
            "V3 service should be properly instantiated"
        );
    }

    #[test]
    fn test_contact_service_with_various_configurations() {
        let variations = vec![
            (
                "minimal",
                Config::builder()
                    .app_id("minimal")
                    .app_secret("secret")
                    .build(),
            ),
            (
                "with_timeout",
                Config::builder()
                    .app_id("timeout")
                    .app_secret("secret")
                    .req_timeout(std::time::Duration::from_millis(15000))
                    .build(),
            ),
            (
                "with_base_url",
                Config::builder()
                    .app_id("base_url")
                    .app_secret("secret")
                    .base_url("https://test.contact.api.com")
                    .build(),
            ),
            (
                "full_featured",
                Config::builder()
                    .app_id("full")
                    .app_secret("secret")
                    .req_timeout(std::time::Duration::from_millis(20000))
                    .base_url("https://full.test.contact.api.com")
                    .enable_token_cache(true)
                    .build(),
            ),
        ];

        let mut services = Vec::new();
        for (name, config) in variations {
            let service = ContactService::new(config);
            services.push((name, service));
        }

        // All variations should create valid services
        assert_eq!(services.len(), 4);

        // Test that all services are independent
        for (i, (_, service1)) in services.iter().enumerate() {
            for (_, service2) in services.iter().skip(i + 1) {
                let ptr1 = std::ptr::addr_of!(*service1) as *const u8;
                let ptr2 = std::ptr::addr_of!(*service2) as *const u8;
                assert_ne!(
                    ptr1, ptr2,
                    "Services with different configs should be independent"
                );
            }
        }
    }

    #[test]
    fn test_contact_service_concurrent_creation() {
        let configs = vec![
            Config::builder()
                .app_id("contact_concurrent_1")
                .app_secret("secret_1")
                .build(),
            Config::builder()
                .app_id("contact_concurrent_2")
                .app_secret("secret_2")
                .build(),
            Config::builder()
                .app_id("contact_concurrent_3")
                .app_secret("secret_3")
                .build(),
        ];

        let mut services = Vec::new();
        for config in configs {
            let service = ContactService::new(config);
            services.push(service);
        }

        // All services should be created successfully
        assert_eq!(services.len(), 3);

        // Verify all services are independent
        for (i, service1) in services.iter().enumerate() {
            for service2 in services.iter().skip(i + 1) {
                let ptr1 = std::ptr::addr_of!(*service1) as *const u8;
                let ptr2 = std::ptr::addr_of!(*service2) as *const u8;
                assert_ne!(ptr1, ptr2, "Services should be independent instances");
            }
        }
    }

    #[test]
    fn test_contact_service_extreme_configurations() {
        let extreme_configs = vec![
            // Very short timeout
            Config::builder()
                .app_id("contact_fast")
                .app_secret("fast_secret")
                .req_timeout(std::time::Duration::from_millis(10))
                .build(),
            // Very long timeout
            Config::builder()
                .app_id("contact_slow")
                .app_secret("slow_secret")
                .req_timeout(std::time::Duration::from_secs(900))
                .build(),
            // Token cache disabled
            Config::builder()
                .app_id("contact_no_cache")
                .app_secret("no_cache_secret")
                .enable_token_cache(false)
                .build(),
            // Custom contact URL
            Config::builder()
                .app_id("contact_custom_base")
                .app_secret("custom_base_secret")
                .base_url("https://custom.contact.api.endpoint")
                .build(),
        ];

        for config in extreme_configs {
            let contact_service = ContactService::new(config);

            // Each service should be created successfully regardless of extreme config
            let service_ptr = std::ptr::addr_of!(contact_service) as *const u8;
            assert!(
                !service_ptr.is_null(),
                "Service should be created with extreme config"
            );
        }
    }

    #[test]
    fn test_contact_service_memory_consistency() {
        let config = create_test_config();
        let contact_service = ContactService::new(config);

        // Test that the service maintains memory consistency across accesses
        let service_ptr1 = std::ptr::addr_of!(contact_service) as *const u8;
        let service_ptr2 = std::ptr::addr_of!(contact_service) as *const u8;

        assert_eq!(
            service_ptr1, service_ptr2,
            "Service memory address should be consistent"
        );

        // Test API version consistency
        let v3_ptr1 = std::ptr::addr_of!(contact_service.v3) as *const u8;
        let v3_ptr2 = std::ptr::addr_of!(contact_service.v3) as *const u8;

        assert_eq!(
            v3_ptr1, v3_ptr2,
            "V3 API memory address should be consistent"
        );
    }

    #[test]
    fn test_contact_service_v3_api_completeness() {
        let config = create_test_config();
        let contact_service = ContactService::new(config);

        // Test V3 API structure exists and is accessible
        let v3_ptr = std::ptr::addr_of!(contact_service.v3) as *const u8;
        assert!(!v3_ptr.is_null(), "V3 Contact API should be instantiated");
    }

    #[test]
    fn test_contact_service_config_independence() {
        let config1 = Config::builder()
            .app_id("contact_app1")
            .app_secret("contact_secret1")
            .build();
        let config2 = Config::builder()
            .app_id("contact_app2")
            .app_secret("contact_secret2")
            .build();

        let contact_service1 = ContactService::new(config1);
        let contact_service2 = ContactService::new(config2);

        // Services should be independent instances
        let service1_ptr = std::ptr::addr_of!(contact_service1) as *const u8;
        let service2_ptr = std::ptr::addr_of!(contact_service2) as *const u8;

        assert_ne!(
            service1_ptr, service2_ptr,
            "Services should be independent instances"
        );

        // V3 APIs should also be independent
        let v3_ptr1 = std::ptr::addr_of!(contact_service1.v3) as *const u8;
        let v3_ptr2 = std::ptr::addr_of!(contact_service2.v3) as *const u8;

        assert_ne!(v3_ptr1, v3_ptr2, "V3 services should be independent");
    }

    #[test]
    fn test_contact_service_configuration_scenarios() {
        // Test empty config handling
        let empty_config = Config::default();
        let contact_service_empty = ContactService::new(empty_config);
        let empty_ptr = std::ptr::addr_of!(contact_service_empty) as *const u8;
        assert!(!empty_ptr.is_null(), "Service should handle empty config");

        // Test minimal config
        let minimal_config = Config::builder().app_id("min").app_secret("sec").build();
        let contact_service_minimal = ContactService::new(minimal_config);
        let minimal_ptr = std::ptr::addr_of!(contact_service_minimal) as *const u8;
        assert!(
            !minimal_ptr.is_null(),
            "Service should handle minimal config"
        );

        // Test Unicode config
        let unicode_config = Config::builder()
            .app_id("通讯录应用")
            .app_secret("通讯录密钥")
            .base_url("https://通讯录.com")
            .build();
        let contact_service_unicode = ContactService::new(unicode_config);
        let unicode_ptr = std::ptr::addr_of!(contact_service_unicode) as *const u8;
        assert!(
            !unicode_ptr.is_null(),
            "Service should handle Unicode config"
        );
    }

    #[test]
    fn test_contact_service_api_structure() {
        let config = create_test_config();
        let contact_service = ContactService::new(config);

        // Verify the service contains v3 API
        let v3_offset = std::ptr::addr_of!(contact_service.v3) as usize
            - std::ptr::addr_of!(contact_service) as usize;

        // V3 offset should be reasonable (within struct bounds)
        assert!(v3_offset < 4096, "V3 offset should be reasonable");
    }

    #[test]
    fn test_contact_service_long_credentials() {
        let long_string = "a".repeat(2000);
        let config = Config::builder()
            .app_id(long_string.clone())
            .app_secret(long_string.clone())
            .base_url(format!("https://{}.contact.com", "test"))
            .build();

        let contact_service = ContactService::new(config);
        let service_ptr = std::ptr::addr_of!(contact_service) as *const u8;
        assert!(
            !service_ptr.is_null(),
            "Service should handle long credentials"
        );
    }
}