Skip to main content

openlark_workflow/v2/comment/
create.rs

1//! 创建评论
2//!
3//! docPath: https://open.feishu.cn/document/server-docs/docs/task-v2/comment/create
4
5use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use crate::v2::comment::models::{CreateCommentBody, CreateCommentResponse};
7use openlark_core::{
8    api::{ApiRequest, ApiResponseTrait, ResponseFormat},
9    config::Config,
10    validate_required, SDKResult,
11};
12use std::sync::Arc;
13
14/// 创建评论请求
15#[derive(Debug, Clone)]
16pub struct CreateCommentRequest {
17    /// 配置信息
18    config: Arc<Config>,
19    /// 任务 GUID
20    task_guid: String,
21    /// 请求体
22    body: CreateCommentBody,
23}
24
25impl CreateCommentRequest {
26    pub fn new(config: Arc<Config>, task_guid: String) -> Self {
27        Self {
28            config,
29            task_guid,
30            body: CreateCommentBody::default(),
31        }
32    }
33
34    /// 设置评论内容
35    pub fn content(mut self, content: impl Into<String>) -> Self {
36        self.body.content = content.into();
37        self
38    }
39
40    /// 执行请求
41    pub async fn execute(self) -> SDKResult<CreateCommentResponse> {
42        self.execute_with_options(openlark_core::req_option::RequestOption::default())
43            .await
44    }
45
46    /// 执行请求(带选项)
47    pub async fn execute_with_options(
48        self,
49        option: openlark_core::req_option::RequestOption,
50    ) -> SDKResult<CreateCommentResponse> {
51        // 验证必填字段
52        validate_required!(self.task_guid.trim(), "任务GUID不能为空");
53        validate_required!(self.body.content.trim(), "评论内容不能为空");
54
55        let api_endpoint = TaskApiV2::CommentCreate(self.task_guid.clone());
56        let mut request = ApiRequest::<CreateCommentResponse>::post(api_endpoint.to_url());
57
58        let request_body = &self.body;
59        request = request.body(serialize_params(request_body, "创建评论")?);
60
61        let response =
62            openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
63        extract_response_data(response, "创建评论")
64    }
65}
66
67impl ApiResponseTrait for CreateCommentResponse {
68    fn data_format() -> ResponseFormat {
69        ResponseFormat::Data
70    }
71}
72
73#[cfg(test)]
74#[allow(unused_imports)]
75mod tests {
76    use std::sync::Arc;
77
78    use super::*;
79
80    #[test]
81    fn test_create_comment_builder() {
82        let config = Arc::new(
83            openlark_core::config::Config::builder()
84                .app_id("test")
85                .app_secret("test")
86                .build(),
87        );
88
89        let request =
90            CreateCommentRequest::new(config, "task_123".to_string()).content("这是一条评论");
91
92        assert_eq!(request.task_guid, "task_123");
93        assert_eq!(request.body.content, "这是一条评论");
94    }
95}