Skip to main content

openlark_client/client/
builder.rs

1use super::Client;
2#[allow(deprecated)]
3use crate::Config;
4use crate::{Result, client_build_config::ClientBuildConfig};
5use openlark_core::error::ErrorTrait;
6
7/// 🏗️ 客户端构建器 - 流畅API
8///
9/// 提供链式调用的客户端构建方式
10///
11/// # 示例
12/// ```rust,no_run
13/// use openlark_client::Client;
14/// use openlark_client::Result;
15/// use std::time::Duration;
16///
17/// fn main() -> Result<()> {
18///     let _client = Client::builder()
19///         .app_id("your_app_id")
20///         .app_secret("your_app_secret")
21///         .base_url("https://open.feishu.cn")
22///         .timeout(Duration::from_secs(30))
23///         .build()?;
24///     Ok(())
25/// }
26/// ```
27#[derive(Debug, Clone)]
28pub struct ClientBuilder {
29    pub(super) config: ClientBuildConfig,
30}
31
32impl ClientBuilder {
33    /// 🆕 创建新的构建器实例
34    pub fn new() -> Self {
35        Self {
36            config: ClientBuildConfig::default(),
37        }
38    }
39
40    /// 🆔 设置应用ID
41    pub fn app_id<S: Into<String>>(mut self, app_id: S) -> Self {
42        self.config.app_id(app_id);
43        self
44    }
45
46    /// 🔑 设置应用密钥
47    pub fn app_secret<S: Into<String>>(mut self, app_secret: S) -> Self {
48        self.config.app_secret(app_secret);
49        self
50    }
51
52    /// 🏷️ 设置应用类型(自建 / 商店)
53    pub fn app_type(mut self, app_type: openlark_core::constants::AppType) -> Self {
54        self.config.app_type(app_type);
55        self
56    }
57
58    /// 🔐 设置是否允许自动获取 token(默认 true)
59    pub fn enable_token_cache(mut self, enable: bool) -> Self {
60        self.config.enable_token_cache(enable);
61        self
62    }
63
64    /// 🌐 设置基础URL
65    pub fn base_url<S: Into<String>>(mut self, base_url: S) -> Self {
66        self.config.base_url(base_url);
67        self
68    }
69
70    /// 🔓 允许自定义 base_url 域名
71    pub fn allow_custom_base_url(mut self, allow: bool) -> Self {
72        self.config.allow_custom_base_url(allow);
73        self
74    }
75
76    /// ⏱️ 设置请求超时时间
77    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
78        self.config.timeout(timeout);
79        self
80    }
81
82    /// 🔄 设置重试次数
83    pub fn retry_count(mut self, retry_count: u32) -> Self {
84        self.config.retry_count(retry_count);
85        self
86    }
87
88    /// 📝 启用或禁用日志
89    pub fn enable_log(mut self, enable: bool) -> Self {
90        self.config.enable_log(enable);
91        self
92    }
93
94    /// 设置响应体最大大小限制(字节)
95    pub fn max_response_size(mut self, size: u64) -> Self {
96        self.config.max_response_size(size);
97        self
98    }
99
100    /// 🔧 添加自定义 HTTP header
101    pub fn add_header<K, V>(mut self, key: K, value: V) -> Self
102    where
103        K: Into<String>,
104        V: Into<String>,
105    {
106        self.config.add_header(key, value);
107        self
108    }
109
110    /// 🌍 从环境变量加载配置
111    pub fn from_env(mut self) -> Self {
112        self.config.load_from_env();
113        self
114    }
115
116    /// 🔨 构建客户端实例
117    ///
118    /// # 返回值
119    /// 返回配置好的客户端实例或验证错误
120    ///
121    /// # 错误
122    /// 如果配置验证失败,会返回相应的错误信息,包含用户友好的恢复建议
123    pub fn build(self) -> Result<Client> {
124        let result = self.config.validate().and_then(|()| {
125            Client::with_validated_core_config(
126                self.config.build_core_config(),
127                "ClientBuilder::build",
128            )
129        });
130        if let Err(ref error) = result {
131            tracing::error!(
132                "客户端构建失败: {}",
133                error.user_message().unwrap_or("未知错误")
134            );
135        }
136        result
137    }
138}
139
140impl Default for ClientBuilder {
141    fn default() -> Self {
142        Self::new()
143    }
144}
145
146/// Client的便利构造函数
147#[allow(deprecated)]
148impl From<Config> for Result<Client> {
149    fn from(config: Config) -> Self {
150        Client::with_config(config)
151    }
152}