openlark-docs 0.16.1

飞书开放平台云文档服务模块 - 文档、表格、知识库API (202 APIs, 100% 覆盖,不含旧版本)
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
#![allow(clippy::empty_line_after_doc_comments)]

/// CCM Drive Explorer V2 API 模块
///
/// 云盘浏览器API实现,包含8个API:
/// - root_folder_meta: 获取我的空间(根文件夹)元数据
/// - folder_meta: 获取文件夹元数据
/// - file: 新建文件
/// - file_copy: 复制文档
/// - file_docs: 删除Doc
/// - file_spreadsheets: 删除Sheet
/// - folder_children: 获取文件夹下的文档清单
/// - folder: 新建文件夹
use openlark_core::{
    SDKResult,
    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
    config::Config,
    http::Transport,
    req_option::RequestOption,
    validate_required,
};
use serde::Deserialize;

use crate::common::{
    api_endpoints::{CcmDriveExplorerApiOld, DriveApi},
    api_utils::*,
};

#[derive(Debug, Clone, Deserialize)]
struct DriveListFilesData {
    files: Vec<DriveListFileItem>,
    next_page_token: Option<String>,
    has_more: bool,
}

#[derive(Debug, Clone, Deserialize)]
struct DriveListFileItem {
    token: String,
    name: String,
    r#type: String,
    created_time: Option<String>,
    modified_time: Option<String>,
}

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

/// Explorer v2 模型模块。
pub mod models;

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

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

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

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

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

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

#[derive(Debug, Clone)]
/// 获取根文件夹元信息请求构建器。
pub struct GetRootFolderMetaRequest {
    config: Config,
}

impl GetRootFolderMetaRequest {
    /// 创建获取根文件夹元信息请求。
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// 使用默认请求选项执行请求。
    pub async fn execute(self) -> SDKResult<FolderMetaResponse> {
        self.execute_with_options(RequestOption::default()).await
    }

    /// 使用指定请求选项执行请求。
    pub async fn execute_with_options(
        self,
        option: RequestOption,
    ) -> SDKResult<FolderMetaResponse> {
        let api_endpoint = CcmDriveExplorerApiOld::RootFolderMeta;
        let api_request: ApiRequest<FolderMetaResponse> = ApiRequest::get(&api_endpoint.to_url());
        let response = Transport::request(api_request, &self.config, Some(option)).await?;
        extract_response_data(response, "获取根文件夹元信息")
    }
}

#[derive(Debug, Clone)]
/// 获取文件夹元信息请求构建器。
pub struct GetFolderMetaRequest {
    config: Config,
    folder_token: String,
}

impl GetFolderMetaRequest {
    /// 创建获取文件夹元信息请求。
    pub fn new(config: Config, folder_token: impl Into<String>) -> Self {
        Self {
            config,
            folder_token: folder_token.into(),
        }
    }

    /// 使用默认请求选项执行请求。
    pub async fn execute(self) -> SDKResult<FolderMetaResponse> {
        self.execute_with_options(RequestOption::default()).await
    }

    /// 使用指定请求选项执行请求。
    pub async fn execute_with_options(
        self,
        option: RequestOption,
    ) -> SDKResult<FolderMetaResponse> {
        validate_required!(self.folder_token.trim(), "文件夹Token不能为空");

        let api_endpoint = CcmDriveExplorerApiOld::FolderMeta(self.folder_token);
        let api_request: ApiRequest<FolderMetaResponse> = ApiRequest::get(&api_endpoint.to_url());
        let response = Transport::request(api_request, &self.config, Some(option)).await?;
        extract_response_data(response, "获取文件夹元信息")
    }
}

#[derive(Debug, Clone)]
/// 新建文件请求构建器。
pub struct CreateFileRequest {
    config: Config,
    folder_token: String,
    params: CreateFileParams,
}

impl CreateFileRequest {
    /// 创建新建文件请求。
    pub fn new(config: Config, folder_token: impl Into<String>, params: CreateFileParams) -> Self {
        Self {
            config,
            folder_token: folder_token.into(),
            params,
        }
    }

    /// 使用默认请求选项执行请求。
    pub async fn execute(self) -> SDKResult<CreateFileResponse> {
        self.execute_with_options(RequestOption::default()).await
    }

    /// 使用指定请求选项执行请求。
    pub async fn execute_with_options(
        self,
        option: RequestOption,
    ) -> SDKResult<CreateFileResponse> {
        validate_required!(self.folder_token.trim(), "文件夹Token不能为空");
        validate_required!(self.params.title.trim(), "文件标题不能为空");
        validate_required!(self.params.parent_type.trim(), "文件类型不能为空");

        let api_endpoint = CcmDriveExplorerApiOld::File(self.folder_token);
        let api_request: ApiRequest<CreateFileResponse> = ApiRequest::post(&api_endpoint.to_url())
            .body(serialize_params(&self.params, "新建文件")?);

        let response = Transport::request(api_request, &self.config, Some(option)).await?;
        extract_response_data(response, "新建文件")
    }
}

#[derive(Debug, Clone)]
/// 复制文件请求构建器。
pub struct CopyFileRequest {
    config: Config,
    file_token: String,
    params: CopyFileParams,
}

impl CopyFileRequest {
    /// 创建复制文件请求。
    pub fn new(config: Config, file_token: impl Into<String>, params: CopyFileParams) -> Self {
        Self {
            config,
            file_token: file_token.into(),
            params,
        }
    }

    /// 使用默认请求选项执行请求。
    pub async fn execute(self) -> SDKResult<CopyFileResponse> {
        self.execute_with_options(RequestOption::default()).await
    }

    /// 使用指定请求选项执行请求。
    pub async fn execute_with_options(self, option: RequestOption) -> SDKResult<CopyFileResponse> {
        validate_required!(self.file_token.trim(), "文件Token不能为空");
        validate_required!(self.params.folder_token.trim(), "目标文件夹Token不能为空");

        let api_endpoint = CcmDriveExplorerApiOld::FileCopy(self.file_token);
        let api_request: ApiRequest<CopyFileResponse> = ApiRequest::post(&api_endpoint.to_url())
            .body(serialize_params(&self.params, "复制文档")?);

        let response = Transport::request(api_request, &self.config, Some(option)).await?;
        extract_response_data(response, "复制文档")
    }
}

#[derive(Debug, Clone)]
/// 删除 Doc 请求构建器。
pub struct DeleteDocRequest {
    config: Config,
    doc_token: String,
}

impl DeleteDocRequest {
    /// 创建删除 Doc 请求。
    pub fn new(config: Config, doc_token: impl Into<String>) -> Self {
        Self {
            config,
            doc_token: doc_token.into(),
        }
    }

    /// 使用默认请求选项执行请求。
    pub async fn execute(self) -> SDKResult<DeleteFileResponse> {
        self.execute_with_options(RequestOption::default()).await
    }

    /// 使用指定请求选项执行请求。
    pub async fn execute_with_options(
        self,
        option: RequestOption,
    ) -> SDKResult<DeleteFileResponse> {
        validate_required!(self.doc_token.trim(), "文档Token不能为空");

        let api_endpoint = CcmDriveExplorerApiOld::FileDocs(self.doc_token);
        let api_request: ApiRequest<DeleteFileResponse> =
            ApiRequest::delete(&api_endpoint.to_url());

        let response = Transport::request(api_request, &self.config, Some(option)).await?;
        extract_response_data(response, "删除Doc")
    }
}

#[derive(Debug, Clone)]
/// 删除 Sheet 请求构建器。
pub struct DeleteSheetRequest {
    config: Config,
    spreadsheet_token: String,
}

impl DeleteSheetRequest {
    /// 创建删除 Sheet 请求。
    pub fn new(config: Config, spreadsheet_token: impl Into<String>) -> Self {
        Self {
            config,
            spreadsheet_token: spreadsheet_token.into(),
        }
    }

    /// 使用默认请求选项执行请求。
    pub async fn execute(self) -> SDKResult<DeleteFileResponse> {
        self.execute_with_options(RequestOption::default()).await
    }

    /// 使用指定请求选项执行请求。
    pub async fn execute_with_options(
        self,
        option: RequestOption,
    ) -> SDKResult<DeleteFileResponse> {
        validate_required!(self.spreadsheet_token.trim(), "表格Token不能为空");

        let api_endpoint = CcmDriveExplorerApiOld::FileSpreadsheets(self.spreadsheet_token);
        let api_request: ApiRequest<DeleteFileResponse> =
            ApiRequest::delete(&api_endpoint.to_url());

        let response = Transport::request(api_request, &self.config, Some(option)).await?;
        extract_response_data(response, "删除Sheet")
    }
}

#[derive(Debug, Clone)]
/// 获取文件夹子项列表请求构建器。
pub struct GetFolderChildrenRequest {
    config: Config,
    folder_token: String,
    params: Option<GetFolderChildrenParams>,
}

impl GetFolderChildrenRequest {
    /// 创建获取文件夹子项列表请求。
    pub fn new(
        config: Config,
        folder_token: impl Into<String>,
        params: Option<GetFolderChildrenParams>,
    ) -> Self {
        Self {
            config,
            folder_token: folder_token.into(),
            params,
        }
    }

    /// 使用默认请求选项执行请求。
    pub async fn execute(self) -> SDKResult<GetFolderChildrenResponse> {
        self.execute_with_options(RequestOption::default()).await
    }

    /// 使用指定请求选项执行请求。
    pub async fn execute_with_options(
        self,
        option: RequestOption,
    ) -> SDKResult<GetFolderChildrenResponse> {
        validate_required!(self.folder_token.trim(), "文件夹Token不能为空");

        let mut api_request: ApiRequest<DriveListFilesData> =
            ApiRequest::get(&DriveApi::ListFiles.to_url()).query("folder_token", self.folder_token);

        if let Some(params) = self.params {
            api_request =
                api_request.query_opt("page_size", params.page_size.map(|v| v.to_string()));
            api_request = api_request.query_opt("page_token", params.page_token);
            if let Some(doc_type) = params.doc_type {
                api_request = api_request.query("type", doc_type);
            }
        }

        let response = Transport::request(api_request, &self.config, Some(option)).await?;
        let data: DriveListFilesData = extract_response_data(response, "获取文件夹子项")?;

        let items = data
            .files
            .into_iter()
            .map(|item| {
                let doc_type = item.r#type;
                let is_folder = doc_type == "folder";
                FileItem {
                    file_token: item.token,
                    title: item.name,
                    doc_type,
                    is_folder,
                    create_time: item
                        .created_time
                        .as_deref()
                        .unwrap_or("0")
                        .parse::<i64>()
                        .unwrap_or(0),
                    update_time: item
                        .modified_time
                        .as_deref()
                        .unwrap_or("0")
                        .parse::<i64>()
                        .unwrap_or(0),
                }
            })
            .collect::<Vec<_>>();

        Ok(GetFolderChildrenResponse {
            data: Some(FolderChildrenData {
                items,
                has_more: data.has_more,
                page_token: data.next_page_token,
            }),
        })
    }
}

#[derive(Debug, Clone)]
/// 新建文件夹请求构建器。
pub struct CreateFolderRequest {
    config: Config,
    folder_token: String,
    params: CreateFolderParams,
}

impl CreateFolderRequest {
    /// 创建新建文件夹请求。
    pub fn new(
        config: Config,
        folder_token: impl Into<String>,
        params: CreateFolderParams,
    ) -> Self {
        Self {
            config,
            folder_token: folder_token.into(),
            params,
        }
    }

    /// 使用默认请求选项执行请求。
    pub async fn execute(self) -> SDKResult<CreateFolderResponse> {
        self.execute_with_options(RequestOption::default()).await
    }

    /// 使用指定请求选项执行请求。
    pub async fn execute_with_options(
        self,
        option: RequestOption,
    ) -> SDKResult<CreateFolderResponse> {
        validate_required!(self.folder_token.trim(), "父文件夹Token不能为空");
        validate_required!(self.params.title.trim(), "文件夹标题不能为空");

        let api_endpoint = CcmDriveExplorerApiOld::Folder(self.folder_token);
        let api_request: ApiRequest<CreateFolderResponse> =
            ApiRequest::post(&api_endpoint.to_url())
                .body(serialize_params(&self.params, "新建文件夹")?);

        let response = Transport::request(api_request, &self.config, Some(option)).await?;
        extract_response_data(response, "新建文件夹")
    }
}

/// 获取我的空间(根文件夹)元数据
///
/// 获取 "我的空间" 的元信息。
/// docPath: /document/server-docs/docs/drive-v1/folder/get-root-folder-meta
pub use models::{
    CopyFileParams, CopyFileResponse, CopyResult, CreateFileParams, CreateFileResponse,
    CreateFolderParams, CreateFolderResponse, DeleteFileResponse, DeleteResult, FileInfo, FileItem,
    FolderChildrenData, FolderMeta, FolderMetaResponse, GetFolderChildrenParams,
    GetFolderChildrenResponse, NewFolderInfo, UserInfo,
};