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