Skip to main content

open_lark/
lib.rs

1//! OpenLark 官方入口 crate。
2//!
3//! 普通用户应优先使用 `openlark`,通过业务 feature 开启所需能力:
4//!
5//! ```toml
6//! [dependencies]
7//! openlark = { version = "0.17.0", default-features = false, features = ["auth", "docs-drive", "docs-bitable", "webhook-signature"] }
8//! ```
9//!
10//! - 统一客户端入口:[`Client`]
11//! - 高级业务模块入口:[`auth`]、[`communication`]、[`docs`]、[`workflow`] 等
12//! - 统一预导出:[`prelude`]
13//!
14//! 若只想要单一业务域的最小依赖,再直接使用 `openlark-{domain}` 子 crate。
15//!
16//! 推荐顺序:
17//!
18//! - 运行时入口:[`Client`] / [`ClientBuilder`]
19//! - 导入入口:[`prelude`]
20//! - 业务命名空间:`open_lark::auth`、`open_lark::communication`、`open_lark::docs`
21//! - 最小依赖场景:直接依赖对应 `openlark-{domain}` crate
22
23// 允许测试模块中的未使用导入(测试桩代码常见模式)
24#![allow(unused_imports)]
25
26// ============================================================================
27// 核心类型导出
28// ============================================================================
29
30pub use openlark_client::{Client, ClientBuilder, Error, Result};
31pub use openlark_core::SDKResult;
32pub use openlark_core::config::Config;
33pub use openlark_core::config::Config as CoreConfig;
34pub use openlark_core::error::{CoreError, ErrorCode, ErrorSeverity, ErrorTrait, ErrorType};
35pub use openlark_core::req_option::RequestOption;
36
37#[cfg(feature = "websocket")]
38/// WebSocket 客户端相关类型导出。
39pub mod ws_client {
40    pub use openlark_client::ws_client::*;
41}
42
43// ============================================================================
44// 业务命名空间导出
45// ============================================================================
46
47#[cfg(feature = "auth")]
48pub use openlark_auth as auth;
49
50#[cfg(feature = "communication")]
51pub use openlark_communication as communication;
52
53#[cfg(any(
54    feature = "docs",
55    feature = "docs-ccm",
56    feature = "docs-base",
57    feature = "docs-bitable",
58    feature = "docs-drive",
59    feature = "docs-explorer",
60    feature = "docs-sheets",
61    feature = "docs-full"
62))]
63pub use openlark_docs as docs;
64
65#[cfg(feature = "hr")]
66pub use openlark_hr as hr;
67
68#[cfg(feature = "ai")]
69pub use openlark_ai as ai;
70
71#[cfg(feature = "helpdesk")]
72pub use openlark_helpdesk as helpdesk;
73
74#[cfg(feature = "mail")]
75pub use openlark_mail as mail;
76
77#[cfg(feature = "bot")]
78pub use openlark_bot as bot;
79
80#[cfg(feature = "meeting")]
81pub use openlark_meeting as meeting;
82
83#[cfg(feature = "application")]
84pub use openlark_application as application;
85
86#[cfg(feature = "security")]
87pub use openlark_security as security;
88
89#[cfg(feature = "workflow")]
90pub use openlark_workflow as workflow;
91
92#[cfg(feature = "platform")]
93pub use openlark_platform as platform;
94
95#[cfg(feature = "analytics")]
96pub use openlark_analytics as analytics;
97
98#[cfg(feature = "user")]
99pub use openlark_user as user;
100
101#[cfg(feature = "webhook")]
102pub use openlark_webhook as webhook;
103
104#[cfg(feature = "cardkit")]
105pub use openlark_cardkit as cardkit;
106
107// ============================================================================
108// 预导出模块
109// ============================================================================
110
111/// 面向 `openlark` 用户的统一预导出。
112///
113/// 该模块只导出"创建客户端 + 顶层业务入口"所需的稳定公共类型。
114///(历史上 registry / feature loader / traits 等 speculative 半边曾位于
115/// `openlark-client`,#471 / 0.19 已移除——零外部消费者。)
116pub mod prelude {
117    pub use crate::SDKResult;
118    pub use crate::{Client, ClientBuilder, CoreConfig, Error, Result};
119    pub use crate::{CoreError, ErrorCode, ErrorSeverity, ErrorTrait, ErrorType, RequestOption};
120    pub use openlark_core::prelude::*;
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126
127    fn build_test_client() -> Result<Client> {
128        Client::builder()
129            .app_id("test_app")
130            .app_secret("test_secret")
131            .build()
132    }
133
134    #[test]
135    fn root_prelude_exposes_canonical_core_entrypoints() {
136        use crate::prelude::*;
137
138        let _builder: ClientBuilder = Client::builder();
139        let _config: CoreConfig = CoreConfig::builder()
140            .app_id("test_app")
141            .app_secret("test_secret")
142            .build();
143        let _request_option: Option<RequestOption> = None;
144    }
145
146    #[test]
147    fn root_minimal_builder_works_without_service_features() {
148        let client = build_test_client().expect("client should build with minimal features");
149        assert_eq!(client.config().app_id(), "test_app");
150    }
151
152    #[cfg(feature = "auth")]
153    #[test]
154    fn root_client_exposes_auth_entrypoint() {
155        let client = build_test_client().expect("client should build with auth feature");
156        let _auth = &client.auth;
157    }
158
159    #[cfg(feature = "communication")]
160    #[test]
161    fn root_client_exposes_communication_namespace() {
162        let client = build_test_client().expect("client should build with communication feature");
163        let _communication = &client.communication;
164        let _endpoint = crate::communication::endpoints::IM_V1_MESSAGES;
165    }
166
167    #[cfg(any(
168        feature = "docs",
169        feature = "docs-ccm",
170        feature = "docs-base",
171        feature = "docs-bitable",
172        feature = "docs-drive",
173        feature = "docs-explorer",
174        feature = "docs-sheets",
175        feature = "docs-full"
176    ))]
177    #[test]
178    fn root_client_exposes_docs_namespace() {
179        let client = build_test_client().expect("client should build with docs feature");
180        let _docs = &client.docs;
181    }
182
183    #[cfg(feature = "hr")]
184    #[test]
185    fn root_client_exposes_hr_entrypoint() {
186        let client = build_test_client().expect("client should build with hr feature");
187        let _hr = &client.hr;
188    }
189
190    #[cfg(feature = "security")]
191    #[test]
192    fn root_client_exposes_security_entrypoint() {
193        let client = build_test_client().expect("client should build with security feature");
194        let _security = &client.security;
195    }
196
197    #[cfg(feature = "docs-bitable")]
198    #[test]
199    fn root_docs_bitable_feature_exposes_query_helper() {
200        let query = crate::docs::BitableRecordQuery::new("app_token", "table_id")
201            .where_equals("状态", "进行中");
202
203        assert_eq!(query.app_token, "app_token");
204        assert_eq!(query.table_id, "table_id");
205    }
206
207    #[cfg(feature = "docs-drive")]
208    #[test]
209    fn root_docs_drive_feature_exposes_drive_helpers() {
210        let upload = crate::docs::DriveUploadFile::new("demo.txt", vec![1, 2, 3]);
211        let range = crate::docs::DriveDownloadRange::from_start(0).with_end(9);
212
213        assert_eq!(upload.file_name, "demo.txt");
214        assert_eq!(upload.size(), 3);
215        assert_eq!(range.to_header_value(), "bytes=0-9");
216    }
217
218    #[cfg(feature = "essential")]
219    #[test]
220    fn root_essential_feature_combines_docs_and_communication_paths() {
221        let client = build_test_client().expect("client should build with essential feature");
222
223        let _communication = &client.communication;
224        let _docs = &client.docs;
225        let recipient = crate::communication::MessageRecipient::open_id("ou_xxx");
226        let query = crate::docs::BitableRecordQuery::new("app_token", "table_id");
227
228        assert_eq!(recipient.receive_id, "ou_xxx");
229        assert_eq!(query.table_id, "table_id");
230    }
231
232    #[cfg(feature = "enterprise")]
233    #[test]
234    fn root_enterprise_feature_combines_quality_critical_domains() {
235        let client = build_test_client().expect("client should build with enterprise feature");
236
237        let _security = &client.security;
238        let _hr = &client.hr;
239        let _workflow = &client.workflow;
240        let action = crate::workflow::ApprovalTaskAction::new(
241            "approval_code",
242            "instance_code",
243            "ou_xxx",
244            "task_123",
245        )
246        .comment("同意");
247
248        assert_eq!(action.task_id, "task_123");
249        assert_eq!(action.comment.as_deref(), Some("同意"));
250    }
251
252    #[cfg(feature = "webhook-full")]
253    #[test]
254    fn root_webhook_full_feature_exposes_signature_and_robot_client() {
255        let client = crate::webhook::WebhookClient::new();
256        let signature = crate::webhook::common::signature::sign(1_700_000_000, "secret");
257
258        let _ = client;
259        assert!(!signature.is_empty());
260    }
261}