use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
use crate::v2::section::models::ListSectionsResponse;
use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
validate_required,
};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct ListSectionsRequest {
config: Arc<Config>,
tasklist_guid: String,
page_size: Option<i32>,
page_token: Option<String>,
}
impl ListSectionsRequest {
pub fn new(config: Arc<Config>, tasklist_guid: String) -> Self {
Self {
config,
tasklist_guid,
page_size: None,
page_token: None,
}
}
pub fn page_size(mut self, page_size: i32) -> Self {
self.page_size = Some(page_size);
self
}
pub fn page_token(mut self, page_token: impl Into<String>) -> Self {
self.page_token = Some(page_token.into());
self
}
pub async fn execute(self) -> SDKResult<ListSectionsResponse> {
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<ListSectionsResponse> {
validate_required!(self.tasklist_guid.trim(), "任务清单GUID不能为空");
let api_endpoint = TaskApiV2::SectionList(self.tasklist_guid.clone());
let mut request = ApiRequest::<ListSectionsResponse>::get(api_endpoint.to_url());
if let Some(page_size) = self.page_size {
request = request.query("page_size", page_size.to_string());
}
if let Some(page_token) = &self.page_token {
request = request.query("page_token", page_token);
}
let response =
openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
extract_response_data(response, "获取分组列表")
}
}
impl ApiResponseTrait for ListSectionsResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use std::sync::Arc;
use super::*;
#[test]
fn test_list_sections_request() {
let config = openlark_core::config::Config::builder()
.app_id("test")
.app_secret("test")
.build();
let request =
ListSectionsRequest::new(Arc::new(config), "tasklist_123".to_string()).page_size(20);
assert_eq!(request.tasklist_guid, "tasklist_123");
assert_eq!(request.page_size, Some(20));
}
}