Skip to main content

openlark_workflow/v1/task/follower/
create.rs

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