use openlark_core::{
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
validate_required, SDKResult,
};
use serde::{Deserialize, Serialize};
use super::models::{App, CopyAppRequest as CopyAppRequestBody};
use super::AppService;
pub struct CopyAppRequest {
app_token: String,
name: Option<String>,
folder_token: Option<String>,
without_content: Option<bool>,
time_zone: Option<String>,
config: Config,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CopyAppResponse {
pub app: App,
}
impl ApiResponseTrait for CopyAppResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl CopyAppRequest {
pub fn new(config: Config) -> Self {
Self {
app_token: String::new(),
name: None,
folder_token: None,
without_content: None,
time_zone: None,
config,
}
}
pub fn app_token(mut self, app_token: impl Into<String>) -> Self {
self.app_token = app_token.into();
self
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn folder_token(mut self, folder_token: impl Into<String>) -> Self {
self.folder_token = Some(folder_token.into());
self
}
pub fn without_content(mut self, without_content: bool) -> Self {
self.without_content = Some(without_content);
self
}
pub fn time_zone(mut self, time_zone: impl Into<String>) -> Self {
self.time_zone = Some(time_zone.into());
self
}
pub async fn execute(self) -> SDKResult<CopyAppResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(self, option: RequestOption) -> SDKResult<CopyAppResponse> {
validate_required!(self.app_token, "应用令牌不能为空");
use crate::common::api_endpoints::BitableApiV1;
let api_endpoint = BitableApiV1::AppCopy(self.app_token.clone());
let request_body = CopyAppRequestBody {
name: self.name.clone(),
folder_token: self.folder_token.clone(),
without_content: self.without_content,
time_zone: self.time_zone.clone(),
};
let api_request: ApiRequest<CopyAppResponse> = ApiRequest::post(&api_endpoint.to_url())
.body(openlark_core::api::RequestData::Binary(serde_json::to_vec(
&request_body,
)?));
let response = Transport::request(api_request, &self.config, Some(option)).await?;
response.data.ok_or_else(|| {
openlark_core::error::validation_error("响应数据为空", "服务器没有返回有效的数据")
})
}
}
impl AppService {
pub fn copy_builder(&self, app_token: impl Into<String>) -> CopyAppRequest {
CopyAppRequest::new(self.config.clone()).app_token(app_token)
}
pub fn copy_app(
&self,
app_token: impl Into<String>,
name: Option<impl Into<String>>,
folder_token: Option<impl Into<String>>,
without_content: Option<bool>,
time_zone: Option<impl Into<String>>,
) -> CopyAppRequest {
let mut request = CopyAppRequest::new(self.config.clone()).app_token(app_token);
if let Some(name) = name {
request = request.name(name);
}
if let Some(folder_token) = folder_token {
request = request.folder_token(folder_token);
}
if let Some(without_content) = without_content {
request = request.without_content(without_content);
}
if let Some(time_zone) = time_zone {
request = request.time_zone(time_zone);
}
request
}
}
#[cfg(test)]
mod tests {
use serde_json;
#[test]
fn test_serialization_roundtrip() {
let json = r#"{"test": "value"}"#;
assert!(serde_json::from_str::<serde_json::Value>(json).is_ok());
}
#[test]
fn test_deserialization_from_json() {
let json = r#"{"field": "data"}"#;
let value: serde_json::Value = serde_json::from_str(json).unwrap();
assert_eq!(value["field"], "data");
}
}