use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct EnvironmentVariableGetBuilder {
config: Config,
namespace: String,
env_var_api_name: String,
}
impl EnvironmentVariableGetBuilder {
pub fn new(
config: Config,
namespace: impl Into<String>,
env_var_api_name: impl Into<String>,
) -> Self {
Self {
config,
namespace: namespace.into(),
env_var_api_name: env_var_api_name.into(),
}
}
pub async fn execute(self) -> SDKResult<EnvironmentVariableGetResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(
self,
option: RequestOption,
) -> SDKResult<EnvironmentVariableGetResponse> {
let url = format!(
"/open-apis/apaas/v1/applications/{}/environment_variables/{}",
self.namespace, self.env_var_api_name
);
let req: ApiRequest<EnvironmentVariableGetResponse> = ApiRequest::get(&url);
let resp = Transport::request(req, &self.config, Some(option)).await?;
resp.data
.ok_or_else(|| openlark_core::error::validation_error("Operation", "响应数据为空"))
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct EnvironmentVariableDetail {
#[serde(rename = "api_name")]
api_name: String,
#[serde(rename = "name")]
name: String,
#[serde(rename = "value")]
value: String,
#[serde(rename = "description", skip_serializing_if = "Option::is_none")]
description: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct EnvironmentVariableGetResponse {
#[serde(rename = "environment_variable")]
environment_variable: EnvironmentVariableDetail,
}
impl ApiResponseTrait for EnvironmentVariableGetResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[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");
}
}