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