openlark_workflow/v1/task/
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 CreateTaskBodyV1 {
18 pub summary: String,
20
21 #[serde(skip_serializing_if = "Option::is_none")]
23 pub description: Option<String>,
25
26 #[serde(skip_serializing_if = "Option::is_none")]
28 pub start: Option<String>,
30
31 #[serde(skip_serializing_if = "Option::is_none")]
33 pub due: Option<String>,
35
36 #[serde(skip_serializing_if = "Option::is_none")]
38 pub priority: Option<i32>,
40}
41
42#[derive(Debug, Clone, Deserialize)]
44pub struct CreateTaskResponseV1 {
46 pub task_guid: String,
48
49 pub summary: String,
51}
52
53#[derive(Debug, Clone)]
55pub struct CreateTaskRequestV1 {
57 config: Arc<Config>,
58 body: CreateTaskBodyV1,
59}
60
61impl CreateTaskRequestV1 {
62 pub fn new(config: Arc<Config>) -> Self {
64 Self {
65 config,
66 body: CreateTaskBodyV1::default(),
67 }
68 }
69
70 pub fn summary(mut self, summary: impl Into<String>) -> Self {
72 self.body.summary = summary.into();
73 self
74 }
75
76 pub fn description(mut self, description: impl Into<String>) -> Self {
78 self.body.description = Some(description.into());
79 self
80 }
81
82 pub fn start(mut self, start: impl Into<String>) -> Self {
84 self.body.start = Some(start.into());
85 self
86 }
87
88 pub fn due(mut self, due: impl Into<String>) -> Self {
90 self.body.due = Some(due.into());
91 self
92 }
93
94 pub fn priority(mut self, priority: i32) -> Self {
96 self.body.priority = Some(priority);
97 self
98 }
99
100 pub async fn execute(self) -> SDKResult<CreateTaskResponseV1> {
102 self.execute_with_options(openlark_core::req_option::RequestOption::default())
103 .await
104 }
105
106 pub async fn execute_with_options(
108 self,
109 option: openlark_core::req_option::RequestOption,
110 ) -> SDKResult<CreateTaskResponseV1> {
111 validate_required!(self.body.summary.trim(), "任务标题不能为空");
112
113 let api_endpoint = crate::common::api_endpoints::TaskApiV1::TaskCreate;
114 let mut request = ApiRequest::<CreateTaskResponseV1>::post(api_endpoint.to_url());
115
116 let body_json = serde_json::to_value(&self.body).map_err(|e| {
117 openlark_core::error::validation_error("序列化请求体失败", e.to_string().as_str())
118 })?;
119
120 request = request.body(body_json);
121
122 openlark_core::http::Transport::request_typed(
123 request,
124 &self.config,
125 Some(option),
126 "响应数据为空",
127 )
128 .await
129 }
130}
131
132impl ApiResponseTrait for CreateTaskResponseV1 {
133 fn data_format() -> ResponseFormat {
134 ResponseFormat::Data
135 }
136}
137
138#[cfg(test)]
139#[allow(unused_imports)]
140mod tests {
141 use std::sync::Arc;
142
143 use super::*;
144
145 #[test]
146 fn test_create_task_v1_builder() {
147 let config = Arc::new(
148 openlark_core::config::Config::builder()
149 .app_id("test")
150 .app_secret("test")
151 .build(),
152 );
153
154 let request = CreateTaskRequestV1::new(config)
155 .summary("测试任务")
156 .description("任务描述")
157 .priority(3);
158
159 assert_eq!(request.body.summary, "测试任务");
160 assert_eq!(request.body.description, Some("任务描述".to_string()));
161 assert_eq!(request.body.priority, Some(3));
162 }
163
164 #[test]
165 fn test_task_api_v1_url() {
166 let endpoint = crate::common::api_endpoints::TaskApiV1::TaskCreate;
167 assert_eq!(endpoint.to_url(), "/open-apis/task/v1/tasks");
168 }
169}