use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
use crate::v2::section::models::{CreateSectionBody, CreateSectionResponse};
use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
validate_required,
};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct CreateSectionRequest {
config: Arc<Config>,
body: CreateSectionBody,
}
impl CreateSectionRequest {
pub fn new(config: Arc<Config>) -> Self {
Self {
config,
body: CreateSectionBody::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 async fn execute(self) -> SDKResult<CreateSectionResponse> {
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<CreateSectionResponse> {
validate_required!(self.body.summary.trim(), "分组标题不能为空");
let api_endpoint = TaskApiV2::SectionCreate;
let mut request = ApiRequest::<CreateSectionResponse>::post(api_endpoint.to_url());
let request_body = &self.body;
request = request.body(serialize_params(request_body, "创建分组")?);
openlark_core::http::Transport::request_typed(
request,
&self.config,
Some(option),
"创建分组",
)
.await
}
}
impl ApiResponseTrait for CreateSectionResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use std::sync::Arc;
use super::*;
#[test]
fn test_create_section_builder() {
let config = Arc::new(
openlark_core::config::Config::builder()
.app_id("test")
.app_secret("test")
.build(),
);
let request = CreateSectionRequest::new(config).summary("测试分组");
assert_eq!(request.body.summary, "测试分组");
}
}