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
use std::fmt::Debug;

use log::error;
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,
};

pub struct ExplorerService {
    config: Config,
}

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

    /// GET /open-apis/drive/explorer/v2/root_folder/meta
    ///
    /// 获取云空间的根目录
    pub async fn root_folder_meta(
        &self,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<ExplorerRootMeta>> {
        let api_req = ApiRequest {
            http_method: Method::GET,
            api_path: "/open-apis/drive/explorer/v2/root_folder/meta".to_string(),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            ..Default::default()
        };

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

        Ok(api_resp)
    }

    /// GET /open-apis/drive/explorer/v2/folder/:folderToken/meta
    ///
    /// 获取文件夹的元信息
    pub async fn folder_meta(
        &self,
        folder_token: &str,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<ExplorerFolderMeta>> {
        let api_req = ApiRequest {
            http_method: Method::GET,
            api_path: format!("/open-apis/drive/explorer/v2/folder/{folder_token}/meta"),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            ..Default::default()
        };

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

        Ok(api_resp)
    }

    /// POST /open-apis/drive/v1/files/create_folder
    /// 新建文件夹
    pub async fn create_folder(
        &self,
        create_folder_request: CreateFolderRequest,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<CreateFolderResponse>> {
        let mut api_req = create_folder_request.api_req;
        api_req.http_method = Method::POST;
        api_req.api_path = "/open-apis/drive/v1/files/create_folder".to_string();
        api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];

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

        Ok(api_resp)
    }

    /// 获取文件夹下的清单
    ///
    /// <https://open.feishu.cn/open-apis/drive/v1/files>
    pub async fn list_folder(
        &self,
        list_folder_request: ListFolderRequest,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<ListFolderResponse>> {
        let mut api_req = list_folder_request.api_req;
        api_req.http_method = Method::GET;
        api_req.api_path = "/open-apis/drive/v1/files".to_string();
        api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];

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

        Ok(api_resp)
    }

    pub fn list_folder_iter(
        &self,
        req: ListFolderRequest,
        option: Option<RequestOption>,
    ) -> ListFolderIterator {
        ListFolderIterator {
            explorer_service: self,
            req,
            option,
            has_more: true,
        }
    }
}

pub struct ListFolderIterator<'a> {
    explorer_service: &'a ExplorerService,
    req: ListFolderRequest,
    option: Option<RequestOption>,
    has_more: bool,
}

impl<'a> ListFolderIterator<'a> {
    pub async fn next(&mut self) -> Option<Vec<FileInFolder>> {
        if !self.has_more {
            return None;
        }

        match self
            .explorer_service
            .list_folder(self.req.clone(), self.option.clone())
            .await
        {
            Ok(resp) => match resp.data {
                Some(data) => {
                    self.has_more = data.has_more;
                    if data.has_more {
                        self.req
                            .api_req
                            .query_params
                            .insert("page_token".to_string(), data.next_page_token.unwrap());
                        Some(data.files)
                    } else if data.files.is_empty() {
                        None
                    } else {
                        Some(data.files)
                    }
                }
                None => None,
            },
            Err(e) => {
                error!("Error: {:?}", e);
                None
            }
        }
    }
}

/// 我的空间(root folder)元信息
#[derive(Debug, Serialize, Deserialize)]
pub struct ExplorerRootMeta {
    /// 文件夹的 token
    pub token: String,
    /// 文件夹的 id
    pub id: String,
    /// 文件夹的所有者 id
    pub user_id: String,
}

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

/// 文件夹元信息
#[derive(Debug, Serialize, Deserialize)]
pub struct ExplorerFolderMeta {
    /// 文件夹的 id
    pub id: String,
    /// 文件夹的标题
    pub name: String,
    /// 文件夹的 token
    pub token: String,
    /// 文件夹的创建者 id
    #[serde(rename = "createUid")]
    pub create_uid: String,
    /// 文件夹的最后编辑者 id
    #[serde(rename = "editUid")]
    pub edit_uid: String,
    /// 文件夹的上级目录 id
    #[serde(rename = "parentId")]
    pub parent_id: String,
    /// 文件夹为个人文件夹时,为文件夹的所有者 id;文件夹为共享文件夹时,为文件夹树id
    #[serde(rename = "ownUid")]
    pub own_uid: String,
}

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

#[derive(Default, Serialize, Deserialize)]
pub struct CreateFolderRequest {
    /// 请求体
    #[serde(skip)]
    api_req: ApiRequest,
    /// 文件夹名称
    ///
    /// 示例值:"New Folder"
    name: String,
    /// 父文件夹token。如果需要创建到「我的空间」作为顶级文件夹,请传入我的空间token
    folder_token: String,
}

impl CreateFolderRequest {
    pub fn builder() -> CreateFolderRequestBuilder {
        CreateFolderRequestBuilder::default()
    }
}

#[derive(Default)]
/// 创建文件夹请求体
pub struct CreateFolderRequestBuilder {
    request: CreateFolderRequest,
}

impl CreateFolderRequestBuilder {
    /// 文件夹名称
    pub fn name(mut self, name: impl ToString) -> Self {
        self.request.name = name.to_string();
        self
    }

    /// 父文件夹token
    pub fn folder_token(mut self, folder_token: impl ToString) -> Self {
        self.request.folder_token = folder_token.to_string();
        self
    }

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

        self.request
    }
}

/// 创建文件夹响应体
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateFolderResponse {
    /// 创建文件夹的token
    pub token: String,
    /// 创建文件夹的访问url
    pub url: String,
}

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

/// 列出文件夹请求体
#[derive(Default)]
pub struct ListFolderRequestBuilder {
    request: ListFolderRequest,
}

impl ListFolderRequestBuilder {
    /// 页大小
    ///
    /// 示例值:50
    ///
    /// 数据校验规则:
    ///
    /// 最大值:200
    pub fn page_size(mut self, page_size: i32) -> Self {
        self.request
            .api_req
            .query_params
            .insert("page_size".to_string(), page_size.to_string());
        self
    }

    /// 分页标记,第一次请求不填,表示从头开始遍历;分页查询结果还有更多项时会同时返回新的
    /// page_token,下次遍历可采用该 page_token 获取查询结果
    ///
    /// 示例值:"MTY1NTA3MTA1OXw3MTA4NDc2MDc1NzkyOTI0Nabcef"
    pub fn page_token(mut self, page_token: impl ToString) -> Self {
        self.request
            .api_req
            .query_params
            .insert("page_token".to_string(), page_token.to_string());
        self
    }

    /// 文件夹的token(若不填写该参数或填写空字符串,则默认获取用户云空间下的清单,且不支持分页)
    ///
    /// 示例值:"fldbcO1UuPz8VwnpPx5a9abcef"
    pub fn folder_token(mut self, fold_token: impl ToString) -> Self {
        self.request
            .api_req
            .query_params
            .insert("folder_token".to_string(), fold_token.to_string());
        self
    }

    /// 排序规则
    ///
    /// 示例值:"EditedTime"
    ///
    /// 可选值有:
    ///
    /// - EditedTime:编辑时间排序
    /// - CreatedTime:创建时间排序
    ///
    /// 默认值:EditedTime
    pub fn order_by(mut self, order_by: impl ToString) -> Self {
        self.request
            .api_req
            .query_params
            .insert("order_by".to_string(), order_by.to_string());
        self
    }

    /// 升序降序
    ///
    /// 默认值:DESC
    ///
    /// 可选值有:
    ///
    /// - ASC:升序
    /// - DESC:降序
    pub fn direction(mut self, direction: impl ToString) -> Self {
        self.request
            .api_req
            .query_params
            .insert("direction".to_string(), direction.to_string());
        self
    }

    /// 用户 ID 类型
    ///
    /// 默认值:open_id
    ///
    /// 可选值有:
    ///
    /// - open_id:标识一个用户在某个应用中的身份。同一个用户在不同应用中的 Open ID
    ///   不同。了解更多:如何获取 Open ID
    /// - union_id:标识一个用户在某个应用开发商下的身份。同一用户在同一开发商下的应用中的 Union ID
    ///   是相同的,在不同开发商下的应用中的 Union ID 是不同的。通过 Union
    ///   ID,应用开发商可以把同个用户在多个应用中的身份关联起来。了解更多:如何获取 Union ID?
    /// - user_id:标识一个用户在某个租户内的身份。同一个用户在租户 A 和租户 B 内的 User ID
    ///   是不同的。在同一个租户内,一个用户的 User ID 在所有应用(包括商店应用)中都保持一致。User
    ///   ID 主要用于在不同的应用间打通用户数据。了解更多:如何获取 User ID?

    pub fn user_id_type(mut self, user_id_type: impl ToString) -> Self {
        self.request
            .api_req
            .query_params
            .insert("user_id_type".to_string(), user_id_type.to_string());
        self
    }

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

/// 列出文件夹查询参数
#[derive(Default, Clone, Serialize, Deserialize)]
pub struct ListFolderRequest {
    /// 请求体
    #[serde(skip)]
    api_req: ApiRequest,
}

impl ListFolderRequest {
    pub fn builder() -> ListFolderRequestBuilder {
        ListFolderRequestBuilder::default()
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ListFolderResponse {
    /// 文件夹列表
    pub files: Vec<FileInFolder>,
    /// 分页标记,当 has_more 为 true 时,会同时返回下一次遍历的page_token,否则则不返回
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_page_token: Option<String>,
    /// 是否还有更多项
    pub has_more: bool,
}

/// 文件夹清单列表
#[derive(Debug, Serialize, Deserialize)]
pub struct FileInFolder {
    /// 文件标识
    pub token: String,
    /// 文件名
    pub name: String,
    /// 文件类型
    ///
    /// 可选值有:
    ///
    /// - doc:旧版文档
    ///
    /// - sheet:表格
    ///
    /// - mindnote:思维导图
    //
    /// - bitable:多维表格
    ///
    /// - file:文件
    ///
    /// - docx:新版文档
    ///
    /// - folder:文件夹
    ///
    /// - shortcut: 快捷方式
    pub r#type: String,
    /// 父文件夹标识
    pub parent_token: String,
    /// 在浏览器中查看的链接
    pub url: String,
    /// 快捷方式文件信息
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shortcut_info: Option<ShortcutInfo>,
    /// 文件创建时间
    pub created_time: String,
    /// 文件最近修改时间
    pub modified_time: String,
    /// 文件所有者
    pub owner_id: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ShortcutInfo {
    /// 快捷方式指向的原文件类型
    ///
    /// 可选值有:
    ///
    /// - doc:旧版文档
    ///
    /// - sheet:表格
    ///
    /// - mindnote:思维导图
    ///
    /// - bitable:多维表格
    ///
    /// - file:文件
    ///
    /// - docx:新版文档
    pub target_type: String,
    /// 快捷方式指向的原文件token
    pub target_token: String,
}

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