Expand description
🚀 OpenLark Client Library
现代化的飞书开放平台 Rust SDK,提供简洁、类型安全的 API 访问 集成 CoreError 企业级错误处理系统,提供全面的错误管理和恢复建议
普通用户请优先使用根 crate
openlark。
openlark-client保留为高级入口:适合只想复用统一客户端层,或明确需要直接控制客户端 feature 组合的场景。
§核心特性
- 🎯 Feature-driven: 基于编译时功能标志的模块化设计
- ⚡ 零配置: 支持从环境变量自动配置客户端
- 🔒 类型安全: 完全编译时验证的 API 调用
- 🚀 异步优先: 完全异步的客户端实现
- 🏗️ 现代构建器: 流畅的构建器模式 API
- 🔍 服务发现: 动态服务注册和管理
- 🛡️ 企业级: 基于 CoreError 的高级错误处理、重试和监控支持
- 🌐 中文优先: 100% 中文错误消息和文档,专为中国开发者优化
§快速开始
§基础用法
use openlark_client::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
// 从环境变量创建客户端(推荐)
let client = Client::from_env()?;
// 单入口:meta 链式字段访问(需要对应 feature)
// - 通讯:client.communication.im...
// - 文档:client.docs.ccm...
// - 认证:client.auth.app / client.auth.user / client.auth.oauth
Ok(())
}§构建器模式
use openlark_client::prelude::*;
use std::time::Duration;
fn main() -> Result<()> {
let _client = Client::builder()
.app_id("your_app_id")
.app_secret("your_app_secret")
.base_url("https://open.feishu.cn")
.timeout(Duration::from_secs(30))
.enable_log(true)
.build()?;
Ok(())
}§Endpoint 切换
OpenLark 默认使用国内飞书 endpoint:https://open.feishu.cn。
如果你的应用运行在国际版 Lark,请将 base_url 切换为 https://open.larksuite.com。
use openlark_client::prelude::*;
fn main() -> Result<()> {
let _client = Client::builder()
.app_id("your_app_id")
.app_secret("your_app_secret")
.base_url("https://open.larksuite.com")
.build()?;
Ok(())
}§环境变量配置
设置以下环境变量:
export OPENLARK_APP_ID="your_app_id"
export OPENLARK_APP_SECRET="your_app_secret"
export OPENLARK_BASE_URL="https://open.feishu.cn" # 可选,国际版请改为 https://open.larksuite.com
export OPENLARK_TIMEOUT="30" # 可选,秒
export OPENLARK_ENABLE_LOG="true" # 可选§功能标志
客户端使用 Rust 功能标志进行模块化编译:
[dependencies]
openlark-client = { version = "0.1", features = [
"communication", # 通讯服务
"hr", # 人力资源服务
"docs", # 文档服务
"ai", # AI 服务
"auth", # 认证服务
"websocket", # WebSocket 支持
]}§服务访问
每个启用功能都提供对应的 meta 链式入口(字段访问):
use openlark_client::prelude::*;
fn main() -> Result<()> {
let client = Client::from_env()?;
// 通讯入口(communication feature)
#[cfg(feature = "communication")]
let _comm = &client.communication;
// 文档入口(docs feature)
#[cfg(feature = "docs")]
let _docs = &client.docs;
// 认证入口(auth feature)
#[cfg(feature = "auth")]
let _auth = &client.auth;
Ok(())
}§高级用法
§服务注册和管理
use openlark_client::prelude::*;
fn main() -> Result<()> {
let client = Client::from_env()?;
let registry = client.registry();
// 检查可用服务
println!("可用服务: {:?}", registry.list_services());
// 检查特定服务是否可用
if registry.has_service("communication") {
println!("通讯服务可用");
}
Ok(())
}§自定义配置
use openlark_client::prelude::*;
use std::time::Duration;
fn main() -> Result<()> {
let _client = Client::builder()
.app_id("app_id")
.app_secret("app_secret")
.base_url("https://open.feishu.cn")
.timeout(Duration::from_secs(60))
.retry_count(3)
.enable_log(true)
.build()?;
Ok(())
}§错误处理
客户端基于 CoreError 提供企业级错误处理,包含详细的错误分析、恢复建议和中文友好的错误消息:
use openlark_client::prelude::*;
match Client::from_env() {
Ok(client) => {
println!("客户端创建成功");
// 使用客户端...
},
Err(error) => {
// 用户友好的错误消息(中文)
eprintln!("❌ {}", error.user_message().unwrap_or("未知错误"));
// 获取错误恢复建议
eprintln!("💡 建议: {}", error.suggestion());
// 获取详细的恢复步骤
for (i, step) in error.recovery_steps().iter().enumerate() {
eprintln!("{}. {}", i + 1, step);
}
// 获取完整的错误分析报告
eprintln!("\n{}", ErrorAnalyzer::new(&error).detailed_report());
// 根据错误类型进行特定处理
if error.is_validation_error() {
eprintln!("请检查配置参数是否正确");
} else if error.is_network_error() {
eprintln!("请检查网络连接并稍后重试");
} else if error.is_auth_error() {
eprintln!("请检查应用凭据是否有效");
}
}
}§错误类型和处理
use openlark_client::prelude::*;
// 捕获和处理特定类型的错误
async fn send_message_with_error_handling() -> Result<()> {
let client = Client::from_env()?;
// 单入口:meta 链式字段访问。这里演示“拿到入口 + 挂上错误上下文”的模式。
#[cfg(feature = "communication")]
let _comm = &client.communication;
// 具体 API 调用请使用 openlark-communication 的强类型请求/构建器并在 `.await` 处处理 Result。
Ok(())
}Re-exports§
pub use client::Client;pub use client::ClientBuilder;pub use config::Config;pub use error::Error;pub use error::Result;pub use error::with_context;pub use error::with_operation_context;pub use error::ClientErrorExt;pub use error::ErrorAnalyzer;pub use error::api_error;pub use error::authentication_error;pub use error::business_error;pub use error::configuration_error;pub use error::internal_error;pub use error::network_error;pub use error::rate_limit_error;pub use error::registry_error;pub use error::serialization_error;pub use error::timeout_error;pub use error::validation_error;pub use features::FeatureLoader;pub use registry::DefaultServiceRegistry;pub use registry::ServiceEntry;pub use registry::ServiceMetadata;pub use registry::ServiceRegistry;pub use registry::ServiceStatus;pub use traits::LarkClient;pub use traits::ServiceLifecycle;pub use client::AuthClient;
Modules§
- client
- OpenLark Client - 全新简化架构
- config
- OpenLark Client 配置管理
- error
- OpenLark Client 错误类型定义
- features
- 🔥 OpenLark Client Feature Loader
- info
- 🏷️ 库信息
- lazy
- 延迟初始化工具模块
- prelude
- 🚀 预导出模块 - 包含最常用的类型和特征
- registry
- 服务注册和发现机制
- traits
- OpenLark Client 核心特征定义
- types
- OpenLark Client 类型系统
- utils
- 🔧 实用工具函数
Structs§
- Communication
Client - Communication 链式入口:
communication.im/communication.contact/communication.moments - Core
Config - 零拷贝配置共享实现
Enums§
- Core
Error - 轻量版核心错误
- Error
Code - 标准错误码枚举
- Error
Severity - 错误严重程度
- Error
Type - 错误类型分类
Traits§
- Error
Trait - 核心错误特征
Type Aliases§
- Core
Result - SDK 操作结果类型别名
- SDKResult
- 🚨 SDK 结果类型别名(与 Core 系统兼容)