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        openlark_core::http::Transport::request_typed(
93            request,
94            &self.config,
95            Some(option),
96            "更新评论",
97        )
98        .await
99    }
100}
101
102impl ApiResponseTrait for UpdateCommentResponse {
103    fn data_format() -> ResponseFormat {
104        ResponseFormat::Data
105    }
106}
107
108#[cfg(test)]
109#[allow(unused_imports)]
110mod tests {
111    use std::sync::Arc;
112
113    use super::*;
114
115    #[test]
116    fn test_update_comment_builder() {
117        let config = Arc::new(
118            openlark_core::config::Config::builder()
119                .app_id("test")
120                .app_secret("test")
121                .build(),
122        );
123
124        let request =
125            UpdateCommentRequest::new(config, "comment_456".to_string()).content("更新的评论内容");
126
127        assert_eq!(request.comment_guid, "comment_456");
128        assert_eq!(
129            request.body.comment.content.as_deref(),
130            Some("更新的评论内容")
131        );
132    }
133}