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
412
413
414
415
416
417
use reqwest::Method;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::{
    core::{
        api_req::ApiRequest,
        api_resp::{ApiResponseTrait, BaseResponse, EmptyResponse, ResponseFormat},
        config::Config,
        constants::AccessTokenType,
        endpoints::{EndpointBuilder, Endpoints},
        http::Transport,
        req_option::RequestOption,
        SDKResult,
    },
    service::task::models::{Comment, UserIdType},
};

/// 评论服务
#[derive(Debug)]
pub struct CommentService {
    pub config: Config,
}

/// 创建评论请求
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateCommentRequest {
    /// 评论内容
    pub content: String,
    /// 父评论ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<String>,
}

/// 创建评论响应
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateCommentResponse {
    /// 创建的评论
    pub comment: Comment,
}

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

/// 更新评论请求
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateCommentRequest {
    /// 评论内容
    pub content: String,
}

/// 更新评论响应
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateCommentResponse {
    /// 更新后的评论
    pub comment: Comment,
}

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

/// 获取评论响应
#[derive(Debug, Serialize, Deserialize)]
pub struct GetCommentResponse {
    /// 评论详情
    pub comment: Comment,
}

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

/// 评论列表响应
#[derive(Debug, Serialize, Deserialize)]
pub struct ListCommentsResponse {
    /// 评论列表
    pub items: Vec<Comment>,
    /// 下一页标记
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
    /// 是否还有更多数据
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_more: Option<bool>,
}

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

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

    /// 创建评论
    pub async fn create(
        &self,
        task_guid: &str,
        request: CreateCommentRequest,
        user_id_type: Option<UserIdType>,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<CreateCommentResponse>> {
        let mut query_params = HashMap::new();
        if let Some(user_id_type) = user_id_type {
            query_params.insert("user_id_type", user_id_type.as_str().to_string());
        }

        let api_req = ApiRequest {
            http_method: Method::POST,
            api_path: EndpointBuilder::replace_param(
                Endpoints::TASK_V2_TASK_COMMENTS,
                "task_guid",
                task_guid,
            ),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            query_params,
            body: serde_json::to_vec(&request)?,
            ..Default::default()
        };

        Transport::request(api_req, &self.config, option).await
    }

    /// 获取评论详情
    pub async fn get(
        &self,
        task_guid: &str,
        comment_id: &str,
        user_id_type: Option<UserIdType>,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<GetCommentResponse>> {
        let mut query_params = HashMap::new();
        if let Some(user_id_type) = user_id_type {
            query_params.insert("user_id_type", user_id_type.as_str().to_string());
        }

        let temp_path = EndpointBuilder::replace_param(
            Endpoints::TASK_V2_TASK_COMMENT_GET,
            "task_guid",
            task_guid,
        );
        let api_req = ApiRequest {
            http_method: Method::GET,
            api_path: EndpointBuilder::replace_param(&temp_path, "comment_id", comment_id),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            query_params,
            ..Default::default()
        };

        Transport::request(api_req, &self.config, option).await
    }

    /// 更新评论
    pub async fn patch(
        &self,
        task_guid: &str,
        comment_id: &str,
        request: UpdateCommentRequest,
        user_id_type: Option<UserIdType>,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<UpdateCommentResponse>> {
        let mut query_params = HashMap::new();
        if let Some(user_id_type) = user_id_type {
            query_params.insert("user_id_type", user_id_type.as_str().to_string());
        }

        let temp_path2 = EndpointBuilder::replace_param(
            Endpoints::TASK_V2_TASK_COMMENT_GET,
            "task_guid",
            task_guid,
        );
        let api_req = ApiRequest {
            http_method: Method::PATCH,
            api_path: EndpointBuilder::replace_param(&temp_path2, "comment_id", comment_id),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            query_params,
            body: serde_json::to_vec(&request)?,
            ..Default::default()
        };

        Transport::request(api_req, &self.config, option).await
    }

    /// 删除评论
    pub async fn delete(
        &self,
        task_guid: &str,
        comment_id: &str,
        user_id_type: Option<UserIdType>,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<EmptyResponse>> {
        let mut query_params = HashMap::new();
        if let Some(user_id_type) = user_id_type {
            query_params.insert("user_id_type", user_id_type.as_str().to_string());
        }

        let temp_path3 = EndpointBuilder::replace_param(
            Endpoints::TASK_V2_TASK_COMMENT_GET,
            "task_guid",
            task_guid,
        );
        let api_req = ApiRequest {
            http_method: Method::DELETE,
            api_path: EndpointBuilder::replace_param(&temp_path3, "comment_id", comment_id),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            query_params,
            ..Default::default()
        };

        Transport::request(api_req, &self.config, option).await
    }

    /// 获取评论列表
    pub async fn list(
        &self,
        task_guid: &str,
        page_size: Option<i32>,
        page_token: Option<&str>,
        user_id_type: Option<UserIdType>,
        option: Option<RequestOption>,
    ) -> SDKResult<BaseResponse<ListCommentsResponse>> {
        let mut query_params = HashMap::new();
        if let Some(user_id_type) = user_id_type {
            query_params.insert("user_id_type", user_id_type.as_str().to_string());
        }
        if let Some(page_size) = page_size {
            query_params.insert("page_size", page_size.to_string());
        }
        if let Some(page_token) = page_token {
            query_params.insert("page_token", page_token.to_string());
        }

        let api_req = ApiRequest {
            http_method: Method::GET,
            api_path: EndpointBuilder::replace_param(
                Endpoints::TASK_V2_TASK_COMMENTS,
                "task_guid",
                task_guid,
            ),
            supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
            query_params,
            ..Default::default()
        };

        Transport::request(api_req, &self.config, option).await
    }
}

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

    fn create_test_config() -> Config {
        Config::default()
    }

    #[test]
    fn test_comment_service_creation() {
        let config = create_test_config();
        let service = CommentService::new(config);
    }

    #[test]
    fn test_create_comment_request_serialization() {
        let request = CreateCommentRequest {
            content: "这是一个测试评论".to_string(),
            parent_id: Some("parent_123".to_string()),
        };

        let serialized = serde_json::to_string(&request).unwrap();
        let deserialized: CreateCommentRequest = serde_json::from_str(&serialized).unwrap();

        assert_eq!(request.content, deserialized.content);
        assert_eq!(request.parent_id, deserialized.parent_id);
    }

    #[test]
    fn test_create_comment_request_without_parent() {
        let request = CreateCommentRequest {
            content: "顶级评论".to_string(),
            parent_id: None,
        };

        let serialized = serde_json::to_string(&request).unwrap();
        let deserialized: CreateCommentRequest = serde_json::from_str(&serialized).unwrap();

        assert_eq!(request.content, deserialized.content);
        assert!(deserialized.parent_id.is_none());
    }

    #[test]
    fn test_create_comment_response_serialization() {
        let comment = Comment {
            id: Some("comment_123".to_string()),
            content: Some("测试评论内容".to_string()),
            ..Default::default()
        };

        let response = CreateCommentResponse { comment };
        let serialized = serde_json::to_string(&response).unwrap();
        let deserialized: CreateCommentResponse = serde_json::from_str(&serialized).unwrap();

        assert_eq!(response.comment.id, deserialized.comment.id);
        assert_eq!(response.comment.content, deserialized.comment.content);
    }

    #[test]
    fn test_update_comment_request_serialization() {
        let request = UpdateCommentRequest {
            content: "更新后的评论内容".to_string(),
        };

        let serialized = serde_json::to_string(&request).unwrap();
        let deserialized: UpdateCommentRequest = serde_json::from_str(&serialized).unwrap();

        assert_eq!(request.content, deserialized.content);
    }

    #[test]
    fn test_update_comment_response_serialization() {
        let comment = Comment {
            id: Some("comment_456".to_string()),
            content: Some("更新的评论".to_string()),
            ..Default::default()
        };

        let response = UpdateCommentResponse { comment };
        let serialized = serde_json::to_string(&response).unwrap();
        let deserialized: UpdateCommentResponse = serde_json::from_str(&serialized).unwrap();

        assert_eq!(response.comment.id, deserialized.comment.id);
        assert_eq!(response.comment.content, deserialized.comment.content);
    }

    #[test]
    fn test_get_comment_response_serialization() {
        let comment = Comment {
            id: Some("comment_789".to_string()),
            content: Some("获取的评论".to_string()),
            ..Default::default()
        };

        let response = GetCommentResponse { comment };
        let serialized = serde_json::to_string(&response).unwrap();
        let deserialized: GetCommentResponse = serde_json::from_str(&serialized).unwrap();

        assert_eq!(response.comment.id, deserialized.comment.id);
        assert_eq!(response.comment.content, deserialized.comment.content);
    }

    #[test]
    fn test_list_comments_response_serialization() {
        let comment = Comment {
            id: Some("comment_list_1".to_string()),
            content: Some("列表评论".to_string()),
            ..Default::default()
        };

        let response = ListCommentsResponse {
            items: vec![comment],
            page_token: Some("next_page_token".to_string()),
            has_more: Some(true),
        };

        let serialized = serde_json::to_string(&response).unwrap();
        let deserialized: ListCommentsResponse = serde_json::from_str(&serialized).unwrap();

        assert_eq!(response.items.len(), deserialized.items.len());
        assert_eq!(response.page_token, deserialized.page_token);
        assert_eq!(response.has_more, deserialized.has_more);
    }

    #[test]
    fn test_list_comments_response_optional_fields() {
        let response = ListCommentsResponse {
            items: vec![],
            page_token: None,
            has_more: None,
        };

        let serialized = serde_json::to_string(&response).unwrap();
        let deserialized: ListCommentsResponse = serde_json::from_str(&serialized).unwrap();

        assert!(deserialized.items.is_empty());
        assert!(deserialized.page_token.is_none());
        assert!(deserialized.has_more.is_none());
    }

    #[test]
    fn test_response_format_traits() {
        assert_eq!(CreateCommentResponse::data_format(), ResponseFormat::Data);
        assert_eq!(UpdateCommentResponse::data_format(), ResponseFormat::Data);
        assert_eq!(GetCommentResponse::data_format(), ResponseFormat::Data);
        assert_eq!(ListCommentsResponse::data_format(), ResponseFormat::Data);
    }

    #[test]
    fn test_comment_service_debug() {
        let config = create_test_config();
        let service = CommentService::new(config);

        // Test Debug trait implementation
        let debug_string = format!("{:?}", service);
        assert!(debug_string.contains("CommentService"));
    }
}