use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
use crate::v2::attachment::models::UploadAttachmentResponse;
use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
validate_required,
};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct UploadAttachmentRequest {
config: Arc<Config>,
task_guid: String,
file_path: String,
file_name: Option<String>,
resource_type: String,
user_id_type: Option<String>,
}
impl UploadAttachmentRequest {
pub fn new(config: Arc<Config>, task_guid: String, file_path: String) -> Self {
Self {
config,
task_guid,
file_path,
file_name: None,
resource_type: "task".to_string(),
user_id_type: None,
}
}
pub fn file_name(mut self, file_name: impl Into<String>) -> Self {
self.file_name = Some(file_name.into());
self
}
pub fn resource_type(mut self, resource_type: impl Into<String>) -> Self {
self.resource_type = resource_type.into();
self
}
pub fn user_id_type(mut self, user_id_type: impl Into<String>) -> Self {
self.user_id_type = Some(user_id_type.into());
self
}
pub async fn execute(self) -> SDKResult<UploadAttachmentResponse> {
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<UploadAttachmentResponse> {
validate_required!(self.task_guid.trim(), "任务GUID不能为空");
validate_required!(self.file_path.trim(), "文件路径不能为空");
validate_required!(self.resource_type.trim(), "资源类型不能为空");
let api_endpoint = TaskApiV2::AttachmentUpload;
let file_content = std::fs::read(&self.file_path).map_err(|e| {
openlark_core::error::validation_error(
"读取文件失败",
format!("无法读取文件 {}: {}", self.file_path, e).as_str(),
)
})?;
let filename = self.file_name.unwrap_or_else(|| {
std::path::Path::new(&self.file_path)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("attachment")
.to_string()
});
let form = serde_json::json!({
"resource_type": self.resource_type,
"resource_id": self.task_guid,
"__file_name": filename,
});
let mut request = ApiRequest::<UploadAttachmentResponse>::post(api_endpoint.to_url());
request = request.body(serialize_params(&form, "上传附件")?);
request = request.file_content(file_content);
if let Some(user_id_type) = &self.user_id_type {
request = request.query("user_id_type", user_id_type);
}
openlark_core::http::Transport::request_typed(
request,
&self.config,
Some(option),
"上传附件",
)
.await
}
}
impl ApiResponseTrait for UploadAttachmentResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use std::sync::Arc;
use super::*;
#[test]
fn test_upload_attachment_request() {
let config = openlark_core::config::Config::builder()
.app_id("test")
.app_secret("test")
.build();
let request = UploadAttachmentRequest::new(
Arc::new(config),
"task_123".to_string(),
"/path/to/file.pdf".to_string(),
);
assert_eq!(request.task_guid, "task_123");
assert_eq!(request.file_path, "/path/to/file.pdf");
}
}