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    SDKResult,
7    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
8    config::Config,
9    validate_required,
10};
11use serde::{Deserialize, Serialize};
12use std::sync::Arc;
13
14/// 创建任务评论请求体(v1)
15#[derive(Debug, Clone, Serialize, Default)]
16/// 创建任务评论请求体。
17pub struct CreateTaskCommentBodyV1 {
18    /// 评论内容
19    pub content: String,
20    /// 父评论 ID(回复评论时使用)
21    #[serde(skip_serializing_if = "Option::is_none")]
22    /// 父评论 ID。
23    pub parent_id: Option<String>,
24}
25
26/// 创建任务评论响应(v1)
27#[derive(Debug, Clone, Deserialize)]
28/// 创建任务评论响应。
29pub struct CreateTaskCommentResponseV1 {
30    /// 评论 ID
31    pub comment_id: String,
32}
33
34/// 创建任务评论请求(v1)
35#[derive(Debug, Clone)]
36/// 创建任务评论请求构建器。
37pub struct CreateTaskCommentRequestV1 {
38    config: Arc<Config>,
39    task_id: String,
40    body: CreateTaskCommentBodyV1,
41}
42
43impl CreateTaskCommentRequestV1 {
44    /// 创建新的请求构建器。
45    pub fn new(config: Arc<Config>, task_id: impl Into<String>) -> Self {
46        Self {
47            config,
48            task_id: task_id.into(),
49            body: CreateTaskCommentBodyV1::default(),
50        }
51    }
52
53    /// 设置评论内容
54    pub fn content(mut self, content: impl Into<String>) -> Self {
55        self.body.content = content.into();
56        self
57    }
58
59    /// 设置父评论 ID(回复评论时使用)
60    pub fn parent_id(mut self, parent_id: impl Into<String>) -> Self {
61        self.body.parent_id = Some(parent_id.into());
62        self
63    }
64
65    /// 执行请求
66    pub async fn execute(self) -> SDKResult<CreateTaskCommentResponseV1> {
67        self.execute_with_options(openlark_core::req_option::RequestOption::default())
68            .await
69    }
70
71    /// 执行请求(带选项)
72    pub async fn execute_with_options(
73        self,
74        option: openlark_core::req_option::RequestOption,
75    ) -> SDKResult<CreateTaskCommentResponseV1> {
76        validate_required!(self.body.content.trim(), "评论内容不能为空");
77
78        let api_endpoint =
79            crate::common::api_endpoints::TaskApiV1::TaskCommentCreate(self.task_id.clone());
80        let mut request = ApiRequest::<CreateTaskCommentResponseV1>::post(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 CreateTaskCommentResponseV1 {
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_create_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 = CreateTaskCommentRequestV1::new(config.clone(), "task_123")
119            .content("这是一条评论")
120            .parent_id("comment_parent");
121
122        assert_eq!(request.body.content, "这是一条评论");
123        assert_eq!(request.body.parent_id, Some("comment_parent".to_string()));
124    }
125
126    #[test]
127    fn test_task_comment_create_v1_url() {
128        let endpoint =
129            crate::common::api_endpoints::TaskApiV1::TaskCommentCreate("task_123".to_string());
130        assert_eq!(
131            endpoint.to_url(),
132            "/open-apis/task/v1/tasks/task_123/comments"
133        );
134    }
135}