openlark_workflow/v2/task/
add_tasklist.rs1use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
6use openlark_core::{
7 api::{ApiRequest, ApiResponseTrait, ResponseFormat},
8 config::Config,
9 validate_required, SDKResult,
10};
11use serde::{Deserialize, Serialize};
12use std::sync::Arc;
13
14#[derive(Debug, Clone, Serialize, Default)]
16pub struct AddTasklistBody {
17 pub tasklist_guid: String,
19 #[serde(skip_serializing_if = "Option::is_none")]
21 pub section_guid: Option<String>,
22}
23
24#[derive(Debug, Clone, Deserialize)]
26pub struct AddTasklistResponse {
27 pub task_guid: String,
29 pub tasklist_guid: String,
31 #[serde(default)]
33 pub section_guid: Option<String>,
34}
35
36#[derive(Debug, Clone)]
38pub struct AddTasklistRequest {
39 config: Arc<Config>,
41 task_guid: String,
43 body: AddTasklistBody,
45}
46
47impl AddTasklistRequest {
48 pub fn new(config: Arc<Config>, task_guid: impl Into<String>) -> Self {
49 Self {
50 config,
51 task_guid: task_guid.into(),
52 body: AddTasklistBody::default(),
53 }
54 }
55
56 pub fn tasklist_guid(mut self, tasklist_guid: impl Into<String>) -> Self {
58 self.body.tasklist_guid = tasklist_guid.into();
59 self
60 }
61
62 pub fn section_guid(mut self, section_guid: impl Into<String>) -> Self {
64 self.body.section_guid = Some(section_guid.into());
65 self
66 }
67
68 pub async fn execute(self) -> SDKResult<AddTasklistResponse> {
70 self.execute_with_options(openlark_core::req_option::RequestOption::default())
71 .await
72 }
73
74 pub async fn execute_with_options(
76 self,
77 option: openlark_core::req_option::RequestOption,
78 ) -> SDKResult<AddTasklistResponse> {
79 validate_required!(self.task_guid.trim(), "任务GUID不能为空");
81 validate_required!(self.body.tasklist_guid.trim(), "任务清单GUID不能为空");
82
83 let api_endpoint = TaskApiV2::TaskAddTasklist(self.task_guid.clone());
84 let mut request = ApiRequest::<AddTasklistResponse>::post(api_endpoint.to_url());
85
86 let request_body = &self.body;
87 request = request.body(serialize_params(request_body, "任务加入清单")?);
88
89 let response =
90 openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
91 extract_response_data(response, "任务加入清单")
92 }
93}
94
95impl ApiResponseTrait for AddTasklistResponse {
96 fn data_format() -> ResponseFormat {
97 ResponseFormat::Data
98 }
99}
100
101#[cfg(test)]
102#[allow(unused_imports)]
103mod tests {
104 use std::sync::Arc;
105
106 use super::*;
107
108 #[test]
109 fn test_add_tasklist_builder() {
110 let config = Arc::new(
111 openlark_core::config::Config::builder()
112 .app_id("test")
113 .app_secret("test")
114 .build(),
115 );
116
117 let request = AddTasklistRequest::new(config, "task_123")
118 .tasklist_guid("tasklist_456")
119 .section_guid("section_789");
120
121 assert_eq!(request.task_guid, "task_123");
122 assert_eq!(request.body.tasklist_guid, "tasklist_456");
123 assert_eq!(request.body.section_guid, Some("section_789".to_string()));
124 }
125
126 #[test]
127 fn test_task_add_tasklist_api_v2_url() {
128 let endpoint = TaskApiV2::TaskAddTasklist("task_123".to_string());
129 assert_eq!(
130 endpoint.to_url(),
131 "/open-apis/task/v2/tasks/task_123/add_tasklist"
132 );
133 }
134}