Skip to main content

Config

Struct Config 

Source
pub struct Config {
    pub app_id: String,
    pub app_secret: String,
    pub app_type: AppType,
    pub enable_token_cache: bool,
    pub base_url: String,
    pub timeout: Duration,
    pub retry_count: u32,
    pub enable_log: bool,
    pub headers: HashMap<String, String>,
    /* private fields */
}
Expand description

🔧 OpenLark客户端配置

支持从环境变量自动加载配置

§环境变量

  • OPENLARK_APP_ID: 应用ID(必需)
  • OPENLARK_APP_SECRET: 应用密钥(必需)
  • OPENLARK_APP_TYPE: 应用类型(可选:self_build / marketplace,默认 self_build)
  • OPENLARK_BASE_URL: API基础URL(可选,默认:https://open.feishu.cn,国际版 Lark 使用 https://open.larksuite.com
  • OPENLARK_ENABLE_TOKEN_CACHE: 是否允许自动获取 token(可选,默认 true)

§示例

use openlark_client::Config;

// 从环境变量创建配置
let config = Config::from_env();

// 手动构建配置
let config = Config::builder()
    .app_id("your_app_id")
    .app_secret("your_app_secret")
    .base_url("https://open.feishu.cn")  // 默认值,国际版 Lark 使用 https://open.larksuite.com
    .build();

Fields§

§app_id: String

🆔 飞书应用ID

§app_secret: String

🔑 飞书应用密钥

§app_type: AppType

🏷️ 应用类型(自建 / 商店)

§enable_token_cache: bool

🔐 是否允许 SDK 自动获取 token(通过 openlark-core 的 TokenProvider)

§base_url: String

🌐 API基础URL

§timeout: Duration

⏱️ 请求超时时间

§retry_count: u32

🔄 默认重试次数

§enable_log: bool

📝 是否启用日志记录

§headers: HashMap<String, String>

📋 自定义HTTP headers

Implementations§

Source§

impl Config

Source

pub fn new() -> Config

🆕 创建新的配置实例

Source

pub fn from_env() -> Config

🌍 从环境变量创建配置

§环境变量
  • OPENLARK_APP_ID: 应用ID(必需)
  • OPENLARK_APP_SECRET: 应用密钥(必需)
  • OPENLARK_APP_TYPE: 应用类型(可选:self_build / marketplace)
  • OPENLARK_BASE_URL: API基础URL(可选,默认 https://open.feishu.cn,国际版 Lark 使用 https://open.larksuite.com
  • OPENLARK_ENABLE_TOKEN_CACHE: 是否允许自动获取 token(可选)
§返回值

返回配置实例,环境变量缺失时使用默认值

Source

pub fn load_from_env(&mut self)

📥 从环境变量加载配置到当前实例

只设置存在且非空的环境变量

Source

pub fn validate(&self) -> Result<(), CoreError>

✅ 验证配置的有效性

§验证规则
  • app_id和app_secret不能为空
  • base_url必须是有效的HTTP/HTTPS URL
  • timeout必须大于0
  • retry_count不能超过合理的范围
§错误

返回验证失败的详细错误信息

Source

pub fn builder() -> ConfigBuilder

🏗️ 创建配置构建器

Source

pub fn add_header<K, V>(&mut self, key: K, value: V)
where K: Into<String>, V: Into<String>,

🔧 添加自定义HTTP header

Source

pub fn clear_headers(&mut self)

🧹 清除所有自定义headers

Source

pub fn is_complete(&self) -> bool

🔍 检查配置是否完整(可用于API调用)

Source

pub fn summary(&self) -> ConfigSummary

📋 获取配置摘要(不包含敏感信息)

Source

pub fn update_with(&mut self, other: &Config)

🔄 更新配置,只更新非空字段

Source

pub fn build_core_config(&self) -> Config

🔧 构建底层 core 配置(不含 TokenProvider)

Source

pub fn build_core_config_with_token_provider(&self) -> Config

🔧 构建带有默认 TokenProvider 的 core 配置

Source

pub fn get_or_build_core_config(&self) -> Config

🔧 获取或构建 core 配置

Source

pub fn get_or_build_core_config_with_token_provider(&self) -> Config

🔧 获取或构建带有 TokenProvider 的 core 配置

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Config

Returns the “default value” for a type. Read more
Source§

impl From<Config> for Result<Client, CoreError>

Client的便利构造函数

Source§

fn from(config: Config) -> Result<Client, CoreError>

Converts to this type from the input type.
Source§

impl From<Vars> for Config

从环境变量创建配置的便利方法

Source§

fn from(env_vars: Vars) -> Config

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more