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
//! 添加协作者
//!
//! 为文件或文件夹添加协作者权限。
//!
//! docPath: https://open.feishu.cn/document/server-docs/docs/permission/permission-member/create

use openlark_core::{
    SDKResult,
    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
    config::Config,
    http::Transport,
    validate_required,
};
use serde::{Deserialize, Serialize};

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

use super::models::PermissionMember;

/// 添加协作者请求
#[derive(Debug, Clone)]
pub struct CreatePermissionMemberRequest {
    config: Config,
    /// 文件token
    pub token: String,
    /// 云文档类型(query 参数 `type`,需要与 token 匹配)
    pub file_type: String,
    /// 是否发送通知(query 参数 `need_notification`,默认 false)
    pub need_notification: Option<bool>,

    /// 协作者 ID 类型,与 member_id 对应
    pub member_type: String,
    /// 协作者 ID
    pub member_id: String,
    /// 权限角色
    pub perm: String,
    /// 权限角色类型(默认 container)
    pub perm_type: Option<String>,
    /// 协作者类型(仅当 member_type 为 wikispaceid 时必填)
    pub member_kind: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
struct CreatePermissionMemberRequestBody {
    member_type: String,
    member_id: String,
    perm: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    perm_type: Option<String>,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    r#type: Option<String>,
}

impl CreatePermissionMemberRequest {
    /// 创建新的添加协作者请求。
    /// 创建添加协作者请求
    ///
    /// # 参数
    /// * `config` - 配置
    /// * `token` - 文件token
    /// * `file_type` - 云文档类型(query 参数 `type`)
    /// * `member_type` - 协作者 ID 类型
    /// * `member_id` - 协作者 ID
    /// * `perm` - 权限角色
    pub fn new(
        config: Config,
        token: impl Into<String>,
        file_type: impl Into<String>,
        member_type: impl Into<String>,
        member_id: impl Into<String>,
        perm: impl Into<String>,
    ) -> Self {
        Self {
            config,
            token: token.into(),
            file_type: file_type.into(),
            need_notification: None,
            member_type: member_type.into(),
            member_id: member_id.into(),
            perm: perm.into(),
            perm_type: None,
            member_kind: None,
        }
    }

    /// 设置是否发送通知(默认 false)
    pub fn need_notification(mut self, need_notification: bool) -> Self {
        self.need_notification = Some(need_notification);
        self
    }

    /// 设置权限角色类型(默认 container,知识库文档有效)
    pub fn perm_type(mut self, perm_type: impl Into<String>) -> Self {
        self.perm_type = Some(perm_type.into());
        self
    }

    /// 设置协作者类型
    ///
    /// **注意**:当 `member_type` 为 `wikispaceid` 时必填,且必须在
    /// `wiki_space_member`、`wiki_space_viewer`、`wiki_space_editor` 中选择。
    pub fn member_kind(mut self, member_kind: impl Into<String>) -> Self {
        self.member_kind = Some(member_kind.into());
        self
    }

    /// 执行请求。
    pub async fn execute(self) -> SDKResult<CreatePermissionMemberResponse> {
        self.execute_with_options(openlark_core::req_option::RequestOption::default())
            .await
    }

    /// 使用指定请求选项执行请求。
    pub async fn execute_with_options(
        self,
        option: openlark_core::req_option::RequestOption,
    ) -> SDKResult<CreatePermissionMemberResponse> {
        // === 必填字段验证 ===
        validate_required!(self.token, "token 不能为空");
        validate_required!(self.file_type, "file_type 不能为空");
        validate_required!(self.member_type, "member_type 不能为空");
        validate_required!(self.member_id, "member_id 不能为空");
        validate_required!(self.perm, "perm 不能为空");

        // === 枚举值验证 ===
        match self.file_type.as_str() {
            "doc" | "sheet" | "file" | "wiki" | "bitable" | "docx" | "folder" | "mindnote"
            | "minutes" | "slides" => {}
            _ => {
                return Err(openlark_core::error::validation_error(
                    "file_type",
                    "file_type 必须为 doc/sheet/file/wiki/bitable/docx/folder/mindnote/minutes/slides",
                ));
            }
        }
        match self.member_type.as_str() {
            "email" | "openid" | "unionid" | "openchat" | "opendepartmentid" | "userid"
            | "groupid" | "wikispaceid" => {}
            _ => {
                return Err(openlark_core::error::validation_error(
                    "member_type",
                    "member_type 必须为 email/openid/unionid/openchat/opendepartmentid/userid/groupid/wikispaceid",
                ));
            }
        }
        match self.perm.as_str() {
            "view" | "edit" | "full_access" => {}
            _ => {
                return Err(openlark_core::error::validation_error(
                    "perm",
                    "perm 必须为 view/edit/full_access",
                ));
            }
        }
        if let Some(perm_type) = &self.perm_type {
            match perm_type.as_str() {
                "container" | "single_page" => {}
                _ => {
                    return Err(openlark_core::error::validation_error(
                        "perm_type",
                        "perm_type 必须为 container/single_page",
                    ));
                }
            }
        }
        if let Some(member_kind) = &self.member_kind {
            match member_kind.as_str() {
                "user" | "chat" | "department" | "group" | "wiki_space_member"
                | "wiki_space_viewer" | "wiki_space_editor" => {}
                _ => {
                    return Err(openlark_core::error::validation_error(
                        "type",
                        "type 必须为 user/chat/department/group/wiki_space_member/wiki_space_viewer/wiki_space_editor",
                    ));
                }
            }
        }

        // === 业务规则验证 ===
        if self.file_type == "minutes" && self.perm == "full_access" {
            return Err(openlark_core::error::validation_error(
                "perm",
                "当 file_type=minutes 时,不支持 full_access",
            ));
        }
        if self.member_type == "wikispaceid" {
            match self.member_kind.as_deref() {
                Some("wiki_space_member" | "wiki_space_viewer" | "wiki_space_editor") => {}
                _ => {
                    return Err(openlark_core::error::validation_error(
                        "type",
                        "当 member_type=wikispaceid 时,type 必须为 wiki_space_member/wiki_space_viewer/wiki_space_editor",
                    ));
                }
            }
        }

        let api_endpoint = DriveApi::CreatePermissionMember(self.token.clone());

        let mut api_request =
            ApiRequest::<CreatePermissionMemberResponse>::post(&api_endpoint.to_url())
                .query("type", &self.file_type);

        if let Some(need_notification) = self.need_notification {
            api_request = api_request.query("need_notification", need_notification.to_string());
        }

        let body = CreatePermissionMemberRequestBody {
            member_type: self.member_type,
            member_id: self.member_id,
            perm: self.perm,
            perm_type: self.perm_type,
            r#type: self.member_kind,
        };

        api_request = api_request.body(serialize_params(&body, "增加协作者权限")?);

        let response = Transport::request(api_request, &self.config, Some(option)).await?;
        extract_response_data(response, "增加协作者权限")
    }
}

/// 协作者响应
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreatePermissionMemberResponse {
    /// 本次新增的协作者信息
    pub member: PermissionMember,
}

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_permission_member_request_builder() {
        let config = Config::default();

        let request = CreatePermissionMemberRequest::new(
            config,
            "file_token",
            "docx",
            "openid",
            "ou_xxx",
            "view",
        )
        .need_notification(false)
        .perm_type("container");

        assert_eq!(request.token, "file_token");
        assert_eq!(request.file_type, "docx");
        assert_eq!(request.member_type, "openid");
        assert_eq!(request.member_id, "ou_xxx");
        assert_eq!(request.perm, "view");
        assert_eq!(request.need_notification, Some(false));
        assert_eq!(request.perm_type, Some("container".to_string()));
    }

    #[test]
    fn test_response_trait() {
        assert_eq!(
            CreatePermissionMemberResponse::data_format(),
            ResponseFormat::Data
        );
    }

    #[test]
    fn test_empty_token_validation() {
        let config = Config::default();
        let request =
            CreatePermissionMemberRequest::new(config, "", "docx", "openid", "ou_xxx", "view");
        assert_eq!(request.token, "");
    }

    #[test]
    fn test_empty_file_type_validation() {
        let config = Config::default();
        let request = CreatePermissionMemberRequest::new(
            config,
            "file_token",
            "",
            "openid",
            "ou_xxx",
            "view",
        );
        assert_eq!(request.file_type, "");
    }

    #[test]
    fn test_invalid_file_type_validation() {
        let config = Config::default();
        let request = CreatePermissionMemberRequest::new(
            config,
            "file_token",
            "invalid_type",
            "openid",
            "ou_xxx",
            "view",
        );
        assert_eq!(request.file_type, "invalid_type");
    }

    #[test]
    fn test_empty_member_type_validation() {
        let config = Config::default();
        let request =
            CreatePermissionMemberRequest::new(config, "file_token", "docx", "", "ou_xxx", "view");
        assert_eq!(request.member_type, "");
    }

    #[test]
    fn test_invalid_member_type_validation() {
        let config = Config::default();
        let request = CreatePermissionMemberRequest::new(
            config,
            "file_token",
            "docx",
            "invalid_type",
            "ou_xxx",
            "view",
        );
        assert_eq!(request.member_type, "invalid_type");
    }

    #[test]
    fn test_empty_member_id_validation() {
        let config = Config::default();
        let request =
            CreatePermissionMemberRequest::new(config, "file_token", "docx", "openid", "", "view");
        assert_eq!(request.member_id, "");
    }

    #[test]
    fn test_empty_perm_validation() {
        let config = Config::default();
        let request = CreatePermissionMemberRequest::new(
            config,
            "file_token",
            "docx",
            "openid",
            "ou_xxx",
            "",
        );
        assert_eq!(request.perm, "");
    }

    #[test]
    fn test_invalid_perm_validation() {
        let config = Config::default();
        let request = CreatePermissionMemberRequest::new(
            config,
            "file_token",
            "docx",
            "openid",
            "ou_xxx",
            "invalid_perm",
        );
        assert_eq!(request.perm, "invalid_perm");
    }

    #[test]
    fn test_invalid_perm_type_validation() {
        let config = Config::default();
        let request = CreatePermissionMemberRequest::new(
            config,
            "file_token",
            "docx",
            "openid",
            "ou_xxx",
            "view",
        )
        .perm_type("invalid_perm_type");
        assert_eq!(request.perm_type, Some("invalid_perm_type".to_string()));
    }

    #[test]
    fn test_invalid_member_kind_validation() {
        let config = Config::default();
        let request = CreatePermissionMemberRequest::new(
            config,
            "file_token",
            "docx",
            "openid",
            "ou_xxx",
            "view",
        )
        .member_kind("invalid_kind");
        assert_eq!(request.member_kind, Some("invalid_kind".to_string()));
    }

    #[test]
    fn test_minutes_with_full_access_validation() {
        let config = Config::default();
        let request = CreatePermissionMemberRequest::new(
            config,
            "file_token",
            "minutes",
            "openid",
            "ou_xxx",
            "full_access",
        );
        assert_eq!(request.file_type, "minutes");
        assert_eq!(request.perm, "full_access");
    }

    #[test]
    fn test_wikispaceid_without_member_kind_validation() {
        let config = Config::default();
        let request = CreatePermissionMemberRequest::new(
            config,
            "file_token",
            "docx",
            "wikispaceid",
            "ou_xxx",
            "view",
        );
        assert_eq!(request.member_type, "wikispaceid");
        assert_eq!(request.member_kind, None);
    }
}