use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
validate_required,
};
use serde::{Deserialize, Serialize};
use super::AppService;
use super::models::{App, AppSettings, UpdateAppRequest as UpdateAppRequestBody};
pub struct UpdateAppRequest {
app_token: String,
name: Option<String>,
avatar: Option<String>,
app_settings: Option<AppSettings>,
config: Config,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct UpdateAppResponse {
pub app: App,
}
impl ApiResponseTrait for UpdateAppResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl UpdateAppRequest {
pub fn new(config: Config) -> Self {
Self {
app_token: String::new(),
name: None,
avatar: None,
app_settings: 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 avatar(mut self, avatar: impl Into<String>) -> Self {
self.avatar = Some(avatar.into());
self
}
pub fn app_settings(mut self, app_settings: AppSettings) -> Self {
self.app_settings = Some(app_settings);
self
}
pub async fn execute(self) -> SDKResult<UpdateAppResponse> {
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<UpdateAppResponse> {
validate_required!(self.app_token, "应用令牌不能为空");
use crate::common::api_endpoints::BitableApiV1;
let api_endpoint = BitableApiV1::AppUpdate(self.app_token.clone());
let request_body = UpdateAppRequestBody {
name: self.name.clone(),
avatar: self.avatar.clone(),
app_settings: self.app_settings.clone(),
};
let api_request: ApiRequest<UpdateAppResponse> = ApiRequest::put(&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 update_builder(&self, app_token: impl Into<String>) -> UpdateAppRequest {
UpdateAppRequest::new(self.config.clone()).app_token(app_token)
}
pub fn update_app(
&self,
app_token: impl Into<String>,
name: Option<impl Into<String>>,
avatar: Option<impl Into<String>>,
app_settings: Option<AppSettings>,
) -> UpdateAppRequest {
let mut request = UpdateAppRequest::new(self.config.clone()).app_token(app_token);
if let Some(name) = name {
request = request.name(name);
}
if let Some(avatar) = avatar {
request = request.avatar(avatar);
}
if let Some(app_settings) = app_settings {
request = request.app_settings(app_settings);
}
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).expect("JSON 反序列化失败");
assert_eq!(value["field"], "data");
}
}