Skip to main content

openlark_docs/common/
mod.rs

1/// 文档服务 API 端点定义。
2pub mod api_endpoints;
3/// 文档服务 API 通用辅助函数。
4pub mod api_utils;
5/// 文档服务请求构建辅助模块。
6pub mod builders;
7/// 文档服务链式调用入口模块。
8pub mod chain;
9/// 文档服务通用请求构建器模块。
10pub mod request_builder;
11#[cfg(all(
12    test,
13    any(
14        feature = "baike",
15        feature = "lingo",
16        feature = "ccm-core",
17        feature = "minutes"
18    )
19))]
20pub(crate) mod test_utils;
21
22/// 重新导出常用 API 端点枚举。
23pub use api_endpoints::{BaseApiV2, BitableApiV1, MinutesApiV1, SheetsApiV3};
24
25/// 通用常量定义。
26pub mod constants {
27    /// 默认分页大小。
28    pub const DEFAULT_PAGE_SIZE: i32 = 20;
29    /// 最大分页大小。
30    pub const MAX_PAGE_SIZE: i32 = 100;
31    /// 默认超时时间(秒)。
32    pub const DEFAULT_TIMEOUT_SECS: u64 = 30;
33}
34
35/// 通用类型别名。
36pub mod types {
37    /// 多维表格应用 Token。
38    pub type AppToken = String;
39    /// 数据表 ID。
40    pub type TableId = String;
41    /// 记录 ID。
42    pub type RecordId = String;
43    /// 表单 ID。
44    pub type FormId = String;
45    /// 视图 ID。
46    pub type ViewId = String;
47    /// 字段 ID。
48    pub type FieldId = String;
49    /// 角色 ID。
50    pub type RoleId = String;
51    /// 用户 ID。
52    pub type UserId = String;
53}
54
55/// 批量操作相关类型。
56pub mod batch {
57    use serde::{Deserialize, Serialize};
58
59    /// 通用批量操作参数。
60    #[derive(Debug, Clone, Default, Serialize, Deserialize)]
61    pub struct BatchCommonParams {
62        /// 用户 ID 类型。
63        pub user_id_type: Option<String>,
64        /// 客户端幂等令牌。
65        pub client_token: Option<String>,
66    }
67
68    impl BatchCommonParams {
69        /// 创建默认批量操作参数。
70        pub fn new() -> Self {
71            Self::default()
72        }
73
74        /// 设置用户 ID 类型。
75        pub fn with_user_id_type(mut self, user_id_type: impl ToString) -> Self {
76            self.user_id_type = Some(user_id_type.to_string());
77            self
78        }
79
80        /// 设置客户端幂等令牌。
81        pub fn with_client_token(mut self, client_token: impl ToString) -> Self {
82            self.client_token = Some(client_token.to_string());
83            self
84        }
85    }
86
87    /// 通用批量请求体。
88    #[derive(Debug, Clone, Default, Serialize, Deserialize)]
89    pub struct BatchCommonBody {
90        /// 请求列表。
91        pub requests: Vec<serde_json::Value>,
92    }
93
94    /// 通用批量操作结果。
95    #[derive(Debug, Clone, Default, Serialize, Deserialize)]
96    pub struct BatchOperationResult {
97        /// 成功标识。
98        pub success: bool,
99        /// 结果列表。
100        pub results: Vec<BatchItemResult>,
101        /// 错误信息(如果有)。
102        pub error: Option<String>,
103    }
104
105    impl BatchOperationResult {
106        /// 创建成功结果。
107        pub fn success() -> Self {
108            Self {
109                success: true,
110                results: Vec::new(),
111                error: None,
112            }
113        }
114
115        /// 创建失败结果。
116        pub fn failure(error: impl ToString) -> Self {
117            Self {
118                success: false,
119                results: Vec::new(),
120                error: Some(error.to_string()),
121            }
122        }
123    }
124
125    /// 批量操作中的单项结果。
126    #[derive(Debug, Clone, Default, Serialize, Deserialize)]
127    pub struct BatchItemResult {
128        /// 成功标识。
129        pub success: bool,
130        /// 数据(如果有)。
131        pub data: Option<serde_json::Value>,
132        /// 错误信息(如果有)。
133        pub error: Option<String>,
134    }
135}
136
137/// 通用特征定义。
138pub mod traits {
139    /// API 请求基础特征。
140    pub trait ApiRequest {
141        /// 请求对应的响应类型。
142        type Response;
143
144        /// 校验请求参数。
145        fn validate(&self) -> openlark_core::SDKResult<()>;
146        /// 构建请求路径。
147        fn build_path(&self) -> String;
148    }
149
150    /// 可分页请求特征。
151    pub trait PaginatedRequest {
152        /// 设置分页游标。
153        fn page_token(self, token: impl Into<String>) -> Self;
154        /// 设置分页大小。
155        fn page_size(self, size: i32) -> Self;
156    }
157
158    /// 可筛选请求特征。
159    pub trait FilterableRequest {
160        /// 添加一个筛选条件。
161        fn add_filter(self, filter: serde_json::Value) -> Self;
162    }
163}