openlark_workflow/v1/task/follower/
create.rs1use 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#[derive(Debug, Clone, Serialize, Default)]
16pub struct CreateTaskFollowerBodyV1 {
18 pub follower_id: String,
20}
21
22#[derive(Debug, Clone, Deserialize)]
24pub struct CreateTaskFollowerResponseV1 {
26 pub follower_id: String,
28}
29
30#[derive(Debug, Clone)]
32pub struct CreateTaskFollowerRequestV1 {
34 config: Arc<Config>,
35 task_id: String,
36 body: CreateTaskFollowerBodyV1,
37}
38
39impl CreateTaskFollowerRequestV1 {
40 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 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 pub async fn execute(self) -> SDKResult<CreateTaskFollowerResponseV1> {
57 self.execute_with_options(openlark_core::req_option::RequestOption::default())
58 .await
59 }
60
61 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 let response =
79 openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
80 response.data.ok_or_else(|| {
81 openlark_core::error::validation_error("响应数据为空", "服务器没有返回有效的数据")
82 })
83 }
84}
85
86impl ApiResponseTrait for CreateTaskFollowerResponseV1 {
87 fn data_format() -> ResponseFormat {
88 ResponseFormat::Data
89 }
90}
91
92#[cfg(test)]
93#[allow(unused_imports)]
94mod tests {
95 use std::sync::Arc;
96
97 use super::*;
98
99 #[test]
100 fn test_create_task_follower_v1_builder() {
101 let config = Arc::new(
102 openlark_core::config::Config::builder()
103 .app_id("test")
104 .app_secret("test")
105 .build(),
106 );
107
108 let request =
109 CreateTaskFollowerRequestV1::new(config.clone(), "task_123").follower_id("user_456");
110
111 assert_eq!(request.body.follower_id, "user_456");
112 assert_eq!(request.task_id, "task_123");
113 }
114
115 #[test]
116 fn test_task_follower_create_v1_url() {
117 let endpoint =
118 crate::common::api_endpoints::TaskApiV1::TaskFollowerCreate("task_123".to_string());
119 assert_eq!(
120 endpoint.to_url(),
121 "/open-apis/task/v1/tasks/task_123/followers"
122 );
123 }
124}