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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
use std::sync::Arc;
use std::time::Duration;

use crate::core::{
    config::{Config, ConfigBuilder},
    constants::AppType,
};

// 条件导入服务
#[cfg(feature = "acs")]
use crate::service::acs::AcsService;
#[cfg(feature = "admin")]
use crate::service::admin::AdminService;
#[cfg(feature = "ai")]
use crate::service::ai::AiService;
#[cfg(feature = "aily")]
use crate::service::aily::AilyService;
#[cfg(feature = "apass")]
use crate::service::apass::ApassService;
#[cfg(feature = "application")]
use crate::service::application::ApplicationService;
#[cfg(feature = "approval")]
use crate::service::approval::ApprovalService;
#[cfg(feature = "attendance")]
use crate::service::attendance::AttendanceService;
#[cfg(feature = "authentication")]
use crate::service::authentication::AuthenService;
#[cfg(feature = "bot")]
use crate::service::bot::BotService;
#[cfg(feature = "calendar")]
use crate::service::calendar::CalendarService;
#[cfg(feature = "cardkit")]
use crate::service::cardkit::CardkitService;
#[cfg(feature = "cloud-docs")]
use crate::service::cloud_docs::CloudDocsService;
#[cfg(feature = "contact")]
use crate::service::contact::ContactService;
#[cfg(feature = "corehr")]
use crate::service::corehr::CoreHRService;
#[cfg(feature = "directory")]
use crate::service::directory::DirectoryService;
#[cfg(feature = "ehr")]
use crate::service::ehr::EhrService;
#[cfg(feature = "elearning")]
use crate::service::elearning::ELearningService;
#[cfg(feature = "group")]
use crate::service::group::GroupService;
#[cfg(feature = "helpdesk")]
use crate::service::helpdesk::HelpdeskService;
#[cfg(feature = "hire")]
use crate::service::hire::HireService;
#[cfg(feature = "human-authentication")]
use crate::service::human_authentication::HumanAuthenticationService;
#[cfg(feature = "im")]
use crate::service::im::ImService;
#[cfg(feature = "lingo")]
use crate::service::lingo::LingoService;
#[cfg(feature = "mail")]
use crate::service::mail::MailService;
#[cfg(feature = "mdm")]
use crate::service::mdm::MdmService;
#[cfg(feature = "minutes")]
use crate::service::minutes::MinutesService;
#[cfg(feature = "moments")]
use crate::service::moments::MomentsService;
#[cfg(feature = "okr")]
use crate::service::okr::OkrService;
#[cfg(feature = "payroll")]
use crate::service::payroll::PayrollService;
#[cfg(feature = "performance")]
use crate::service::performance::PerformanceService;
#[cfg(feature = "personal-settings")]
use crate::service::personal_settings::PersonalSettingsService;
#[cfg(feature = "report")]
use crate::service::report::ReportService;
#[cfg(feature = "search")]
use crate::service::search::SearchService;
#[cfg(feature = "security-and-compliance")]
use crate::service::security_and_compliance::SecurityAndComplianceService;
#[cfg(feature = "task")]
use crate::service::task::TaskV2Service;
#[cfg(feature = "tenant")]
use crate::service::tenant::TenantService;
#[cfg(feature = "tenant-tag")]
use crate::service::tenant_tag::TenantTagService;
#[cfg(feature = "trust-party")]
use crate::service::trust_party::TrustPartyService;
#[cfg(feature = "vc")]
use crate::service::vc::VcService;
#[cfg(feature = "verification")]
use crate::service::verification::VerificationService;
#[cfg(feature = "workplace")]
use crate::service::workplace::WorkplaceService;

// 向后兼容的导入
#[cfg(feature = "cloud-docs")]
use crate::service::{
    AssistantService, BitableService, BoardService, CommentsService, DocsService, DriveService,
    PermissionService, SheetsService, WikiService,
};

#[cfg(feature = "websocket")]
pub mod ws_client;

/// 飞书开放平台SDK主客户端
///
/// 提供对所有飞书开放平台API的统一访问接口。支持自建应用和商店应用两种类型,
/// 自动处理认证、令牌管理、请求重试等核心功能。
///
/// # 主要功能
///
/// - 🔐 自动令牌管理和刷新
/// - 🚀 支持所有飞书开放平台API
/// - 🔄 内置请求重试机制
/// - 📡 WebSocket长连接支持(需开启websocket特性)
/// - 🎯 类型安全的API调用
///
/// # 快速开始
///
/// ```rust
/// use open_lark::prelude::*;
///
/// // 创建自建应用客户端
/// let client = LarkClient::builder("your_app_id", "your_app_secret")
///     .with_app_type(AppType::SelfBuild)
///     .with_enable_token_cache(true)
///     .build();
///
/// // 发送文本消息
/// let message = CreateMessageRequestBody::builder()
///     .receive_id("ou_xxx")
///     .msg_type("text")
///     .content("{\"text\":\"Hello from Rust!\"}")
///     .build();
///
/// let request = CreateMessageRequest::builder()
///     .receive_id_type("open_id")
///     .request_body(message)
///     .build();
///
/// // let result = client.im.message.create(request, None).await?;
/// ```
///
/// # 服务模块
///
/// 客户端包含以下主要服务模块:
/// - `im`: 即时消息
/// - `drive`: 云盘文件
/// - `sheets`: 电子表格
/// - `calendar`: 日历
/// - `contact`: 通讯录
/// - `hire`: 招聘
/// - 更多服务请参考各字段文档
pub struct LarkClient {
    pub config: Config,
    /// 共享配置(实验性):单一 `Arc<Config>`,用于内部服务扇出以减少 clone
    #[allow(dead_code)] // Used in constructor and tests
    shared_config: Arc<Config>,
    // 核心服务 - 使用条件编译
    #[cfg(feature = "acs")]
    pub acs: AcsService,
    #[cfg(feature = "admin")]
    pub admin: AdminService,
    #[cfg(feature = "ai")]
    pub ai: AiService,
    #[cfg(feature = "aily")]
    pub aily: AilyService,
    #[cfg(feature = "apass")]
    pub apass: ApassService,
    #[cfg(feature = "application")]
    pub application: ApplicationService,
    #[cfg(feature = "approval")]
    pub approval: ApprovalService,
    #[cfg(feature = "attendance")]
    pub attendance: AttendanceService,
    #[cfg(feature = "authentication")]
    pub auth: AuthenService,
    #[cfg(feature = "bot")]
    pub bot: BotService,
    #[cfg(feature = "calendar")]
    pub calendar: CalendarService,
    #[cfg(feature = "cardkit")]
    pub cardkit: CardkitService,
    #[cfg(feature = "contact")]
    pub contact: ContactService,
    #[cfg(feature = "corehr")]
    pub corehr: CoreHRService,
    #[cfg(feature = "directory")]
    pub directory: DirectoryService,
    #[cfg(feature = "ehr")]
    pub ehr: EhrService,
    #[cfg(feature = "elearning")]
    pub elearning: ELearningService,
    #[cfg(feature = "group")]
    pub group: GroupService,
    #[cfg(feature = "helpdesk")]
    pub helpdesk: HelpdeskService,
    #[cfg(feature = "hire")]
    pub hire: HireService,
    #[cfg(feature = "human-authentication")]
    pub human_authentication: HumanAuthenticationService,
    #[cfg(feature = "im")]
    pub im: ImService,
    #[cfg(feature = "lingo")]
    pub lingo: LingoService,
    #[cfg(feature = "mail")]
    pub mail: MailService,
    #[cfg(feature = "mdm")]
    pub mdm: MdmService,
    #[cfg(feature = "minutes")]
    pub minutes: MinutesService,
    #[cfg(feature = "moments")]
    pub moments: MomentsService,
    #[cfg(feature = "okr")]
    pub okr: OkrService,
    #[cfg(feature = "payroll")]
    pub payroll: PayrollService,
    #[cfg(feature = "performance")]
    pub performance: PerformanceService,
    #[cfg(feature = "personal-settings")]
    pub personal_settings: PersonalSettingsService,
    #[cfg(feature = "report")]
    pub report: ReportService,
    #[cfg(feature = "search")]
    pub search: SearchService,
    #[cfg(feature = "security-and-compliance")]
    pub security_and_compliance: SecurityAndComplianceService,
    #[cfg(feature = "task")]
    pub task: TaskV2Service,
    #[cfg(feature = "tenant")]
    pub tenant: TenantService,
    #[cfg(feature = "tenant-tag")]
    pub tenant_tag: TenantTagService,
    #[cfg(feature = "trust-party")]
    pub trust_party: TrustPartyService,
    #[cfg(feature = "vc")]
    pub vc: VcService,
    #[cfg(feature = "verification")]
    pub verification: VerificationService,
    #[cfg(feature = "workplace")]
    pub workplace: WorkplaceService,
    // 云文档服务聚合
    #[cfg(feature = "cloud-docs")]
    pub cloud_docs: CloudDocsService,
    // 向后兼容的字段
    #[cfg(feature = "cloud-docs")]
    pub assistant: AssistantService,
    #[cfg(feature = "cloud-docs")]
    pub docs: DocsService,
    #[cfg(feature = "cloud-docs")]
    pub drive: DriveService,
    #[cfg(feature = "cloud-docs")]
    pub sheets: SheetsService,
    #[cfg(feature = "cloud-docs")]
    pub bitable: BitableService,
    #[cfg(feature = "cloud-docs")]
    pub wiki: WikiService,
    #[cfg(feature = "cloud-docs")]
    pub comments: CommentsService,
    #[cfg(feature = "cloud-docs")]
    pub permission: PermissionService,
    #[cfg(feature = "cloud-docs")]
    pub board: BoardService,
}

/// 飞书客户端构建器
///
/// 使用构建器模式配置和创建LarkClient实例。支持链式调用配置各种选项。
///
/// # 示例
///
/// ```rust
/// use open_lark::prelude::*;
///
/// let client = LarkClient::builder("app_id", "app_secret")
///     .with_app_type(AppType::SelfBuild)
///     .with_enable_token_cache(true)
///     .with_req_timeout(Some(30.0))
///     .build();
/// ```
pub struct LarkClientBuilder {
    config_builder: ConfigBuilder,
}

impl LarkClientBuilder {
    /// 获取当前配置构建器(仅测试使用)
    #[cfg(test)]
    fn build_config(&self) -> Config {
        self.config_builder.clone().build()
    }

    /// 设置应用类型
    ///
    /// # 参数
    /// - `app_type`: 应用类型,`AppType::SelfBuild`(自建应用)或`AppType::Marketplace`(商店应用)
    pub fn with_app_type(mut self, app_type: AppType) -> Self {
        self.config_builder = self.config_builder.app_type(app_type);
        self
    }

    /// 设置为商店应用(等同于 `with_app_type(AppType::Marketplace)`)
    pub fn with_marketplace_app(mut self) -> Self {
        self.config_builder = self.config_builder.app_type(AppType::Marketplace);
        self
    }

    /// 设置自定义API基础URL
    ///
    /// # 参数
    /// - `base_url`: 自定义的API基础URL,默认为官方地址
    pub fn with_open_base_url(mut self, base_url: String) -> Self {
        self.config_builder = self.config_builder.base_url(base_url);
        self
    }

    /// 启用或禁用令牌缓存
    ///
    /// # 参数
    /// - `enable`: 是否启用令牌缓存,建议启用以提高性能
    pub fn with_enable_token_cache(mut self, enable: bool) -> Self {
        self.config_builder = self.config_builder.enable_token_cache(enable);
        self
    }

    /// 设置请求超时时间
    ///
    /// # 参数
    /// - `timeout`: 超时时间(秒),None表示使用默认值
    pub fn with_req_timeout(mut self, timeout: Option<f32>) -> Self {
        if let Some(timeout) = timeout {
            self.config_builder = self
                .config_builder
                .req_timeout(Duration::from_secs_f32(timeout));
        }
        self
    }

    /// 构建LarkClient实例
    ///
    /// 根据配置的参数创建最终的客户端实例。
    pub fn build(self) -> LarkClient {
        let config = self.config_builder.build();
        let shared_config = Arc::new(config.clone());
        LarkClient {
            config: config.clone(),
            shared_config: shared_config.clone(),
            // 核心服务 - 使用条件编译
            #[cfg(feature = "acs")]
            acs: AcsService::new(config.clone()),
            #[cfg(feature = "admin")]
            admin: AdminService::new(config.clone()),
            #[cfg(feature = "ai")]
            ai: AiService::new(config.clone()),
            #[cfg(feature = "aily")]
            aily: AilyService::new(config.clone()),
            #[cfg(feature = "apass")]
            apass: ApassService::new(config.clone()),
            #[cfg(feature = "application")]
            application: ApplicationService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "approval")]
            approval: ApprovalService::new(config.clone()),
            #[cfg(feature = "attendance")]
            attendance: AttendanceService::new(config.clone()),
            #[cfg(feature = "authentication")]
            auth: AuthenService::new(config.clone()),
            #[cfg(feature = "bot")]
            bot: BotService::new(config.clone()),
            #[cfg(feature = "calendar")]
            calendar: CalendarService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "cardkit")]
            cardkit: CardkitService::new(config.clone()),
            #[cfg(feature = "contact")]
            contact: ContactService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "corehr")]
            corehr: CoreHRService::new(config.clone()),
            #[cfg(feature = "directory")]
            directory: DirectoryService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "ehr")]
            ehr: EhrService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "elearning")]
            elearning: ELearningService::new(config.clone()),
            #[cfg(feature = "group")]
            group: GroupService::new(config.clone()),
            #[cfg(feature = "helpdesk")]
            helpdesk: HelpdeskService::new(config.clone()),
            #[cfg(feature = "hire")]
            hire: HireService::new(config.clone()),
            #[cfg(feature = "human-authentication")]
            human_authentication: HumanAuthenticationService::new(config.clone()),
            #[cfg(feature = "im")]
            im: ImService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "lingo")]
            lingo: LingoService::new(config.clone()),
            #[cfg(feature = "mail")]
            mail: MailService::new(config.clone()),
            #[cfg(feature = "mdm")]
            mdm: MdmService::new(config.clone()),
            #[cfg(feature = "minutes")]
            minutes: MinutesService::new(config.clone()),
            #[cfg(feature = "moments")]
            moments: MomentsService::new(config.clone()),
            #[cfg(feature = "okr")]
            okr: OkrService::new(config.clone()),
            #[cfg(feature = "payroll")]
            payroll: PayrollService::new(config.clone()),
            #[cfg(feature = "performance")]
            performance: PerformanceService::new(config.clone()),
            #[cfg(feature = "personal-settings")]
            personal_settings: PersonalSettingsService::new(config.clone()),
            #[cfg(feature = "report")]
            report: ReportService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "search")]
            search: SearchService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "security-and-compliance")]
            security_and_compliance: SecurityAndComplianceService::new_from_shared(
                shared_config.clone(),
            ),
            #[cfg(feature = "task")]
            task: TaskV2Service::new_from_shared(shared_config.clone()),
            #[cfg(feature = "tenant")]
            tenant: TenantService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "tenant-tag")]
            tenant_tag: TenantTagService::new(config.clone()),
            #[cfg(feature = "trust-party")]
            trust_party: TrustPartyService::new(config.clone()),
            #[cfg(feature = "vc")]
            vc: VcService::new(config.clone()),
            #[cfg(feature = "verification")]
            verification: VerificationService::new(config.clone()),
            #[cfg(feature = "workplace")]
            workplace: WorkplaceService::new(config.clone()),
            // 云文档服务聚合
            #[cfg(feature = "cloud-docs")]
            cloud_docs: CloudDocsService::new_from_shared(shared_config.clone()),
            // 向后兼容的字段(重新创建实例)
            #[cfg(feature = "cloud-docs")]
            assistant: AssistantService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "cloud-docs")]
            docs: DocsService::new(config.clone()),
            #[cfg(feature = "cloud-docs")]
            drive: DriveService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "cloud-docs")]
            sheets: SheetsService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "cloud-docs")]
            bitable: BitableService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "cloud-docs")]
            wiki: WikiService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "cloud-docs")]
            comments: CommentsService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "cloud-docs")]
            permission: PermissionService::new_from_shared(shared_config.clone()),
            #[cfg(feature = "cloud-docs")]
            board: BoardService::new_from_shared(shared_config.clone()),
        }
    }
}

impl LarkClient {
    /// 创建客户端构建器
    ///
    /// # 参数
    /// - `app_id`: 应用ID,从飞书开放平台获取
    /// - `app_secret`: 应用密钥,从飞书开放平台获取
    ///
    /// # 示例
    /// ```rust
    /// use open_lark::prelude::*;
    ///
    /// let client = LarkClient::builder("cli_xxx", "xxx")
    ///     .with_app_type(AppType::SelfBuild)
    ///     .build();
    /// ```
    pub fn builder(app_id: &str, app_secret: &str) -> LarkClientBuilder {
        LarkClientBuilder {
            config_builder: Config::builder().app_id(app_id).app_secret(app_secret),
        }
    }

    /// 获取共享配置(用于内部服务扇出,减少 clone)
    #[allow(dead_code)] // Used by services in constructor
    pub(crate) fn shared_config(&self) -> Arc<Config> {
        self.shared_config.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    fn create_test_builder() -> LarkClientBuilder {
        LarkClient::builder("test_app_id", "test_app_secret")
    }

    #[test]
    fn test_client_builder_creation() {
        let client = LarkClient::builder("test_id", "test_secret").build();
        assert_eq!(client.config.app_id, "test_id");
        assert_eq!(client.config.app_secret, "test_secret");
        assert_eq!(client.config.app_type, AppType::SelfBuild); // Default
    }

    #[test]
    fn test_builder_with_app_type() {
        let client = create_test_builder()
            .with_app_type(AppType::Marketplace)
            .build();
        assert_eq!(client.config.app_type, AppType::Marketplace);
    }

    #[test]
    fn test_builder_with_marketplace_app() {
        let client = create_test_builder().with_marketplace_app().build();
        assert_eq!(client.config.app_type, AppType::Marketplace);
    }

    #[test]
    fn test_builder_with_custom_base_url() {
        let custom_url = "https://custom.api.feishu.cn";
        let client = create_test_builder()
            .with_open_base_url(custom_url.to_string())
            .build();
        assert_eq!(client.config.base_url, custom_url);
    }

    #[test]
    fn test_builder_with_enable_token_cache() {
        let client_enabled = create_test_builder().with_enable_token_cache(true).build();
        assert!(client_enabled.config.enable_token_cache);

        let client_disabled = create_test_builder().with_enable_token_cache(false).build();
        assert!(!client_disabled.config.enable_token_cache);
    }

    #[test]
    fn test_builder_with_req_timeout() {
        let timeout_seconds = 30.0;
        let client = create_test_builder()
            .with_req_timeout(Some(timeout_seconds))
            .build();

        let expected_duration = Duration::from_secs_f32(timeout_seconds);
        assert_eq!(client.config.req_timeout, Some(expected_duration));
    }

    #[test]
    fn test_builder_with_none_timeout() {
        let client = create_test_builder().with_req_timeout(None).build();
        assert_eq!(client.config.req_timeout, None);
    }

    #[test]
    fn test_builder_chaining() {
        let client = create_test_builder()
            .with_app_type(AppType::Marketplace)
            .with_enable_token_cache(true)
            .with_req_timeout(Some(45.0))
            .with_open_base_url("https://test.api.feishu.cn".to_string())
            .build();

        assert_eq!(client.config.app_type, AppType::Marketplace);
        assert!(client.config.enable_token_cache);
        assert_eq!(
            client.config.req_timeout,
            Some(Duration::from_secs_f32(45.0))
        );
        assert_eq!(client.config.base_url, "https://test.api.feishu.cn");
    }

    #[test]
    fn test_client_build() {
        let client = create_test_builder().build();

        assert_eq!(client.config.app_id, "test_app_id");
        assert_eq!(client.config.app_secret, "test_app_secret");
        assert_eq!(client.config.app_type, AppType::SelfBuild);
    }

    #[test]
    fn test_client_build_with_timeout() {
        let client = create_test_builder().with_req_timeout(Some(60.0)).build();

        assert_eq!(
            client.config.req_timeout,
            Some(Duration::from_secs_f32(60.0))
        );
    }

    #[test]
    fn test_client_build_marketplace_app() {
        let client = create_test_builder().with_marketplace_app().build();

        assert_eq!(client.config.app_type, AppType::Marketplace);
    }

    #[test]
    fn test_client_build_with_custom_config() {
        let client = create_test_builder()
            .with_app_type(AppType::Marketplace)
            .with_enable_token_cache(false)
            .with_open_base_url("https://custom.feishu.cn".to_string())
            .with_req_timeout(Some(120.0))
            .build();

        assert_eq!(client.config.app_type, AppType::Marketplace);
        assert!(!client.config.enable_token_cache);
        assert_eq!(client.config.base_url, "https://custom.feishu.cn");
        assert_eq!(
            client.config.req_timeout,
            Some(Duration::from_secs_f32(120.0))
        );
    }

    #[test]
    fn test_builder_empty_credentials() {
        let builder = LarkClient::builder("", "");
        let config = builder.build_config();
        assert_eq!(config.app_id, "");
        assert_eq!(config.app_secret, "");
    }

    #[test]
    fn test_builder_unicode_credentials() {
        let app_id = "测试_app_id_🔑";
        let app_secret = "测试_secret_🔐";
        let builder = LarkClient::builder(app_id, app_secret);
        let config = builder.build_config();

        assert_eq!(config.app_id, app_id);
        assert_eq!(config.app_secret, app_secret);
    }

    #[test]
    fn test_builder_very_long_credentials() {
        let long_id = "a".repeat(1000);
        let long_secret = "b".repeat(1000);
        let builder = LarkClient::builder(&long_id, &long_secret);
        let config = builder.build_config();

        assert_eq!(config.app_id, long_id);
        assert_eq!(config.app_secret, long_secret);
    }

    #[test]
    fn test_builder_special_characters() {
        let special_id = "app-id_123!@#$%^&*()";
        let special_secret = "secret/\\?<>|:\"{}";
        let builder = LarkClient::builder(special_id, special_secret);
        let config = builder.build_config();

        assert_eq!(config.app_id, special_id);
        assert_eq!(config.app_secret, special_secret);
    }

    #[test]
    fn test_builder_extreme_timeout_values() {
        // Very small timeout
        let small_timeout = create_test_builder().with_req_timeout(Some(0.001)).build();
        assert_eq!(
            small_timeout.config.req_timeout,
            Some(Duration::from_secs_f32(0.001))
        );

        // Very large timeout
        let large_timeout = create_test_builder()
            .with_req_timeout(Some(3600.0)) // 1 hour
            .build();
        assert_eq!(
            large_timeout.config.req_timeout,
            Some(Duration::from_secs_f32(3600.0))
        );
    }

    #[test]
    fn test_config_independence() {
        // Test that multiple builders don't interfere with each other
        let builder1 = create_test_builder().with_app_type(AppType::Marketplace);

        let builder2 = create_test_builder().with_app_type(AppType::SelfBuild);

        assert_eq!(builder1.build_config().app_type, AppType::Marketplace);
        assert_eq!(builder2.build_config().app_type, AppType::SelfBuild);
    }

    #[test]
    fn test_builder_default_values() {
        let builder = create_test_builder();
        let config = builder.build_config();

        // Verify default values match Config defaults
        assert_eq!(config.app_type, AppType::SelfBuild);
        assert!(config.enable_token_cache); // Default from Config
        assert_eq!(config.req_timeout, None);
        assert!(!config.base_url.is_empty()); // Should have default URL
    }

    #[cfg(feature = "cloud-docs")]
    #[test]
    fn test_client_cloud_docs_services() {
        let client = create_test_builder().build();

        // Verify cloud docs services are available when feature is enabled
        // This is mainly to check that the struct is properly constructed
        // We can't test much without actual functionality, but we can verify the services exist
        let _assistant = &client.assistant;
        let _drive = &client.drive;
        let _sheets = &client.sheets;
        let _bitable = &client.bitable;
        let _wiki = &client.wiki;
        let _docs = &client.docs;
    }

    #[test]
    fn test_client_builder_multiple_configurations() {
        // Test that the builder can be used to create multiple different clients
        let client1 = create_test_builder().with_marketplace_app().build();

        let client2 = LarkClient::builder("different_id", "different_secret")
            .with_enable_token_cache(false)
            .build();

        assert_eq!(client1.config.app_type, AppType::Marketplace);
        assert_eq!(client1.config.app_id, "test_app_id");

        assert_eq!(client2.config.app_type, AppType::SelfBuild);
        assert_eq!(client2.config.app_id, "different_id");
        assert!(!client2.config.enable_token_cache);
    }
}