use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
validate_required,
};
use serde::{Deserialize, Serialize};
use crate::common::{api_endpoints::DriveApi, api_utils::*};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateImportTaskRequest {
#[serde(skip)]
config: Config,
pub file_extension: String,
pub file_token: String,
pub r#type: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub file_name: Option<String>,
pub point: Point,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Point {
pub mount_type: i32,
pub mount_key: String,
}
impl Point {
pub fn new(mount_key: impl Into<String>) -> Self {
Self {
mount_type: 1,
mount_key: mount_key.into(),
}
}
}
impl CreateImportTaskRequest {
pub fn new(
config: Config,
file_extension: impl Into<String>,
file_token: impl Into<String>,
r#type: impl Into<String>,
point: Point,
) -> Self {
Self {
config,
file_extension: file_extension.into(),
file_token: file_token.into(),
r#type: r#type.into(),
file_name: None,
point,
}
}
pub fn file_name(mut self, file_name: impl Into<String>) -> Self {
self.file_name = Some(file_name.into());
self
}
pub async fn execute(self) -> SDKResult<CreateImportTaskResponse> {
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<CreateImportTaskResponse> {
validate_required!(self.file_extension, "file_extension 不能为空");
let file_token_len = self.file_token.len();
if file_token_len == 0 || file_token_len > 27 {
return Err(openlark_core::error::validation_error(
"file_token",
"file_token 长度必须在 1~27 字节之间",
));
}
match self.r#type.as_str() {
"docx" | "sheet" | "bitable" => {}
_ => {
return Err(openlark_core::error::validation_error(
"type",
"type 仅支持 docx/sheet/bitable",
));
}
}
if self.point.mount_type != 1 {
return Err(openlark_core::error::validation_error(
"point.mount_type",
"point.mount_type 仅支持固定值 1(挂载到云空间)",
));
}
let api_endpoint = DriveApi::CreateImportTask;
let api_request: ApiRequest<CreateImportTaskResponse> = api_endpoint
.to_request()
.body(serialize_params(&self, "创建导入任务")?);
let response = Transport::request(api_request, &self.config, Some(option)).await?;
extract_response_data(response, "创建")
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateImportTaskResponse {
pub ticket: String,
}
impl ApiResponseTrait for CreateImportTaskResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_import_task_request_builder() {
let config = Config::default();
let request = CreateImportTaskRequest::new(
config,
"pdf",
"file_token",
"sheet",
Point::new("AbqrfuRTjlJEIJduwDwcnIabcef"),
)
.file_name("test_file");
assert_eq!(request.file_extension, "pdf");
assert_eq!(request.file_token, "file_token");
assert_eq!(request.r#type, "sheet");
assert_eq!(request.file_name, Some("test_file".to_string()));
}
#[test]
fn test_point_structure() {
let point = Point::new("mount_key");
assert_eq!(point.mount_type, 1);
assert_eq!(point.mount_key, "mount_key");
}
#[test]
fn test_response_trait() {
assert_eq!(
CreateImportTaskResponse::data_format(),
ResponseFormat::Data
);
}
#[test]
fn test_supported_types() {
let config = Config::default();
let point = Point::new("mount_key");
for import_type in ["docx", "sheet", "bitable"] {
let request = CreateImportTaskRequest::new(
config.clone(),
"pdf",
"file_token",
import_type.to_string(),
point.clone(),
);
assert_eq!(request.r#type, import_type);
}
}
#[test]
fn test_file_name_optional() {
let config = Config::default();
let point = Point::new("mount_key");
let request1 = CreateImportTaskRequest::new(
config.clone(),
"pdf",
"file_token",
"sheet",
point.clone(),
);
assert!(request1.file_name.is_none());
let request2 = CreateImportTaskRequest::new(config, "pdf", "file_token", "sheet", point)
.file_name("custom_name");
assert_eq!(request2.file_name, Some("custom_name".to_string()));
}
#[tokio::test]
async fn test_create_import_task_returns_data_on_success() {
use serde_json::json;
use wiremock::MockServer;
use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/open-apis/drive/v1/import_tasks"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"code": 0,
"msg": "success",
"data": {
"ticket": "import_ticket_001"
}
})))
.mount(&server)
.await;
let config = Config::builder()
.app_id("ci_app_id")
.app_secret("ci_app_secret")
.base_url(server.uri())
.enable_token_cache(false)
.build();
let resp = CreateImportTaskRequest::new(
config,
"pdf",
"fileTokenAbc",
"docx",
Point::new("AbqrfuRTjlJEIJduwDwcnIabcef"),
)
.execute()
.await
.expect("创建导入任务应成功");
assert_eq!(resp.ticket, "import_ticket_001");
let received = server.received_requests().await.unwrap_or_default();
assert_eq!(received.len(), 1);
assert_eq!(received[0].url.path(), "/open-apis/drive/v1/import_tasks");
}
}