use super::Client;
#[allow(deprecated)]
use crate::Config;
use crate::{Result, client_build_config::ClientBuildConfig};
use openlark_core::error::ErrorTrait;
#[derive(Debug, Clone)]
pub struct ClientBuilder {
pub(super) config: ClientBuildConfig,
}
impl ClientBuilder {
pub fn new() -> Self {
Self {
config: ClientBuildConfig::default(),
}
}
pub fn app_id<S: Into<String>>(mut self, app_id: S) -> Self {
self.config.app_id(app_id);
self
}
pub fn app_secret<S: Into<String>>(mut self, app_secret: S) -> Self {
self.config.app_secret(app_secret);
self
}
pub fn app_type(mut self, app_type: openlark_core::constants::AppType) -> Self {
self.config.app_type(app_type);
self
}
pub fn enable_token_cache(mut self, enable: bool) -> Self {
self.config.enable_token_cache(enable);
self
}
pub fn base_url<S: Into<String>>(mut self, base_url: S) -> Self {
self.config.base_url(base_url);
self
}
pub fn allow_custom_base_url(mut self, allow: bool) -> Self {
self.config.allow_custom_base_url(allow);
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 max_response_size(mut self, size: u64) -> Self {
self.config.max_response_size(size);
self
}
pub fn add_header<K, V>(mut self, key: K, value: V) -> Self
where
K: Into<String>,
V: Into<String>,
{
self.config.add_header(key, value);
self
}
pub fn from_env(mut self) -> Self {
self.config.load_from_env();
self
}
pub fn build(self) -> Result<Client> {
let result = self.config.validate().and_then(|()| {
Client::with_validated_core_config(
self.config.build_core_config(),
"ClientBuilder::build",
)
});
if let Err(ref error) = result {
tracing::error!(
"客户端构建失败: {}",
error.user_message().unwrap_or("未知错误")
);
}
result
}
}
impl Default for ClientBuilder {
fn default() -> Self {
Self::new()
}
}
#[allow(deprecated)]
impl From<Config> for Result<Client> {
fn from(config: Config) -> Self {
Client::with_config(config)
}
}