use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, Response, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
};
use serde::{Deserialize, Serialize};
use crate::baike::lingo::v1::models::Classification;
use crate::common::api_endpoints::LingoApiV1;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListClassificationResp {
#[serde(default)]
pub items: Vec<Classification>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_token: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_more: Option<bool>,
}
impl ApiResponseTrait for ListClassificationResp {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
pub struct ListClassificationRequest {
config: Config,
page_size: Option<i32>,
page_token: Option<String>,
repo_id: Option<String>,
}
impl ListClassificationRequest {
pub fn new(config: Config) -> Self {
Self {
config,
page_size: None,
page_token: None,
repo_id: 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 fn repo_id(mut self, repo_id: impl Into<String>) -> Self {
self.repo_id = Some(repo_id.into());
self
}
pub async fn execute(self) -> SDKResult<ListClassificationResp> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(
self,
option: RequestOption,
) -> SDKResult<ListClassificationResp> {
let mut api_request: ApiRequest<ListClassificationResp> =
ApiRequest::get(&LingoApiV1::ClassificationList.to_url());
if let Some(page_size) = self.page_size {
api_request = api_request.query("page_size", &page_size.to_string());
}
if let Some(page_token) = &self.page_token {
api_request = api_request.query("page_token", page_token);
}
if let Some(repo_id) = &self.repo_id {
api_request = api_request.query("repo_id", repo_id);
}
let response: Response<ListClassificationResp> =
Transport::request(api_request, &self.config, Some(option)).await?;
response
.data
.ok_or_else(|| openlark_core::error::validation_error("response", "响应数据为空"))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_list_classification_request_builder() {
let config = Config::default();
let request = ListClassificationRequest::new(config);
assert!(request.page_size.is_none());
assert!(request.page_token.is_none());
assert!(request.repo_id.is_none());
}
#[test]
fn test_list_classification_request_with_params() {
let config = Config::default();
let request = ListClassificationRequest::new(config)
.page_size(50)
.page_token("token")
.repo_id("repo_123");
assert_eq!(request.page_size, Some(50));
assert_eq!(request.page_token, Some("token".to_string()));
assert_eq!(request.repo_id, Some("repo_123".to_string()));
}
#[test]
fn test_response_structure() {
let response = ListClassificationResp {
items: vec![],
page_token: Some("next".to_string()),
has_more: Some(true),
};
assert!(response.items.is_empty());
assert_eq!(response.page_token, Some("next".to_string()));
assert_eq!(response.has_more, Some(true));
}
#[test]
fn test_response_trait() {
assert_eq!(ListClassificationResp::data_format(), ResponseFormat::Data);
}
}