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

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

/// 文档服务
pub struct DocumentService {
    config: Config,
}

impl DocumentService {
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// 创建文档
    ///
    /// 该接口用于创建一个新的文档。
    ///
    /// <https://open.feishu.cn/document/server-docs/docs/docs/docx-v1/document/create>
    pub async fn create(
        &self,
        request: CreateDocumentRequest,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<CreateDocumentRespData>> {
        let api_req = ApiRequest {
            http_method: Method::POST,
            api_path: "/open-apis/docx/v1/documents".to_string(),
            supported_access_token_types: vec![AccessTokenType::User, AccessTokenType::Tenant],
            body: serde_json::to_vec(&request)?,
            ..Default::default()
        };

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

    /// 获取文档基本信息
    ///
    /// 该接口用于获取文档的基本信息。
    ///
    /// <https://open.feishu.cn/document/server-docs/docs/docs/docx-v1/document/get>
    pub async fn get(
        &self,
        document_id: impl Into<String>,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<GetDocumentRespData>> {
        let api_req = ApiRequest {
            http_method: Method::GET,
            api_path: format!("/open-apis/docx/v1/documents/{}", document_id.into()),
            supported_access_token_types: vec![AccessTokenType::User, AccessTokenType::Tenant],
            ..Default::default()
        };

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

    /// 获取文档纯文本内容
    ///
    /// 该接口用于获取文档的纯文本内容。
    ///
    /// <https://open.feishu.cn/document/server-docs/docs/docs/docx-v1/document/raw_content>
    pub async fn get_raw_content(
        &self,
        document_id: impl Into<String>,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<GetRawContentRespData>> {
        let mut api_req = ApiRequest {
            http_method: Method::GET,
            api_path: format!(
                "/open-apis/docx/v1/documents/{}/raw_content",
                document_id.into()
            ),
            ..Default::default()
        };
        api_req.supported_access_token_types = vec![AccessTokenType::User, AccessTokenType::Tenant];

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

    /// 获取文档所有块
    ///
    /// 该接口用于获取文档的所有块。
    ///
    /// <https://open.feishu.cn/document/server-docs/docs/docs/docx-v1/document/list>
    pub async fn list_blocks(
        &self,
        request: ListDocumentBlocksRequest,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<ListDocumentBlocksRespData>> {
        let mut api_req = ApiRequest {
            http_method: Method::GET,
            api_path: format!(
                "/open-apis/docx/v1/documents/{}/blocks",
                request.document_id
            ),
            ..Default::default()
        };
        api_req.supported_access_token_types = vec![AccessTokenType::User, AccessTokenType::Tenant];

        // 添加查询参数
        if let Some(page_size) = request.page_size {
            api_req
                .query_params
                .insert("page_size".to_string(), page_size.to_string());
        }
        if let Some(page_token) = request.page_token {
            api_req
                .query_params
                .insert("page_token".to_string(), page_token);
        }

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

    /// 转换为文档块
    ///
    /// 该接口用于将旧版文档转换为新版文档块格式。
    ///
    /// <https://open.feishu.cn/document/ukTMukTMukTM/uUDN04SN0QjL1QDN/document-docx/docx-v1/document/convert>
    pub async fn convert_to_docx(
        &self,
        document_id: impl Into<String>,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<ConvertToDocxRespData>> {
        let mut api_req = ApiRequest {
            http_method: Method::POST,
            api_path: format!(
                "/open-apis/docx/v1/documents/{}/convert",
                document_id.into()
            ),
            ..Default::default()
        };
        api_req.supported_access_token_types = vec![AccessTokenType::User, AccessTokenType::Tenant];

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

// === 数据结构定义 ===

/// 创建文档请求参数
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CreateDocumentRequest {
    /// 文档标题
    pub title: String,
    /// 文档内容,JSON格式的块内容
    pub content: Option<String>,
    /// 文件夹token
    pub folder_token: Option<String>,
}

impl CreateDocumentRequest {
    pub fn builder() -> CreateDocumentRequestBuilder {
        CreateDocumentRequestBuilder::default()
    }

    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            content: None,
            folder_token: None,
        }
    }

    pub fn with_content(mut self, content: impl Into<String>) -> Self {
        self.content = Some(content.into());
        self
    }

    pub fn with_folder_token(mut self, folder_token: impl Into<String>) -> Self {
        self.folder_token = Some(folder_token.into());
        self
    }
}

/// 创建文档请求构建器
#[derive(Default)]
pub struct CreateDocumentRequestBuilder {
    request: CreateDocumentRequest,
}

impl CreateDocumentRequestBuilder {
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.request.title = title.into();
        self
    }

    pub fn content(mut self, content: impl Into<String>) -> Self {
        self.request.content = Some(content.into());
        self
    }

    pub fn folder_token(mut self, folder_token: impl Into<String>) -> Self {
        self.request.folder_token = Some(folder_token.into());
        self
    }

    pub fn build(self) -> CreateDocumentRequest {
        self.request
    }
}

impl_executable_builder_owned!(
    CreateDocumentRequestBuilder,
    DocumentService,
    CreateDocumentRequest,
    BaseResponse<CreateDocumentRespData>,
    create
);

/// 创建文档响应数据
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateDocumentRespData {
    /// 文档ID
    pub document_id: String,
    /// 文档版本ID
    pub document_revision_id: i64,
    /// 文档标题
    pub title: String,
}

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

/// 获取文档信息响应数据
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetDocumentRespData {
    /// 文档信息
    pub document: DocumentInfo,
}

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

/// 文档信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentInfo {
    /// 文档ID
    pub document_id: String,
    /// 文档版本ID
    pub document_revision_id: i64,
    /// 文档标题
    pub title: String,
    /// 创建时间
    pub create_time: String,
    /// 更新时间
    pub update_time: String,
    /// 创建者ID
    pub creator_id: String,
    /// 最后编辑者ID
    pub last_editor_id: String,
}

/// 获取纯文本内容响应数据
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetRawContentRespData {
    /// 纯文本内容
    pub content: String,
}

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

/// 获取文档所有块请求参数
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ListDocumentBlocksRequest {
    /// 文档ID
    pub document_id: String,
    /// 分页大小
    pub page_size: Option<i32>,
    /// 分页标记
    pub page_token: Option<String>,
}

impl ListDocumentBlocksRequest {
    pub fn builder() -> ListDocumentBlocksRequestBuilder {
        ListDocumentBlocksRequestBuilder::default()
    }

    pub fn new(document_id: impl Into<String>) -> Self {
        Self {
            document_id: document_id.into(),
            page_size: None,
            page_token: None,
        }
    }

    pub fn with_page_size(mut self, page_size: i32) -> Self {
        self.page_size = Some(page_size);
        self
    }

    pub fn with_page_token(mut self, page_token: impl Into<String>) -> Self {
        self.page_token = Some(page_token.into());
        self
    }
}

/// 获取文档所有块请求构建器
#[derive(Default)]
pub struct ListDocumentBlocksRequestBuilder {
    request: ListDocumentBlocksRequest,
}

impl ListDocumentBlocksRequestBuilder {
    pub fn document_id(mut self, document_id: impl Into<String>) -> Self {
        self.request.document_id = document_id.into();
        self
    }

    pub fn page_size(mut self, page_size: i32) -> Self {
        self.request.page_size = Some(page_size);
        self
    }

    pub fn page_token(mut self, page_token: impl Into<String>) -> Self {
        self.request.page_token = Some(page_token.into());
        self
    }

    pub fn build(self) -> ListDocumentBlocksRequest {
        self.request
    }
}

impl_executable_builder_owned!(
    ListDocumentBlocksRequestBuilder,
    DocumentService,
    ListDocumentBlocksRequest,
    BaseResponse<ListDocumentBlocksRespData>,
    list_blocks
);

/// 获取文档所有块响应数据
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListDocumentBlocksRespData {
    /// 块列表
    pub items: Vec<Block>,
    /// 是否还有更多数据
    pub has_more: bool,
    /// 下一页标记
    pub page_token: Option<String>,
}

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

/// 块信息
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Block {
    /// 块ID
    pub block_id: String,
    /// 父块ID
    pub parent_id: String,
    /// 子块ID列表
    pub children: Vec<String>,
    /// 块类型
    pub block_type: i32,
    /// 块索引
    pub index: i32,
}

/// 转换为文档块响应数据
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConvertToDocxRespData {
    /// 新文档ID
    pub document_id: String,
    /// 文档版本ID
    pub document_revision_id: i64,
}

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