Skip to main content

openlark_workflow/v2/comment/
update.rs

1//! 更新评论
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v2/comment/update
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    task_guid: String,
22    /// 评论 GUID
23    comment_guid: String,
24    /// 请求体
25    body: UpdateCommentBody,
26}
27
28impl UpdateCommentRequest {
29    /// 创建新的请求构建器。
30    pub fn new(config: Arc<Config>, task_guid: String, comment_guid: String) -> Self {
31        Self {
32            config,
33            task_guid,
34            comment_guid,
35            body: UpdateCommentBody::default(),
36        }
37    }
38
39    /// 设置评论内容
40    pub fn content(mut self, content: impl Into<String>) -> Self {
41        self.body.content = content.into();
42        self
43    }
44
45    /// 执行请求
46    pub async fn execute(self) -> SDKResult<UpdateCommentResponse> {
47        self.execute_with_options(openlark_core::req_option::RequestOption::default())
48            .await
49    }
50
51    /// 执行请求(带选项)
52    pub async fn execute_with_options(
53        self,
54        option: openlark_core::req_option::RequestOption,
55    ) -> SDKResult<UpdateCommentResponse> {
56        // 验证必填字段
57        validate_required!(self.task_guid.trim(), "任务GUID不能为空");
58        validate_required!(self.comment_guid.trim(), "评论GUID不能为空");
59        validate_required!(self.body.content.trim(), "评论内容不能为空");
60
61        let api_endpoint =
62            TaskApiV2::CommentUpdate(self.task_guid.clone(), self.comment_guid.clone());
63        let mut request = ApiRequest::<UpdateCommentResponse>::put(api_endpoint.to_url());
64
65        let request_body = &self.body;
66        request = request.body(serialize_params(request_body, "更新评论")?);
67
68        let response =
69            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
70        extract_response_data(response, "更新评论")
71    }
72}
73
74impl ApiResponseTrait for UpdateCommentResponse {
75    fn data_format() -> ResponseFormat {
76        ResponseFormat::Data
77    }
78}
79
80#[cfg(test)]
81#[allow(unused_imports)]
82mod tests {
83    use std::sync::Arc;
84
85    use super::*;
86
87    #[test]
88    fn test_update_comment_builder() {
89        let config = Arc::new(
90            openlark_core::config::Config::builder()
91                .app_id("test")
92                .app_secret("test")
93                .build(),
94        );
95
96        let request =
97            UpdateCommentRequest::new(config, "task_123".to_string(), "comment_456".to_string())
98                .content("更新的评论内容");
99
100        assert_eq!(request.task_guid, "task_123");
101        assert_eq!(request.comment_guid, "comment_456");
102        assert_eq!(request.body.content, "更新的评论内容");
103    }
104}