use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
use crate::v2::task::models::{CreateTaskBody, CreateTaskResponse};
use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
validate_required,
};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct CreateTaskRequest {
config: Arc<Config>,
body: CreateTaskBody,
}
impl CreateTaskRequest {
pub fn new(config: Arc<Config>) -> Self {
Self {
config,
body: CreateTaskBody::default(),
}
}
pub fn summary(mut self, summary: impl Into<String>) -> Self {
self.body.summary = summary.into();
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.body.description = Some(description.into());
self
}
pub fn start(mut self, start: impl Into<String>) -> Self {
self.body.start = Some(start.into());
self
}
pub fn due(mut self, due: impl Into<String>) -> Self {
self.body.due = Some(due.into());
self
}
pub fn tasklist_guid(mut self, tasklist_guid: impl Into<String>) -> Self {
self.body.tasklist_guid = Some(tasklist_guid.into());
self
}
pub fn section_guid(mut self, section_guid: impl Into<String>) -> Self {
self.body.section_guid = Some(section_guid.into());
self
}
pub fn priority(mut self, priority: i32) -> Self {
self.body.priority = Some(priority);
self
}
pub fn custom_fields(mut self, custom_fields: serde_json::Value) -> Self {
self.body.custom_fields = Some(custom_fields);
self
}
pub fn followers(mut self, followers: Vec<String>) -> Self {
self.body.followers = Some(followers);
self
}
pub fn subtasks(mut self, subtasks: Vec<serde_json::Value>) -> Self {
self.body.subtasks = Some(subtasks);
self
}
pub fn assignee(mut self, assignee: impl Into<String>) -> Self {
self.body.assignee = Some(assignee.into());
self
}
pub fn remind_time(mut self, remind_time: impl Into<String>) -> Self {
self.body.remind_time = Some(remind_time.into());
self
}
pub fn repeat_rule(mut self, repeat_rule: serde_json::Value) -> Self {
self.body.repeat_rule = Some(repeat_rule);
self
}
pub async fn execute(self) -> SDKResult<CreateTaskResponse> {
self.execute_with_options(openlark_core::req_option::RequestOption::default())
.await
}
pub async fn execute_with_options(
self,
option: openlark_core::req_option::RequestOption,
) -> SDKResult<CreateTaskResponse> {
validate_required!(self.body.summary.trim(), "任务标题不能为空");
let api_endpoint = TaskApiV2::TaskCreate;
let mut request = ApiRequest::<CreateTaskResponse>::post(api_endpoint.to_url());
let request_body = &self.body;
request = request.body(serialize_params(request_body, "创建任务")?);
let response =
openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
extract_response_data(response, "创建任务")
}
}
impl ApiResponseTrait for CreateTaskResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use std::sync::Arc;
use super::*;
#[test]
fn test_create_task_builder() {
let config = Arc::new(
openlark_core::config::Config::builder()
.app_id("test")
.app_secret("test")
.build(),
);
let request = CreateTaskRequest::new(config)
.summary("测试任务")
.description("任务描述")
.priority(3);
assert_eq!(request.body.summary, "测试任务");
assert_eq!(request.body.description, Some("任务描述".to_string()));
assert_eq!(request.body.priority, Some(3));
}
#[test]
fn test_task_api_v2_url() {
let endpoint = TaskApiV2::TaskCreate;
assert_eq!(endpoint.to_url(), "/open-apis/task/v2/tasks");
let endpoint = TaskApiV2::TaskGet("task_123".to_string());
assert!(endpoint.to_url().contains("task_123"));
}
}