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