use super::Client;
use crate::Result;
use openlark_core::config::{Config, ConfigBuilder};
use openlark_core::error::ErrorTrait;
use std::fmt;
use std::time::Duration;
#[derive(Clone)]
pub struct ClientBuilder {
config: ConfigBuilder,
}
impl fmt::Debug for ClientBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClientBuilder")
.field("config", &self.config)
.finish()
}
}
impl ClientBuilder {
pub fn new() -> Self {
Self {
config: Config::builder().req_timeout(Duration::from_secs(30)),
}
}
pub fn app_id<S: Into<String>>(self, app_id: S) -> Self {
Self {
config: self.config.app_id(app_id),
}
}
pub fn app_secret<S: Into<String>>(self, app_secret: S) -> Self {
Self {
config: self.config.app_secret(app_secret),
}
}
pub fn app_type(self, app_type: openlark_core::constants::AppType) -> Self {
Self {
config: self.config.app_type(app_type),
}
}
pub fn enable_token_cache(self, enable: bool) -> Self {
Self {
config: self.config.enable_token_cache(enable),
}
}
pub fn base_url<S: Into<String>>(self, base_url: S) -> Self {
Self {
config: self.config.base_url(base_url),
}
}
pub fn allow_custom_base_url(self, allow: bool) -> Self {
Self {
config: self.config.allow_custom_base_url(allow),
}
}
pub fn timeout(self, timeout: Duration) -> Self {
Self {
config: self.config.req_timeout(timeout),
}
}
pub fn retry_count(self, retry_count: u32) -> Self {
Self {
config: self.config.retry_count(retry_count),
}
}
pub fn enable_log(self, enable: bool) -> Self {
Self {
config: self.config.enable_log(enable),
}
}
pub fn max_response_size(self, size: u64) -> Self {
Self {
config: self.config.max_response_size(size),
}
}
pub fn add_header<K, V>(self, key: K, value: V) -> Self
where
K: Into<String>,
V: Into<String>,
{
Self {
config: self.config.add_header(key, value),
}
}
pub fn from_env(self) -> Self {
Self {
config: self.config.load_from_env(),
}
}
pub fn build(self) -> Result<Client> {
let result = Client::with_checked_core_config(self.config.build(), "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()
}
}