Skip to main content

openlark_workflow/v2/comment/
update.rs

1//! 更新评论
2//!
3//! docPath: <https://open.feishu.cn/document/task-v2/comment/patch>
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use crate::v2::comment::models::{UpdateCommentBody, UpdateCommentResponse};
7use openlark_core::{
8    SDKResult,
9    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
10    config::Config,
11    validate_required,
12};
13use std::sync::Arc;
14
15/// 更新评论请求
16#[derive(Debug, Clone)]
17pub struct UpdateCommentRequest {
18    /// 配置信息
19    config: Arc<Config>,
20    /// 评论 GUID
21    comment_guid: String,
22    /// 请求体
23    body: UpdateCommentBody,
24    /// 用户 ID 类型
25    user_id_type: Option<String>,
26}
27
28impl UpdateCommentRequest {
29    /// 创建新的请求构建器。
30    pub fn new(config: Arc<Config>, comment_guid: String) -> Self {
31        Self {
32            config,
33            comment_guid,
34            body: UpdateCommentBody {
35                comment: Default::default(),
36                update_fields: vec!["content".to_string()],
37            },
38            user_id_type: None,
39        }
40    }
41
42    /// 设置评论内容
43    pub fn content(mut self, content: impl Into<String>) -> Self {
44        self.body.comment.content = Some(content.into());
45        self
46    }
47
48    /// 设置要更新的字段名。
49    pub fn update_fields(mut self, update_fields: Vec<String>) -> Self {
50        self.body.update_fields = update_fields;
51        self
52    }
53
54    /// 设置用户 ID 类型。
55    pub fn user_id_type(mut self, user_id_type: impl Into<String>) -> Self {
56        self.user_id_type = Some(user_id_type.into());
57        self
58    }
59
60    /// 执行请求
61    pub async fn execute(self) -> SDKResult<UpdateCommentResponse> {
62        self.execute_with_options(openlark_core::req_option::RequestOption::default())
63            .await
64    }
65
66    /// 执行请求(带选项)
67    pub async fn execute_with_options(
68        self,
69        option: openlark_core::req_option::RequestOption,
70    ) -> SDKResult<UpdateCommentResponse> {
71        // 验证必填字段
72        validate_required!(self.comment_guid.trim(), "评论GUID不能为空");
73        validate_required!(
74            self.body
75                .comment
76                .content
77                .as_deref()
78                .unwrap_or_default()
79                .trim(),
80            "评论内容不能为空"
81        );
82
83        let api_endpoint = TaskApiV2::CommentUpdate(self.comment_guid.clone());
84        let mut request = ApiRequest::<UpdateCommentResponse>::patch(api_endpoint.to_url());
85
86        let request_body = &self.body;
87        request = request.body(serialize_params(request_body, "更新评论")?);
88        if let Some(user_id_type) = &self.user_id_type {
89            request = request.query("user_id_type", user_id_type);
90        }
91
92        let response =
93            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
94        extract_response_data(response, "更新评论")
95    }
96}
97
98impl ApiResponseTrait for UpdateCommentResponse {
99    fn data_format() -> ResponseFormat {
100        ResponseFormat::Data
101    }
102}
103
104#[cfg(test)]
105#[allow(unused_imports)]
106mod tests {
107    use std::sync::Arc;
108
109    use super::*;
110
111    #[test]
112    fn test_update_comment_builder() {
113        let config = Arc::new(
114            openlark_core::config::Config::builder()
115                .app_id("test")
116                .app_secret("test")
117                .build(),
118        );
119
120        let request =
121            UpdateCommentRequest::new(config, "comment_456".to_string()).content("更新的评论内容");
122
123        assert_eq!(request.comment_guid, "comment_456");
124        assert_eq!(
125            request.body.comment.content.as_deref(),
126            Some("更新的评论内容")
127        );
128    }
129}