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
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,
    service::cloud_docs::assistant::v1::subscription::SubscriptionService,
};

/// 获取订阅状态请求
#[derive(Debug, Serialize, Default, Clone)]
pub struct GetSubscriptionRequest {
    #[serde(skip)]
    api_request: ApiRequest,
    /// 文档token
    #[serde(skip)]
    file_token: String,
    /// 文档类型
    #[serde(skip)]
    file_type: String,
}

impl GetSubscriptionRequest {
    pub fn builder() -> GetSubscriptionRequestBuilder {
        GetSubscriptionRequestBuilder::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 GetSubscriptionRequestBuilder {
    request: GetSubscriptionRequest,
}

impl GetSubscriptionRequestBuilder {
    /// 文档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 build(mut self) -> GetSubscriptionRequest {
        self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
        self.request
    }
}

impl_executable_builder_owned!(
    GetSubscriptionRequestBuilder,
    SubscriptionService,
    GetSubscriptionRequest,
    GetSubscriptionResponse,
    get
);

/// 文档类型
#[derive(Debug, Clone)]
pub enum FileType {
    /// 多维表格
    Bitable,
    /// 文档
    Doc,
    /// 电子表格
    Sheet,
    /// Wiki
    Wiki,
}

impl std::fmt::Display for FileType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FileType::Bitable => write!(f, "bitable"),
            FileType::Doc => write!(f, "doc"),
            FileType::Sheet => write!(f, "sheet"),
            FileType::Wiki => write!(f, "wiki"),
        }
    }
}

impl FileType {
    /// 获取文档类型的中文名称
    pub fn chinese_name(&self) -> &'static str {
        match self {
            FileType::Bitable => "多维表格",
            FileType::Doc => "文档",
            FileType::Sheet => "电子表格",
            FileType::Wiki => "Wiki",
        }
    }

    /// 是否支持助手功能
    pub fn supports_assistant(&self) -> bool {
        match self {
            FileType::Bitable => true,
            FileType::Doc => true,
            FileType::Sheet => true,
            FileType::Wiki => true,
        }
    }
}

/// 订阅状态
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub enum SubscriptionStatus {
    /// 已订阅
    Subscribed,
    /// 未订阅
    Unsubscribed,
    /// 已暂停
    Paused,
    /// 已取消
    Cancelled,
    /// 未知状态
    #[serde(other)]
    Unknown,
}

impl SubscriptionStatus {
    /// 是否为活跃状态
    pub fn is_active(&self) -> bool {
        matches!(self, SubscriptionStatus::Subscribed)
    }

    /// 是否可以激活
    pub fn can_activate(&self) -> bool {
        matches!(
            self,
            SubscriptionStatus::Unsubscribed | SubscriptionStatus::Paused
        )
    }

    /// 获取状态描述
    pub fn description(&self) -> &'static str {
        match self {
            SubscriptionStatus::Subscribed => "已订阅",
            SubscriptionStatus::Unsubscribed => "未订阅",
            SubscriptionStatus::Paused => "已暂停",
            SubscriptionStatus::Cancelled => "已取消",
            SubscriptionStatus::Unknown => "未知状态",
        }
    }

    /// 获取状态颜色(用于UI显示)
    pub fn color(&self) -> &'static str {
        match self {
            SubscriptionStatus::Subscribed => "green",
            SubscriptionStatus::Unsubscribed => "gray",
            SubscriptionStatus::Paused => "orange",
            SubscriptionStatus::Cancelled => "red",
            SubscriptionStatus::Unknown => "gray",
        }
    }
}

/// 订阅详情
#[derive(Debug, Deserialize, Clone)]
pub struct SubscriptionDetail {
    /// 订阅状态
    pub status: SubscriptionStatus,
    /// 订阅开始时间(时间戳)
    pub start_time: Option<i64>,
    /// 订阅结束时间(时间戳)
    pub end_time: Option<i64>,
    /// 最后更新时间(时间戳)
    pub last_update_time: Option<i64>,
    /// 订阅者ID
    pub subscriber_id: Option<String>,
    /// 订阅者类型
    pub subscriber_type: Option<String>,
    /// 订阅配置
    pub config: Option<serde_json::Value>,
    /// 扩展信息
    pub extra: Option<serde_json::Value>,
}

/// 获取订阅状态响应
#[derive(Debug, Deserialize)]
pub struct GetSubscriptionResponse {
    /// 订阅详情
    pub subscription: SubscriptionDetail,
    /// 文档token
    pub file_token: String,
    /// 文档类型
    pub file_type: String,
}

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

/// 获取订阅状态
pub async fn get_subscription(
    request: GetSubscriptionRequest,
    config: &Config,
    option: Option<RequestOption>,
) -> SDKResult<BaseResponse<GetSubscriptionResponse>> {
    let mut api_req = request.api_request;
    api_req.http_method = Method::GET;

    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 SubscriptionDetail {
    /// 是否已订阅
    pub fn is_subscribed(&self) -> bool {
        self.status.is_active()
    }

    /// 是否可以激活订阅
    pub fn can_activate(&self) -> bool {
        self.status.can_activate()
    }

    /// 获取订阅持续时间(秒)
    pub fn duration_seconds(&self) -> Option<i64> {
        match (self.start_time, self.end_time) {
            (Some(start), Some(end)) => Some(end - start),
            _ => None,
        }
    }

    /// 获取订阅持续时间(天)
    pub fn duration_days(&self) -> Option<f64> {
        self.duration_seconds().map(|s| s as f64 / 86400.0)
    }

    /// 获取订阅时间格式化字符串
    pub fn start_time_formatted(&self) -> Option<String> {
        self.start_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 end_time_formatted(&self) -> Option<String> {
        self.end_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 last_update_time_formatted(&self) -> Option<String> {
        self.last_update_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 summary(&self) -> String {
        let mut parts = Vec::new();

        parts.push(format!("状态: {}", self.status.description()));

        if let Some(start) = self.start_time_formatted() {
            parts.push(format!("开始时间: {start}"));
        }

        if let Some(end) = self.end_time_formatted() {
            parts.push(format!("结束时间: {end}"));
        }

        if let Some(days) = self.duration_days() {
            parts.push(format!("持续时间: {days:.1}"));
        }

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

        parts.join(" | ")
    }
}

impl GetSubscriptionResponse {
    /// 获取文档类型枚举
    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 info_summary(&self) -> String {
        format!(
            "{} ({}) - {}",
            self.file_type_enum().chinese_name(),
            self.file_token,
            self.subscription.summary()
        )
    }
}

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

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

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

    #[test]
    fn test_file_type_methods() {
        assert_eq!(FileType::Doc.to_string(), "doc");
        assert_eq!(FileType::Doc.chinese_name(), "文档");
        assert!(FileType::Doc.supports_assistant());
    }

    #[test]
    fn test_subscription_status_methods() {
        assert!(SubscriptionStatus::Subscribed.is_active());
        assert!(!SubscriptionStatus::Unsubscribed.is_active());
        assert!(SubscriptionStatus::Unsubscribed.can_activate());
        assert!(!SubscriptionStatus::Subscribed.can_activate());

        assert_eq!(SubscriptionStatus::Subscribed.description(), "已订阅");
        assert_eq!(SubscriptionStatus::Subscribed.color(), "green");
    }

    #[test]
    fn test_subscription_detail_methods() {
        let detail = SubscriptionDetail {
            status: SubscriptionStatus::Subscribed,
            start_time: Some(1700000000),
            end_time: Some(1700086400),
            last_update_time: Some(1700043200),
            subscriber_id: Some("user123".to_string()),
            subscriber_type: Some("user".to_string()),
            config: None,
            extra: None,
        };

        assert!(detail.is_subscribed());
        assert!(!detail.can_activate());
        assert_eq!(detail.duration_seconds(), Some(86400));
        assert_eq!(detail.duration_days(), Some(1.0));
    }
}