open-lark 0.14.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
use reqwest::Method;
use serde::{Deserialize, Serialize};

use crate::{
    core::{
        api_req::ApiRequest,
        api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
        config::Config,
        constants::AccessTokenType,
        endpoints::cloud_docs::*,
        http::Transport,
        req_option::RequestOption,
        SDKResult,
    },
    impl_executable_builder_owned,
};

use super::{
    get::{FileType, SubscriptionDetail},
    SubscriptionService,
};

/// 创建订阅请求
#[derive(Debug, Serialize, Default, Clone)]
pub struct CreateSubscriptionRequest {
    #[serde(skip)]
    api_request: ApiRequest,
    /// 文档token
    #[serde(skip)]
    file_token: String,
    /// 文档类型
    #[serde(skip)]
    file_type: String,
    /// 订阅配置
    #[serde(skip_serializing_if = "Option::is_none")]
    pub config: Option<SubscriptionConfig>,
    /// 扩展信息
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extra: Option<serde_json::Value>,
}

/// 订阅配置
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct SubscriptionConfig {
    pub enable_notification: Option<bool>,
    pub notification_interval: Option<i32>,
    pub priority: Option<SubscriptionPriority>,
    pub auto_renew: Option<bool>,
    pub tags: Option<Vec<String>>,
}

/// 订阅优先级
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub enum SubscriptionPriority {
    /// 低优先级
    Low,
    /// 普通优先级
    Normal,
    /// 高优先级
    High,
    /// 紧急优先级
    Urgent,
}

impl CreateSubscriptionRequest {
    pub fn builder() -> CreateSubscriptionRequestBuilder {
        CreateSubscriptionRequestBuilder::default()
    }

    pub fn new(file_token: impl ToString, file_type: FileType) -> Self {
        Self {
            file_token: file_token.to_string(),
            file_type: file_type.to_string(),
            ..Default::default()
        }
    }
}

#[derive(Default)]
pub struct CreateSubscriptionRequestBuilder {
    request: CreateSubscriptionRequest,
}

impl CreateSubscriptionRequestBuilder {
    /// 文档token
    pub fn file_token(mut self, token: impl ToString) -> Self {
        self.request.file_token = token.to_string();
        self
    }

    /// 文档类型
    pub fn file_type(mut self, file_type: FileType) -> Self {
        self.request.file_type = file_type.to_string();
        self
    }

    /// 设置为多维表格
    pub fn as_bitable(mut self) -> Self {
        self.request.file_type = FileType::Bitable.to_string();
        self
    }

    /// 设置为文档
    pub fn as_doc(mut self) -> Self {
        self.request.file_type = FileType::Doc.to_string();
        self
    }

    /// 设置为表格
    pub fn as_sheet(mut self) -> Self {
        self.request.file_type = FileType::Sheet.to_string();
        self
    }

    /// 设置为Wiki
    pub fn as_wiki(mut self) -> Self {
        self.request.file_type = FileType::Wiki.to_string();
        self
    }

    /// 设置订阅配置
    pub fn config(mut self, config: SubscriptionConfig) -> Self {
        self.request.config = Some(config);
        self
    }

    /// 启用实时通知
    pub fn with_notification(mut self, enable: bool) -> Self {
        let mut config = self.request.config.unwrap_or_default();
        config.enable_notification = Some(enable);
        self.request.config = Some(config);
        self
    }

    /// 设置通知频率(秒)
    pub fn notification_interval(mut self, interval: i32) -> Self {
        let mut config = self.request.config.unwrap_or_default();
        config.notification_interval = Some(interval.max(1));
        self.request.config = Some(config);
        self
    }

    /// 设置订阅优先级
    pub fn priority(mut self, priority: SubscriptionPriority) -> Self {
        let mut config = self.request.config.unwrap_or_default();
        config.priority = Some(priority);
        self.request.config = Some(config);
        self
    }

    /// 设置为高优先级
    pub fn high_priority(self) -> Self {
        self.priority(SubscriptionPriority::High)
    }

    /// 设置为低优先级
    pub fn low_priority(self) -> Self {
        self.priority(SubscriptionPriority::Low)
    }

    /// 启用自动续费
    pub fn auto_renew(mut self, enable: bool) -> Self {
        let mut config = self.request.config.unwrap_or_default();
        config.auto_renew = Some(enable);
        self.request.config = Some(config);
        self
    }

    /// 添加标签
    pub fn add_tag(mut self, tag: impl ToString) -> Self {
        let mut config = self.request.config.unwrap_or_default();
        let mut tags = config.tags.unwrap_or_default();
        tags.push(tag.to_string());
        config.tags = Some(tags);
        self.request.config = Some(config);
        self
    }

    /// 添加多个标签
    pub fn add_tags(mut self, tags: Vec<String>) -> Self {
        let mut config = self.request.config.unwrap_or_default();
        let mut existing_tags = config.tags.unwrap_or_default();
        existing_tags.extend(tags);
        config.tags = Some(existing_tags);
        self.request.config = Some(config);
        self
    }

    /// 设置扩展信息
    pub fn extra(mut self, extra: serde_json::Value) -> Self {
        self.request.extra = Some(extra);
        self
    }

    /// 快速创建基础订阅
    pub fn basic_subscription(mut self) -> Self {
        let config = SubscriptionConfig::default();
        self.request.config = Some(config);
        self
    }

    /// 快速创建高级订阅
    pub fn premium_subscription(mut self) -> Self {
        let config = SubscriptionConfig::default();
        self.request.config = Some(config);
        self
    }

    pub fn build(mut self) -> CreateSubscriptionRequest {
        self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
        self.request
    }
}

impl_executable_builder_owned!(
    CreateSubscriptionRequestBuilder,
    SubscriptionService,
    CreateSubscriptionRequest,
    CreateSubscriptionResponse,
    create
);

/// 创建订阅响应
#[derive(Debug, Deserialize)]
pub struct CreateSubscriptionResponse {
    /// 订阅详情
    pub subscription: SubscriptionDetail,
    /// 文档token
    pub file_token: String,
    /// 文档类型
    pub file_type: String,
    /// 创建时间
    pub create_time: Option<i64>,
    /// 订阅ID
    pub subscription_id: Option<String>,
}

impl ApiResponseTrait for CreateSubscriptionResponse {
    fn data_format() -> ResponseFormat {
        ResponseFormat::Data
    }
}

/// 创建订阅
pub async fn create_subscription(
    request: CreateSubscriptionRequest,
    config: &Config,
    option: Option<RequestOption>,
) -> SDKResult<BaseResponse<CreateSubscriptionResponse>> {
    let mut api_req = request.api_request;
    api_req.http_method = Method::POST;

    api_req.api_path = ASSISTANT_V1_FILE_SUBSCRIPTION
        .replace("{}", &request.file_type)
        .replace("{}", &request.file_token);

    api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];

    let api_resp = Transport::request(api_req, config, option).await?;
    Ok(api_resp)
}

impl SubscriptionPriority {
    /// 获取优先级描述
    pub fn description(&self) -> &'static str {
        match self {
            SubscriptionPriority::Low => "低优先级",
            SubscriptionPriority::Normal => "普通优先级",
            SubscriptionPriority::High => "高优先级",
            SubscriptionPriority::Urgent => "紧急优先级",
        }
    }

    /// 获取优先级数值(用于排序)
    pub fn value(&self) -> u8 {
        match self {
            SubscriptionPriority::Low => 1,
            SubscriptionPriority::Normal => 2,
            SubscriptionPriority::High => 3,
            SubscriptionPriority::Urgent => 4,
        }
    }

    /// 获取优先级颜色
    pub fn color(&self) -> &'static str {
        match self {
            SubscriptionPriority::Low => "gray",
            SubscriptionPriority::Normal => "blue",
            SubscriptionPriority::High => "orange",
            SubscriptionPriority::Urgent => "red",
        }
    }
}

impl SubscriptionConfig {
    /// 获取通知频率(秒)
    pub fn get_notification_interval(&self) -> i32 {
        self.notification_interval.unwrap_or(3600)
    }

    /// 获取通知频率(分钟)
    pub fn get_notification_interval_minutes(&self) -> f64 {
        self.get_notification_interval() as f64 / 60.0
    }

    /// 获取通知频率(小时)
    pub fn get_notification_interval_hours(&self) -> f64 {
        self.get_notification_interval() as f64 / 3600.0
    }

    /// 是否为高频通知(小于1小时)
    pub fn is_high_frequency(&self) -> bool {
        self.get_notification_interval() < 3600
    }

    /// 是否启用通知
    pub fn has_notification(&self) -> bool {
        self.enable_notification.unwrap_or(false)
    }

    /// 获取优先级
    pub fn get_priority(&self) -> SubscriptionPriority {
        self.priority
            .clone()
            .unwrap_or(SubscriptionPriority::Normal)
    }

    /// 是否启用自动续费
    pub fn has_auto_renew(&self) -> bool {
        self.auto_renew.unwrap_or(false)
    }

    /// 获取标签列表
    pub fn get_tags(&self) -> Vec<String> {
        self.tags.clone().unwrap_or_default()
    }

    /// 是否包含指定标签
    pub fn has_tag(&self, tag: &str) -> bool {
        self.get_tags().contains(&tag.to_string())
    }

    /// 获取配置摘要
    pub fn summary(&self) -> String {
        let mut parts = Vec::new();

        parts.push(format!("优先级: {}", self.get_priority().description()));

        if self.has_notification() {
            let interval = self.get_notification_interval_hours();
            if interval < 1.0 {
                parts.push(format!("通知: 每{:.0}分钟", interval * 60.0));
            } else {
                parts.push(format!("通知: 每{interval:.1}小时"));
            }
        } else {
            parts.push("通知: 已禁用".to_string());
        }

        if self.has_auto_renew() {
            parts.push("自动续费: 是".to_string());
        }

        let tags = self.get_tags();
        if !tags.is_empty() {
            parts.push(format!("标签: {}", tags.join(", ")));
        }

        parts.join(" | ")
    }
}

impl CreateSubscriptionResponse {
    /// 获取文档类型枚举
    pub fn file_type_enum(&self) -> FileType {
        match self.file_type.as_str() {
            "bitable" => FileType::Bitable,
            "doc" => FileType::Doc,
            "sheet" => FileType::Sheet,
            "wiki" => FileType::Wiki,
            _ => FileType::Doc,
        }
    }

    /// 获取创建时间格式化字符串
    pub fn create_time_formatted(&self) -> Option<String> {
        self.create_time.map(|timestamp| {
            let datetime =
                chrono::DateTime::from_timestamp(timestamp, 0).unwrap_or_else(chrono::Utc::now);
            datetime.format("%Y-%m-%d %H:%M:%S").to_string()
        })
    }

    /// 获取完整信息摘要
    pub fn info_summary(&self) -> String {
        let mut parts = vec![
            format!(
                "{} ({})",
                self.file_type_enum().chinese_name(),
                self.file_token
            ),
            self.subscription.summary(),
        ];

        if let Some(ref subscription_id) = self.subscription_id {
            parts.push(format!("订阅ID: {subscription_id}"));
        }

        if let Some(create_time) = self.create_time_formatted() {
            parts.push(format!("创建时间: {create_time}"));
        }

        parts.join(" | ")
    }
}

#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod tests {
    use super::*;

    #[test]
    fn test_create_subscription_request_builder() {
        let request = CreateSubscriptionRequest::builder()
            .file_token("doccnxxxxxx")
            .as_doc()
            .basic_subscription()
            .build();

        assert_eq!(request.file_token, "doccnxxxxxx");
        assert_eq!(request.file_type, "doc");
        assert!(request.config.is_some());
    }

    #[test]
    fn test_subscription_priority_methods() {
        assert_eq!(SubscriptionPriority::High.description(), "高优先级");
        assert_eq!(SubscriptionPriority::High.value(), 3);
        assert_eq!(SubscriptionPriority::High.color(), "orange");
    }

    #[test]
    fn test_subscription_config_methods() {
        let config = SubscriptionConfig {
            enable_notification: Some(true),
            notification_interval: Some(3600),
            ..Default::default()
        };

        assert!(config.has_notification());
        assert_eq!(config.get_notification_interval(), 3600);
        assert_eq!(config.get_notification_interval_hours(), 1.0);
        assert!(!config.is_high_frequency());
        assert!(!config.has_auto_renew());
    }

    #[test]
    fn test_subscription_config_builder() {
        let request = CreateSubscriptionRequest::builder()
            .file_token("test")
            .as_doc()
            .high_priority()
            .notification_interval(300)
            .auto_renew(true)
            .add_tag("important")
            .build();

        let config = request.config.unwrap();
        assert_eq!(config.get_priority().value(), 3);
        assert_eq!(config.get_notification_interval(), 300);
        assert!(config.has_auto_renew());
        assert!(config.has_tag("important"));
    }
}