openlark_workflow/v2/comment/
update.rs1use 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#[derive(Debug, Clone)]
17pub struct UpdateCommentRequest {
18 config: Arc<Config>,
20 comment_guid: String,
22 body: UpdateCommentBody,
24 user_id_type: Option<String>,
26}
27
28impl UpdateCommentRequest {
29 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 pub fn content(mut self, content: impl Into<String>) -> Self {
44 self.body.comment.content = Some(content.into());
45 self
46 }
47
48 pub fn update_fields(mut self, update_fields: Vec<String>) -> Self {
50 self.body.update_fields = update_fields;
51 self
52 }
53
54 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 pub async fn execute(self) -> SDKResult<UpdateCommentResponse> {
62 self.execute_with_options(openlark_core::req_option::RequestOption::default())
63 .await
64 }
65
66 pub async fn execute_with_options(
68 self,
69 option: openlark_core::req_option::RequestOption,
70 ) -> SDKResult<UpdateCommentResponse> {
71 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}