openlark_client/client/
builder.rs1use super::Client;
2#[allow(deprecated)]
3use crate::Config;
4use crate::{Result, client_build_config::ClientBuildConfig};
5use openlark_core::error::ErrorTrait;
6
7#[derive(Debug, Clone)]
28pub struct ClientBuilder {
29 pub(super) config: ClientBuildConfig,
30}
31
32impl ClientBuilder {
33 pub fn new() -> Self {
35 Self {
36 config: ClientBuildConfig::default(),
37 }
38 }
39
40 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 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 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 pub fn enable_token_cache(mut self, enable: bool) -> Self {
60 self.config.enable_token_cache(enable);
61 self
62 }
63
64 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 pub fn allow_custom_base_url(mut self, allow: bool) -> Self {
72 self.config.allow_custom_base_url(allow);
73 self
74 }
75
76 pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
78 self.config.timeout(timeout);
79 self
80 }
81
82 pub fn retry_count(mut self, retry_count: u32) -> Self {
84 self.config.retry_count(retry_count);
85 self
86 }
87
88 pub fn enable_log(mut self, enable: bool) -> Self {
90 self.config.enable_log(enable);
91 self
92 }
93
94 pub fn max_response_size(mut self, size: u64) -> Self {
96 self.config.max_response_size(size);
97 self
98 }
99
100 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 pub fn from_env(mut self) -> Self {
112 self.config.load_from_env();
113 self
114 }
115
116 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#[allow(deprecated)]
148impl From<Config> for Result<Client> {
149 fn from(config: Config) -> Self {
150 Client::with_config(config)
151 }
152}