use openlark_core::{
api::{ApiRequest, ApiResponseTrait},
config::Config,
http::Transport,
req_option::RequestOption,
SDKResult,
validate_required,
validate_required_list,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct CountryRegionBatchGetBuilder {
config: Config,
mdm_codes: Vec<String>,
}
impl CountryRegionBatchGetBuilder {
pub fn new(config: Config) -> Self {
Self {
config,
mdm_codes: Vec::new(),
}
}
pub fn mdm_code(mut self, mdm_code: impl Into<String>) -> Self {
self.mdm_codes.push(mdm_code.into());
self
}
pub fn mdm_codes(mut self, mdm_codes: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.mdm_codes.extend(mdm_codes.into_iter().map(Into::into));
self
}
pub async fn execute(self) -> SDKResult<CountryRegionBatchGetResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(
self,
option: RequestOption,
) -> SDKResult<CountryRegionBatchGetResponse> {
let mut url = "/open-apis/mdm/v3/batch_country_region".to_string();
validate_required_list!(self.mdm_codes, 50, "mdm_codes 不能为空");
let req: ApiRequest<CountryRegionBatchGetResponse> = 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 CountryRegionBatchGetResponse {
pub items: Vec<CountryRegion>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CountryRegion {
#[serde(rename = "mdm_code")]
pub mdm_code: String,
pub name: String,
#[serde(rename = "i18n_name")]
pub i18n_name: Option<CountryRegionI18nName>,
#[serde(rename = "phone_code")]
pub phone_code: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CountryRegionI18nName {
#[serde(rename = "zh_cn")]
pub zh_cn: Option<String>,
#[serde(rename = "en_us")]
pub en_us: Option<String>,
#[serde(rename = "ja_jp")]
pub ja_jp: Option<String>,
}
impl ApiResponseTrait for CountryRegionBatchGetResponse {}
#[cfg(test)]
mod tests {
use super::*;
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");
}
}