use crate::common::{api_endpoints::TaskApiV2, api_utils::*};
use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AttachmentListItem {
pub attachment_guid: String,
pub task_guid: String,
pub file_name: String,
pub file_size: i64,
pub file_type: String,
pub created_at: String,
#[serde(default)]
pub creator_id: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ListAttachmentsResponse {
#[serde(default)]
pub has_more: bool,
#[serde(default)]
pub page_token: Option<String>,
#[serde(default)]
pub total: Option<i32>,
#[serde(default)]
pub items: Vec<AttachmentListItem>,
}
#[derive(Debug, Clone)]
pub struct ListAttachmentsRequest {
config: Arc<Config>,
page_size: Option<i32>,
page_token: Option<String>,
task_guid: Option<String>,
}
impl ListAttachmentsRequest {
pub fn new(config: Arc<Config>) -> Self {
Self {
config,
page_size: None,
page_token: None,
task_guid: 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 task_guid(mut self, task_guid: impl Into<String>) -> Self {
self.task_guid = Some(task_guid.into());
self
}
pub async fn execute(self) -> SDKResult<ListAttachmentsResponse> {
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<ListAttachmentsResponse> {
let api_endpoint = TaskApiV2::AttachmentList;
let mut request = ApiRequest::<ListAttachmentsResponse>::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);
}
if let Some(task_guid) = &self.task_guid {
request = request.query("task_guid", task_guid);
}
let response =
openlark_core::http::Transport::request(request, &self.config, Some(option)).await?;
extract_response_data(response, "列取附件")
}
}
impl ApiResponseTrait for ListAttachmentsResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
use std::sync::Arc;
use super::*;
#[test]
fn test_list_attachments_request() {
let config = openlark_core::config::Config::builder()
.app_id("test")
.app_secret("test")
.build();
let request = ListAttachmentsRequest::new(Arc::new(config))
.page_size(20)
.page_token("next_page_token")
.task_guid("task_123");
assert_eq!(request.page_size, Some(20));
assert_eq!(request.page_token, Some("next_page_token".to_string()));
assert_eq!(request.task_guid, Some("task_123".to_string()));
}
#[test]
fn test_attachment_list_api_v2_url() {
let endpoint = TaskApiV2::AttachmentList;
assert_eq!(endpoint.to_url(), "/open-apis/task/v2/attachments");
}
}