openlark-client 0.15.0

OpenLark 高级客户端 - 统一入口点和轻量级服务注册表
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
//! OpenLark Client - 全新简化架构
//!
//! 极简设计:仅保留 meta 链式字段访问(单入口,KISS)

use crate::{
    error::{with_context, with_operation_context},
    traits::LarkClient,
    Config, DefaultServiceRegistry, Result,
};
use openlark_core::error::ErrorTrait;
use std::sync::Arc;

/// 🔐 认证 meta 入口:`client.auth.app / client.auth.user / client.auth.oauth`
#[cfg(feature = "auth")]
#[derive(Debug, Clone)]
pub struct AuthClient {
    /// 应用认证服务
    pub app: openlark_auth::AuthService,
    /// 用户身份认证服务
    pub user: openlark_auth::AuthenService,
    /// OAuth 授权服务
    pub oauth: openlark_auth::OAuthService,
}

#[cfg(feature = "auth")]
impl AuthClient {
    fn new(config: openlark_core::config::Config) -> Self {
        Self {
            app: openlark_auth::AuthService::new(config.clone()),
            user: openlark_auth::AuthenService::new(config.clone()),
            oauth: openlark_auth::OAuthService::new(config),
        }
    }
}

/// 🚀 OpenLark客户端 - 极简设计
///
/// # 特性
/// - 零配置启动:`Client::from_env()`
/// - 单入口:meta 链式字段访问(`client.docs/...`)
/// - 编译时feature优化
/// - 高性能异步
/// - 现代化错误处理
///
/// # 示例
/// ```rust,no_run
/// use openlark_client::prelude::*;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     // 从环境变量创建客户端
///     let client = Client::from_env()?;
///
///     // meta 链式入口(需要对应 feature)
///     // - 通讯:client.communication.im...
///     // - 文档:client.docs.ccm...
///     // - 认证:client.auth.app / client.auth.user / client.auth.oauth
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone)]
pub struct Client {
    /// 客户端配置
    config: Arc<Config>,
    /// 服务注册表
    registry: Arc<DefaultServiceRegistry>,
    /// 底层 core 配置(供各 meta client 复用)
    core_config: openlark_core::config::Config,

    /// CardKit meta 调用链:client.cardkit.v1.card.create(...)
    #[cfg(feature = "cardkit")]
    pub cardkit: openlark_cardkit::CardkitClient,

    /// Auth meta 调用链入口:client.auth.app / client.auth.user / client.auth.oauth
    #[cfg(feature = "auth")]
    pub auth: AuthClient,

    /// Docs meta 调用链入口:client.docs.ccm / client.docs.base ...
    #[cfg(feature = "docs")]
    pub docs: openlark_docs::DocsClient,

    /// Communication meta 调用链入口:client.communication.im / client.communication.contact ...
    #[cfg(feature = "communication")]
    pub communication: openlark_communication::CommunicationClient,

    /// HR meta 调用链入口:client.hr.attendance / client.hr.corehr / client.hr.hire ...
    #[cfg(feature = "hr")]
    pub hr: openlark_hr::HrClient,

    /// Meeting meta 调用链入口:client.meeting.vc.v1.room.create() ...
    #[cfg(feature = "meeting")]
    pub meeting: openlark_meeting::MeetingClient,
}

impl Client {
    /// 🔥 从环境变量创建客户端
    ///
    /// # 环境变量
    /// ```bash
    /// export OPENLARK_APP_ID=your_app_id
    /// export OPENLARK_APP_SECRET=your_app_secret
    /// export OPENLARK_BASE_URL=https://open.feishu.cn  # 可选
    /// ```
    ///
    /// # 返回值
    /// 返回配置好的客户端实例或错误
    ///
    /// # 示例
    /// ```rust,no_run
    /// use openlark_client::Client;
    ///
    /// let _client = Client::from_env();
    /// ```
    pub fn from_env() -> Result<Self> {
        Self::builder().from_env().build()
    }

    /// 🏗️ 创建构建器
    pub fn builder() -> ClientBuilder {
        ClientBuilder::new()
    }

    /// 🔧 获取客户端配置
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// 📋 获取服务注册表
    pub fn registry(&self) -> &DefaultServiceRegistry {
        &self.registry
    }

    /// 🔧 获取底层 core 配置(高级用法/调试用)
    pub fn core_config(&self) -> &openlark_core::config::Config {
        &self.core_config
    }

    /// 🔧 获取可直接传给函数式 API 的认证后配置
    ///
    /// 与 [`Self::core_config`] 返回同一份配置,保留这个别名是为了让
    /// 业务侧更容易理解它的用途:可直接传给 `openlark_docs::*`、
    /// `openlark_auth::*` 等函数式 API。
    pub fn api_config(&self) -> &openlark_core::config::Config {
        &self.core_config
    }

    /// ✅ 检查客户端是否已正确配置
    pub fn is_configured(&self) -> bool {
        !self.config.app_id.is_empty() && !self.config.app_secret.is_empty()
    }

    /// 🆕 创建带有自定义配置的客户端
    pub fn with_config(config: Config) -> Result<Self> {
        let validation_result = config.validate();
        if let Err(err) = validation_result {
            return with_context(Err(err), "operation", "Client::with_config");
        }

        let config = Arc::new(config);
        let mut registry = DefaultServiceRegistry::new();

        // 加载启用的服务
        if let Err(err) = crate::registry::bootstrap::register_compiled_services(&mut registry) {
            return with_operation_context(Err(err), "Client::with_config", "service_loading");
        }

        let registry = Arc::new(registry);

        // 从 client Config 获取 core Config
        #[cfg(feature = "auth")]
        let base_core_config = config.as_ref().build_core_config();
        #[cfg(feature = "auth")]
        let core_config = config
            .as_ref()
            .get_or_build_core_config_with_token_provider();
        #[cfg(not(feature = "auth"))]
        let core_config = config.as_ref().get_or_build_core_config();

        #[cfg(feature = "cardkit")]
        let cardkit = openlark_cardkit::CardkitClient::new(core_config.clone());

        #[cfg(feature = "auth")]
        let auth = AuthClient::new(base_core_config.clone());

        #[cfg(feature = "docs")]
        let docs = openlark_docs::DocsClient::new(core_config.clone());

        #[cfg(feature = "communication")]
        let communication = openlark_communication::CommunicationClient::new(core_config.clone());

        #[cfg(feature = "hr")]
        let hr = openlark_hr::HrClient::new(core_config.clone());

        #[cfg(feature = "meeting")]
        let meeting = openlark_meeting::MeetingClient::new(core_config.clone());

        Ok(Client {
            config,
            registry,
            core_config: core_config.clone(),
            #[cfg(feature = "cardkit")]
            cardkit,
            #[cfg(feature = "auth")]
            auth,
            #[cfg(feature = "docs")]
            docs,
            #[cfg(feature = "communication")]
            communication,
            #[cfg(feature = "hr")]
            hr,
            #[cfg(feature = "meeting")]
            meeting,
        })
    }

    /// 🔧 执行带有错误上下文的操作
    pub async fn execute_with_context<F, T>(&self, operation: &str, f: F) -> Result<T>
    where
        F: std::future::Future<Output = Result<T>>,
    {
        let result = f.await;
        with_operation_context(result, operation, "Client")
    }
}

// 实现LarkClient trait
impl LarkClient for Client {
    fn config(&self) -> &Config {
        &self.config
    }

    fn is_configured(&self) -> bool {
        self.is_configured()
    }
}

/// 🏗️ 客户端构建器 - 流畅API
///
/// 提供链式调用的客户端构建方式
///
/// # 示例
/// ```rust,no_run
/// use openlark_client::Client;
/// use openlark_client::Result;
/// use std::time::Duration;
///
/// fn main() -> Result<()> {
///     let _client = Client::builder()
///         .app_id("your_app_id")
///         .app_secret("your_app_secret")
///         .base_url("https://open.feishu.cn")
///         .timeout(Duration::from_secs(30))
///         .build()?;
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone)]
pub struct ClientBuilder {
    config: Config,
}

impl ClientBuilder {
    /// 🆕 创建新的构建器实例
    pub fn new() -> Self {
        Self {
            config: Config::default(),
        }
    }

    /// 🆔 设置应用ID
    pub fn app_id<S: Into<String>>(mut self, app_id: S) -> Self {
        self.config.app_id = app_id.into();
        self
    }

    /// 🔑 设置应用密钥
    pub fn app_secret<S: Into<String>>(mut self, app_secret: S) -> Self {
        self.config.app_secret = app_secret.into();
        self
    }

    /// 🏷️ 设置应用类型(自建 / 商店)
    pub fn app_type(mut self, app_type: openlark_core::constants::AppType) -> Self {
        self.config.app_type = app_type;
        self
    }

    /// 🔐 设置是否允许自动获取 token(默认 true)
    pub fn enable_token_cache(mut self, enable: bool) -> Self {
        self.config.enable_token_cache = enable;
        self
    }

    /// 🌐 设置基础URL
    pub fn base_url<S: Into<String>>(mut self, base_url: S) -> Self {
        self.config.base_url = base_url.into();
        self
    }

    /// ⏱️ 设置请求超时时间
    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
        self.config.timeout = timeout;
        self
    }

    /// 🔄 设置重试次数
    pub fn retry_count(mut self, retry_count: u32) -> Self {
        self.config.retry_count = retry_count;
        self
    }

    /// 📝 启用或禁用日志
    pub fn enable_log(mut self, enable: bool) -> Self {
        self.config.enable_log = enable;
        self
    }

    /// 🌍 从环境变量加载配置
    pub fn from_env(mut self) -> Self {
        self.config.load_from_env();
        self
    }

    /// 🔨 构建客户端实例
    ///
    /// # 返回值
    /// 返回配置好的客户端实例或验证错误
    ///
    /// # 错误
    /// 如果配置验证失败,会返回相应的错误信息,包含用户友好的恢复建议
    pub fn build(self) -> Result<Client> {
        let result = Client::with_config(self.config);
        if let Err(ref error) = result {
            tracing::error!(
                "客户端构建失败: {}",
                error.user_message().unwrap_or("未知错误")
            );
        }
        result
    }
}

impl Default for ClientBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Client的便利构造函数
impl From<Config> for Result<Client> {
    fn from(config: Config) -> Self {
        Client::with_config(config)
    }
}

/// 客户端错误处理扩展特征
pub trait ClientErrorHandling {
    /// 处理错误并添加客户端上下文
    fn handle_error<T>(&self, result: Result<T>, operation: &str) -> Result<T>;
    /// 处理异步错误并添加客户端上下文
    async fn handle_async_error<T, F>(&self, f: F, operation: &str) -> Result<T>
    where
        F: std::future::Future<Output = Result<T>>;
}

impl ClientErrorHandling for Client {
    fn handle_error<T>(&self, result: Result<T>, operation: &str) -> Result<T> {
        with_operation_context(result, operation, "Client")
    }

    async fn handle_async_error<T, F>(&self, f: F, operation: &str) -> Result<T>
    where
        F: std::future::Future<Output = Result<T>>,
    {
        let result = f.await;
        with_operation_context(result, operation, "Client")
    }
}

#[cfg(test)]
#[allow(unused_imports)]
mod tests {
    use super::*;
    use openlark_core::error::ErrorTrait;
    use std::time::Duration;

    #[test]
    fn test_client_builder() {
        let client = Client::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret")
            .timeout(Duration::from_secs(30))
            .build();

        assert!(client.is_ok());
    }

    #[test]
    fn test_client_config() {
        let config = Config {
            app_id: "test_app_id".to_string(),
            app_secret: "test_app_secret".to_string(),
            base_url: "https://open.feishu.cn".to_string(),
            ..Default::default()
        };

        let client = Client::with_config(config).unwrap();
        assert_eq!(client.config().app_id, "test_app_id");
        assert_eq!(client.config().app_secret, "test_app_secret");
        assert!(client.is_configured());
    }

    #[test]
    fn test_client_not_configured() {
        let config = Config {
            app_id: String::new(),
            app_secret: String::new(),
            ..Default::default()
        };

        let client_result = Client::with_config(config);
        assert!(client_result.is_err());

        if let Err(error) = client_result {
            assert!(error.is_config_error() || error.is_validation_error());
            assert!(!error.user_message().unwrap_or("未知错误").is_empty());
        }
    }

    #[test]
    fn test_client_clone() {
        let client = Client::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret")
            .build()
            .unwrap();

        let cloned_client = client.clone();
        assert_eq!(client.config().app_id, cloned_client.config().app_id);
    }

    #[cfg(feature = "cardkit")]
    #[test]
    fn test_cardkit_chain_exists() {
        let client = Client::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret")
            .build()
            .unwrap();

        let _ = &client.cardkit.v1.card;
    }

    #[cfg(feature = "docs")]
    #[test]
    fn test_docs_chain_exists() {
        let client = Client::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret")
            .build()
            .unwrap();

        let _ = client.docs.config();
    }

    #[cfg(feature = "communication")]
    #[test]
    fn test_communication_chain_exists() {
        let client = Client::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret")
            .build()
            .unwrap();

        let _ = client.communication.config();
    }

    #[cfg(feature = "meeting")]
    #[test]
    fn test_meeting_chain_exists() {
        let client = Client::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret")
            .build()
            .unwrap();

        let _ = client.meeting.config();
    }

    #[test]
    fn test_client_error_handling() {
        let client = Client::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret")
            .build()
            .unwrap();

        // 测试错误上下文处理
        let error_result: Result<i32> =
            Err(crate::error::validation_error("field", "validation failed"));
        let result = client.handle_error(error_result, "test_operation");

        assert!(result.is_err());
        if let Err(error) = result {
            assert!(error.context().has_context("operation"));
            assert_eq!(
                error.context().get_context("operation"),
                Some("test_operation")
            );
            assert_eq!(error.context().get_context("component"), Some("Client"));
        }
    }

    #[tokio::test]
    async fn test_async_error_handling() {
        let client = Client::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret")
            .build()
            .unwrap();

        // 测试异步错误上下文处理
        let result = client
            .handle_async_error(
                async { Err::<i32, _>(crate::error::network_error("async error")) },
                "async_test",
            )
            .await;

        assert!(result.is_err());
        if let Err(error) = result {
            assert!(error.context().has_context("operation"));
            assert_eq!(error.context().get_context("operation"), Some("async_test"));
            assert_eq!(error.context().get_context("component"), Some("Client"));
        }
    }

    #[test]
    fn test_from_env_missing_vars() {
        // 验证默认构建器未配置 app_id/app_secret 时会失败(不依赖环境变量)。
        let builder = ClientBuilder::default();
        let result = builder.build();
        assert!(result.is_err()); // 没有app_id和app_secret应该失败
    }

    #[test]
    fn test_from_app_id_string() {
        crate::test_utils::with_env_vars(
            &[
                ("OPENLARK_APP_ID", Some("test_app_id")),
                ("OPENLARK_APP_SECRET", Some("test_secret")),
            ],
            || {
                let result: Result<Client> = Client::from_env();
                assert!(result.is_ok());

                if let Ok(client) = result {
                    assert_eq!(client.config().app_id, "test_app_id");
                    assert_eq!(client.config().app_secret, "test_secret");
                }
            },
        );
    }

    #[test]
    fn test_builder_default() {
        let builder = ClientBuilder::default();
        assert!(builder.config.app_id.is_empty());
        assert!(builder.config.app_secret.is_empty());
    }

    #[cfg(feature = "communication")]
    #[test]
    fn test_communication_service_access() {
        let client = Client::builder()
            .app_id("test_app_id")
            .app_secret("test_app_secret")
            .build()
            .unwrap();

        // 单入口:meta 链式字段访问(这里只验证字段可用)
        let _comm = &client.communication;
    }
}