Skip to main content

openlark_workflow/v1/task/comment/
update.rs

1//! 更新任务评论(v1)
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v1/taskcomment/update
4
5use openlark_core::{
6    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
7    config::Config,
8    validate_required, SDKResult,
9};
10use serde::{Deserialize, Serialize};
11use std::sync::Arc;
12
13/// 更新任务评论请求体(v1)
14#[derive(Debug, Clone, Serialize, Default)]
15pub struct UpdateTaskCommentBodyV1 {
16    /// 评论内容
17    pub content: String,
18}
19
20/// 更新任务评论响应(v1)
21#[derive(Debug, Clone, Deserialize)]
22pub struct UpdateTaskCommentResponseV1 {
23    /// 评论 ID
24    pub comment_id: String,
25    /// 评论内容
26    pub content: String,
27}
28
29/// 更新任务评论请求(v1)
30#[derive(Debug, Clone)]
31pub struct UpdateTaskCommentRequestV1 {
32    config: Arc<Config>,
33    task_id: String,
34    comment_id: String,
35    body: UpdateTaskCommentBodyV1,
36}
37
38impl UpdateTaskCommentRequestV1 {
39    pub fn new(
40        config: Arc<Config>,
41        task_id: impl Into<String>,
42        comment_id: impl Into<String>,
43    ) -> Self {
44        Self {
45            config,
46            task_id: task_id.into(),
47            comment_id: comment_id.into(),
48            body: UpdateTaskCommentBodyV1::default(),
49        }
50    }
51
52    /// 设置评论内容
53    pub fn content(mut self, content: impl Into<String>) -> Self {
54        self.body.content = content.into();
55        self
56    }
57
58    /// 执行请求
59    pub async fn execute(self) -> SDKResult<UpdateTaskCommentResponseV1> {
60        self.execute_with_options(openlark_core::req_option::RequestOption::default())
61            .await
62    }
63
64    /// 执行请求(带选项)
65    pub async fn execute_with_options(
66        self,
67        option: openlark_core::req_option::RequestOption,
68    ) -> SDKResult<UpdateTaskCommentResponseV1> {
69        validate_required!(self.body.content.trim(), "评论内容不能为空");
70
71        let api_endpoint = crate::common::api_endpoints::TaskApiV1::TaskCommentUpdate(
72            self.task_id.clone(),
73            self.comment_id.clone(),
74        );
75        let mut request = ApiRequest::<UpdateTaskCommentResponseV1>::put(api_endpoint.to_url());
76
77        let body_json = serde_json::to_value(&self.body).map_err(|e| {
78            openlark_core::error::validation_error("序列化请求体失败", e.to_string().as_str())
79        })?;
80
81        request = request.body(body_json);
82
83        let response =
84            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
85        response.data.ok_or_else(|| {
86            openlark_core::error::validation_error("响应数据为空", "服务器没有返回有效的数据")
87        })
88    }
89}
90
91impl ApiResponseTrait for UpdateTaskCommentResponseV1 {
92    fn data_format() -> ResponseFormat {
93        ResponseFormat::Data
94    }
95}
96
97#[cfg(test)]
98#[allow(unused_imports)]
99mod tests {
100    use std::sync::Arc;
101
102    use super::*;
103
104    #[test]
105    fn test_update_task_comment_v1_builder() {
106        let config = Arc::new(
107            openlark_core::config::Config::builder()
108                .app_id("test")
109                .app_secret("test")
110                .build(),
111        );
112
113        let request = UpdateTaskCommentRequestV1::new(config.clone(), "task_123", "comment_456")
114            .content("更新后的评论内容");
115
116        assert_eq!(request.body.content, "更新后的评论内容");
117    }
118
119    #[test]
120    fn test_task_comment_update_v1_url() {
121        let endpoint = crate::common::api_endpoints::TaskApiV1::TaskCommentUpdate(
122            "task_123".to_string(),
123            "comment_456".to_string(),
124        );
125        assert_eq!(
126            endpoint.to_url(),
127            "/open-apis/task/v1/tasks/task_123/comments/comment_456"
128        );
129    }
130}