openlark_client/client/
builder.rs1use super::Client;
2use crate::Result;
3use openlark_core::config::{Config, ConfigBuilder};
4use openlark_core::error::ErrorTrait;
5use std::fmt;
6use std::time::Duration;
7
8#[derive(Clone)]
30pub struct ClientBuilder {
31 config: ConfigBuilder,
32}
33
34impl fmt::Debug for ClientBuilder {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 f.debug_struct("ClientBuilder")
38 .field("config", &self.config)
39 .finish()
40 }
41}
42
43impl ClientBuilder {
44 pub fn new() -> Self {
48 Self {
49 config: Config::builder().req_timeout(Duration::from_secs(30)),
50 }
51 }
52
53 pub fn app_id<S: Into<String>>(self, app_id: S) -> Self {
55 Self {
56 config: self.config.app_id(app_id),
57 }
58 }
59
60 pub fn app_secret<S: Into<String>>(self, app_secret: S) -> Self {
62 Self {
63 config: self.config.app_secret(app_secret),
64 }
65 }
66
67 pub fn app_type(self, app_type: openlark_core::constants::AppType) -> Self {
69 Self {
70 config: self.config.app_type(app_type),
71 }
72 }
73
74 pub fn enable_token_cache(self, enable: bool) -> Self {
76 Self {
77 config: self.config.enable_token_cache(enable),
78 }
79 }
80
81 pub fn base_url<S: Into<String>>(self, base_url: S) -> Self {
83 Self {
84 config: self.config.base_url(base_url),
85 }
86 }
87
88 pub fn allow_custom_base_url(self, allow: bool) -> Self {
90 Self {
91 config: self.config.allow_custom_base_url(allow),
92 }
93 }
94
95 pub fn timeout(self, timeout: Duration) -> Self {
97 Self {
98 config: self.config.req_timeout(timeout),
99 }
100 }
101
102 pub fn retry_count(self, retry_count: u32) -> Self {
104 Self {
105 config: self.config.retry_count(retry_count),
106 }
107 }
108
109 pub fn enable_log(self, enable: bool) -> Self {
111 Self {
112 config: self.config.enable_log(enable),
113 }
114 }
115
116 pub fn max_response_size(self, size: u64) -> Self {
118 Self {
119 config: self.config.max_response_size(size),
120 }
121 }
122
123 pub fn add_header<K, V>(self, key: K, value: V) -> Self
125 where
126 K: Into<String>,
127 V: Into<String>,
128 {
129 Self {
130 config: self.config.add_header(key, value),
131 }
132 }
133
134 pub fn from_env(self) -> Self {
138 Self {
139 config: self.config.load_from_env(),
140 }
141 }
142
143 pub fn build(self) -> Result<Client> {
153 let result = Client::with_checked_core_config(self.config.build(), "ClientBuilder::build");
154 if let Err(ref error) = result {
155 tracing::error!(
156 "客户端构建失败: {}",
157 error.user_message().unwrap_or("未知错误")
158 );
159 }
160 result
161 }
162}
163
164impl Default for ClientBuilder {
165 fn default() -> Self {
166 Self::new()
167 }
168}