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 let response =
123 openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
124 response.data.ok_or_else(|| {
125 openlark_core::error::validation_error("响应数据为空", "服务器没有返回有效的数据")
126 })
127 }
128}
129
130impl ApiResponseTrait for CreateTaskResponseV1 {
131 fn data_format() -> ResponseFormat {
132 ResponseFormat::Data
133 }
134}
135
136#[cfg(test)]
137#[allow(unused_imports)]
138mod tests {
139 use std::sync::Arc;
140
141 use super::*;
142
143 #[test]
144 fn test_create_task_v1_builder() {
145 let config = Arc::new(
146 openlark_core::config::Config::builder()
147 .app_id("test")
148 .app_secret("test")
149 .build(),
150 );
151
152 let request = CreateTaskRequestV1::new(config)
153 .summary("测试任务")
154 .description("任务描述")
155 .priority(3);
156
157 assert_eq!(request.body.summary, "测试任务");
158 assert_eq!(request.body.description, Some("任务描述".to_string()));
159 assert_eq!(request.body.priority, Some(3));
160 }
161
162 #[test]
163 fn test_task_api_v1_url() {
164 let endpoint = crate::common::api_endpoints::TaskApiV1::TaskCreate;
165 assert_eq!(endpoint.to_url(), "/open-apis/task/v1/tasks");
166 }
167}