Skip to main content

openlark_workflow/v1/task/comment/
create.rs

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