open-lark 0.13.0

Enterprise-grade Lark/Feishu Open API SDK with comprehensive Chinese documentation and advanced error handling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#[cfg(any(
    feature = "attendance",
    feature = "authentication",
    feature = "im",
    feature = "search",
    feature = "cloud-docs"
))]
use std::sync::Arc;
use std::time::Duration;

use crate::core::{config::Config, constants::AppType};

// 条件导入服务
#[cfg(feature = "acs")]
use crate::service::acs::AcsService;
#[cfg(feature = "admin")]
use crate::service::admin::AdminService;
#[cfg(feature = "ai")]
use crate::service::ai::AiService;
#[cfg(feature = "aily")]
use crate::service::aily::AilyService;
#[cfg(feature = "apass")]
use crate::service::apass::ApassService;
#[cfg(feature = "application")]
use crate::service::application::ApplicationService;
#[cfg(feature = "approval")]
use crate::service::approval::ApprovalService;
#[cfg(feature = "attendance")]
use crate::service::attendance::AttendanceService;
#[cfg(feature = "authentication")]
use crate::service::authentication::AuthenService;
#[cfg(feature = "bot")]
use crate::service::bot::BotService;
#[cfg(feature = "calendar")]
use crate::service::calendar::CalendarService;
#[cfg(feature = "cardkit")]
use crate::service::cardkit::CardkitService;
#[cfg(feature = "cloud-docs")]
use crate::service::cloud_docs::CloudDocsService;
#[cfg(feature = "contact")]
use crate::service::contact::ContactService;
#[cfg(feature = "corehr")]
use crate::service::corehr::CoreHRService;
#[cfg(feature = "directory")]
use crate::service::directory::DirectoryService;
#[cfg(feature = "ehr")]
use crate::service::ehr::EhrService;
#[cfg(feature = "elearning")]
use crate::service::elearning::ELearningService;
#[cfg(feature = "group")]
use crate::service::group::GroupService;
#[cfg(feature = "helpdesk")]
use crate::service::helpdesk::HelpdeskService;
#[cfg(feature = "hire")]
use crate::service::hire::HireService;
#[cfg(feature = "human-authentication")]
use crate::service::human_authentication::HumanAuthenticationService;
#[cfg(feature = "im")]
use crate::service::im::ImService;
#[cfg(feature = "lingo")]
use crate::service::lingo::LingoService;
#[cfg(feature = "mail")]
use crate::service::mail::MailService;
#[cfg(feature = "mdm")]
use crate::service::mdm::MdmService;
#[cfg(feature = "minutes")]
use crate::service::minutes::MinutesService;
#[cfg(feature = "moments")]
use crate::service::moments::MomentsService;
#[cfg(feature = "okr")]
use crate::service::okr::OkrService;
#[cfg(feature = "payroll")]
use crate::service::payroll::PayrollService;
#[cfg(feature = "performance")]
use crate::service::performance::PerformanceService;
#[cfg(feature = "personal-settings")]
use crate::service::personal_settings::PersonalSettingsService;
#[cfg(feature = "report")]
use crate::service::report::ReportService;
#[cfg(feature = "search")]
use crate::service::search::SearchService;
#[cfg(feature = "security-and-compliance")]
use crate::service::security_and_compliance::SecurityAndComplianceService;
#[cfg(feature = "task")]
use crate::service::task::TaskV2Service;
#[cfg(feature = "tenant")]
use crate::service::tenant::TenantService;
#[cfg(feature = "tenant-tag")]
use crate::service::tenant_tag::TenantTagService;
#[cfg(feature = "trust-party")]
use crate::service::trust_party::TrustPartyService;
#[cfg(feature = "vc")]
use crate::service::vc::VcService;
#[cfg(feature = "verification")]
use crate::service::verification::VerificationService;
#[cfg(feature = "workplace")]
use crate::service::workplace::WorkplaceService;

// 向后兼容的导入
#[cfg(feature = "cloud-docs")]
use crate::service::{
    AssistantService, BitableService, BoardService, CommentsService, DocsService, DriveService,
    PermissionService, SheetsService, WikiService,
};

#[cfg(feature = "websocket")]
pub mod ws_client;

/// 飞书开放平台SDK主客户端
///
/// 提供对所有飞书开放平台API的统一访问接口。支持自建应用和商店应用两种类型,
/// 自动处理认证、令牌管理、请求重试等核心功能。
///
/// # 主要功能
///
/// - 🔐 自动令牌管理和刷新
/// - 🚀 支持所有飞书开放平台API
/// - 🔄 内置请求重试机制
/// - 📡 WebSocket长连接支持(需开启websocket特性)
/// - 🎯 类型安全的API调用
///
/// # 快速开始
///
/// ```rust
/// use open_lark::prelude::*;
///
/// // 创建自建应用客户端
/// let client = LarkClient::builder("your_app_id", "your_app_secret")
///     .with_app_type(AppType::SelfBuild)
///     .with_enable_token_cache(true)
///     .build();
///
/// // 发送文本消息
/// let message = CreateMessageRequestBody::builder()
///     .receive_id("ou_xxx")
///     .msg_type("text")
///     .content("{\"text\":\"Hello from Rust!\"}")
///     .build();
///
/// let request = CreateMessageRequest::builder()
///     .receive_id_type("open_id")
///     .request_body(message)
///     .build();
///
/// // let result = client.im.message.create(request, None).await?;
/// ```
///
/// # 服务模块
///
/// 客户端包含以下主要服务模块:
/// - `im`: 即时消息
/// - `drive`: 云盘文件
/// - `sheets`: 电子表格
/// - `calendar`: 日历
/// - `contact`: 通讯录
/// - `hire`: 招聘
/// - 更多服务请参考各字段文档
pub struct LarkClient {
    pub config: Config,
    // 核心服务 - 使用条件编译
    #[cfg(feature = "acs")]
    pub acs: AcsService,
    #[cfg(feature = "admin")]
    pub admin: AdminService,
    #[cfg(feature = "ai")]
    pub ai: AiService,
    #[cfg(feature = "aily")]
    pub aily: AilyService,
    #[cfg(feature = "apass")]
    pub apass: ApassService,
    #[cfg(feature = "application")]
    pub application: ApplicationService,
    #[cfg(feature = "approval")]
    pub approval: ApprovalService,
    #[cfg(feature = "attendance")]
    pub attendance: AttendanceService,
    #[cfg(feature = "authentication")]
    pub auth: AuthenService,
    #[cfg(feature = "bot")]
    pub bot: BotService,
    #[cfg(feature = "calendar")]
    pub calendar: CalendarService,
    #[cfg(feature = "cardkit")]
    pub cardkit: CardkitService,
    #[cfg(feature = "contact")]
    pub contact: ContactService,
    #[cfg(feature = "corehr")]
    pub corehr: CoreHRService,
    #[cfg(feature = "directory")]
    pub directory: DirectoryService,
    #[cfg(feature = "ehr")]
    pub ehr: EhrService,
    #[cfg(feature = "elearning")]
    pub elearning: ELearningService,
    #[cfg(feature = "group")]
    pub group: GroupService,
    #[cfg(feature = "helpdesk")]
    pub helpdesk: HelpdeskService,
    #[cfg(feature = "hire")]
    pub hire: HireService,
    #[cfg(feature = "human-authentication")]
    pub human_authentication: HumanAuthenticationService,
    #[cfg(feature = "im")]
    pub im: ImService,
    #[cfg(feature = "lingo")]
    pub lingo: LingoService,
    #[cfg(feature = "mail")]
    pub mail: MailService,
    #[cfg(feature = "mdm")]
    pub mdm: MdmService,
    #[cfg(feature = "minutes")]
    pub minutes: MinutesService,
    #[cfg(feature = "moments")]
    pub moments: MomentsService,
    #[cfg(feature = "okr")]
    pub okr: OkrService,
    #[cfg(feature = "payroll")]
    pub payroll: PayrollService,
    #[cfg(feature = "performance")]
    pub performance: PerformanceService,
    #[cfg(feature = "personal-settings")]
    pub personal_settings: PersonalSettingsService,
    #[cfg(feature = "report")]
    pub report: ReportService,
    #[cfg(feature = "search")]
    pub search: SearchService,
    #[cfg(feature = "security-and-compliance")]
    pub security_and_compliance: SecurityAndComplianceService,
    #[cfg(feature = "task")]
    pub task: TaskV2Service,
    #[cfg(feature = "tenant")]
    pub tenant: TenantService,
    #[cfg(feature = "tenant-tag")]
    pub tenant_tag: TenantTagService,
    #[cfg(feature = "trust-party")]
    pub trust_party: TrustPartyService,
    #[cfg(feature = "vc")]
    pub vc: VcService,
    #[cfg(feature = "verification")]
    pub verification: VerificationService,
    #[cfg(feature = "workplace")]
    pub workplace: WorkplaceService,
    // 云文档服务聚合
    #[cfg(feature = "cloud-docs")]
    pub cloud_docs: CloudDocsService,
    // 向后兼容的字段
    #[cfg(feature = "cloud-docs")]
    pub assistant: AssistantService,
    #[cfg(feature = "cloud-docs")]
    pub docs: DocsService,
    #[cfg(feature = "cloud-docs")]
    pub drive: DriveService,
    #[cfg(feature = "cloud-docs")]
    pub sheets: SheetsService,
    #[cfg(feature = "cloud-docs")]
    pub bitable: BitableService,
    #[cfg(feature = "cloud-docs")]
    pub wiki: WikiService,
    #[cfg(feature = "cloud-docs")]
    pub comments: CommentsService,
    #[cfg(feature = "cloud-docs")]
    pub permission: PermissionService,
    #[cfg(feature = "cloud-docs")]
    pub board: BoardService,
}

/// 飞书客户端构建器
///
/// 使用构建器模式配置和创建LarkClient实例。支持链式调用配置各种选项。
///
/// # 示例
///
/// ```rust
/// use open_lark::prelude::*;
///
/// let client = LarkClient::builder("app_id", "app_secret")
///     .with_app_type(AppType::SelfBuild)
///     .with_enable_token_cache(true)
///     .with_req_timeout(Some(30.0))
///     .build();
/// ```
pub struct LarkClientBuilder {
    config: Config,
}

impl LarkClientBuilder {
    /// 设置应用类型
    ///
    /// # 参数
    /// - `app_type`: 应用类型,`AppType::SelfBuild`(自建应用)或`AppType::Marketplace`(商店应用)
    pub fn with_app_type(mut self, app_type: AppType) -> Self {
        self.config.app_type = app_type;
        self
    }

    /// 设置为商店应用(等同于 `with_app_type(AppType::Marketplace)`)
    pub fn with_marketplace_app(mut self) -> Self {
        self.config.app_type = AppType::Marketplace;
        self
    }

    /// 设置自定义API基础URL
    ///
    /// # 参数
    /// - `base_url`: 自定义的API基础URL,默认为官方地址
    pub fn with_open_base_url(mut self, base_url: String) -> Self {
        self.config.base_url = base_url;
        self
    }

    /// 启用或禁用令牌缓存
    ///
    /// # 参数
    /// - `enable`: 是否启用令牌缓存,建议启用以提高性能
    pub fn with_enable_token_cache(mut self, enable: bool) -> Self {
        self.config.enable_token_cache = enable;
        self
    }

    /// 设置请求超时时间
    ///
    /// # 参数
    /// - `timeout`: 超时时间(秒),None表示使用默认值
    pub fn with_req_timeout(mut self, timeout: Option<f32>) -> Self {
        self.config.req_timeout = timeout.map(Duration::from_secs_f32);
        self
    }

    /// 构建LarkClient实例
    ///
    /// 根据配置的参数创建最终的客户端实例。
    pub fn build(mut self) -> LarkClient {
        if let Some(req_timeout) = self.config.req_timeout {
            self.config.http_client = reqwest::Client::builder()
                .timeout(req_timeout)
                .build()
                .expect("Failed to build HTTP client with timeout")
        }

        LarkClient {
            config: self.config.clone(),
            // 核心服务 - 使用条件编译
            #[cfg(feature = "acs")]
            acs: AcsService::new(self.config.clone()),
            #[cfg(feature = "admin")]
            admin: AdminService::new(self.config.clone()),
            #[cfg(feature = "ai")]
            ai: AiService::new(self.config.clone()),
            #[cfg(feature = "aily")]
            aily: AilyService::new(self.config.clone()),
            #[cfg(feature = "apass")]
            apass: ApassService::new(self.config.clone()),
            #[cfg(feature = "application")]
            application: ApplicationService::new(self.config.clone()),
            #[cfg(feature = "approval")]
            approval: ApprovalService::new(self.config.clone()),
            #[cfg(feature = "attendance")]
            attendance: AttendanceService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "authentication")]
            auth: AuthenService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "bot")]
            bot: BotService::new(self.config.clone()),
            #[cfg(feature = "calendar")]
            calendar: CalendarService::new(self.config.clone()),
            #[cfg(feature = "cardkit")]
            cardkit: CardkitService::new(self.config.clone()),
            #[cfg(feature = "contact")]
            contact: ContactService::new(self.config.clone()),
            #[cfg(feature = "corehr")]
            corehr: CoreHRService::new(self.config.clone()),
            #[cfg(feature = "directory")]
            directory: DirectoryService::new(self.config.clone()),
            #[cfg(feature = "ehr")]
            ehr: EhrService::new(self.config.clone()),
            #[cfg(feature = "elearning")]
            elearning: ELearningService::new(self.config.clone()),
            #[cfg(feature = "group")]
            group: GroupService::new(self.config.clone()),
            #[cfg(feature = "helpdesk")]
            helpdesk: HelpdeskService::new(self.config.clone()),
            #[cfg(feature = "hire")]
            hire: HireService::new(self.config.clone()),
            #[cfg(feature = "human-authentication")]
            human_authentication: HumanAuthenticationService::new(self.config.clone()),
            #[cfg(feature = "im")]
            im: ImService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "lingo")]
            lingo: LingoService::new(self.config.clone()),
            #[cfg(feature = "mail")]
            mail: MailService::new(self.config.clone()),
            #[cfg(feature = "mdm")]
            mdm: MdmService::new(self.config.clone()),
            #[cfg(feature = "minutes")]
            minutes: MinutesService::new(self.config.clone()),
            #[cfg(feature = "moments")]
            moments: MomentsService::new(self.config.clone()),
            #[cfg(feature = "okr")]
            okr: OkrService::new(self.config.clone()),
            #[cfg(feature = "payroll")]
            payroll: PayrollService::new(self.config.clone()),
            #[cfg(feature = "performance")]
            performance: PerformanceService::new(self.config.clone()),
            #[cfg(feature = "personal-settings")]
            personal_settings: PersonalSettingsService::new(self.config.clone()),
            #[cfg(feature = "report")]
            report: ReportService::new(self.config.clone()),
            #[cfg(feature = "search")]
            search: SearchService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "security-and-compliance")]
            security_and_compliance: SecurityAndComplianceService::new(self.config.clone()),
            #[cfg(feature = "task")]
            task: TaskV2Service::new(self.config.clone()),
            #[cfg(feature = "tenant")]
            tenant: TenantService::new(self.config.clone()),
            #[cfg(feature = "tenant-tag")]
            tenant_tag: TenantTagService::new(self.config.clone()),
            #[cfg(feature = "trust-party")]
            trust_party: TrustPartyService::new(self.config.clone()),
            #[cfg(feature = "vc")]
            vc: VcService::new(self.config.clone()),
            #[cfg(feature = "verification")]
            verification: VerificationService::new(self.config.clone()),
            #[cfg(feature = "workplace")]
            workplace: WorkplaceService::new(self.config.clone()),
            // 云文档服务聚合
            #[cfg(feature = "cloud-docs")]
            cloud_docs: CloudDocsService::new(Arc::new(self.config.clone())),
            // 向后兼容的字段(重新创建实例)
            #[cfg(feature = "cloud-docs")]
            assistant: AssistantService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "cloud-docs")]
            docs: DocsService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "cloud-docs")]
            drive: DriveService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "cloud-docs")]
            sheets: SheetsService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "cloud-docs")]
            bitable: BitableService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "cloud-docs")]
            wiki: WikiService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "cloud-docs")]
            comments: CommentsService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "cloud-docs")]
            permission: PermissionService::new(Arc::new(self.config.clone())),
            #[cfg(feature = "cloud-docs")]
            board: BoardService::new(Arc::new(self.config.clone())),
        }
    }
}

impl LarkClient {
    /// 创建客户端构建器
    ///
    /// # 参数
    /// - `app_id`: 应用ID,从飞书开放平台获取
    /// - `app_secret`: 应用密钥,从飞书开放平台获取
    ///
    /// # 示例
    /// ```rust
    /// use open_lark::prelude::*;
    ///
    /// let client = LarkClient::builder("cli_xxx", "xxx")
    ///     .with_app_type(AppType::SelfBuild)
    ///     .build();
    /// ```
    pub fn builder(app_id: &str, app_secret: &str) -> LarkClientBuilder {
        LarkClientBuilder {
            config: Config {
                app_id: app_id.to_string(),
                app_secret: app_secret.to_string(),
                ..Default::default()
            },
        }
    }
}