use openlark_core::{
api::{ApiRequest, ApiResponseTrait},
config::Config,
http::Transport,
req_option::RequestOption,
SDKResult,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct IdentityCreateBuilder {
config: Config,
identity_name: String,
identity_code: String,
mobile: Option<String>,
}
impl IdentityCreateBuilder {
pub fn new(config: Config) -> Self {
Self {
config,
identity_name: String::new(),
identity_code: String::new(),
mobile: None,
}
}
pub fn identity_name(mut self, name: impl Into<String>) -> Self {
self.identity_name = name.into();
self
}
pub fn identity_code(mut self, code: impl Into<String>) -> Self {
self.identity_code = code.into();
self
}
pub fn mobile(mut self, mobile: impl Into<String>) -> Self {
self.mobile = Some(mobile.into());
self
}
pub async fn execute(self) -> SDKResult<IdentityCreateResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(
self,
option: RequestOption,
) -> SDKResult<IdentityCreateResponse> {
let url = "/open-apis/human_authentication/v1/identities";
let request = IdentityCreateRequest {
identity_name: self.identity_name,
identity_code: self.identity_code,
mobile: self.mobile,
};
let req: ApiRequest<IdentityCreateResponse> =
ApiRequest::post(url).body(serde_json::to_value(&request)?);
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)]
struct IdentityCreateRequest {
#[serde(rename = "identity_name")]
identity_name: String,
#[serde(rename = "identity_code")]
identity_code: String,
#[serde(rename = "mobile")]
#[serde(skip_serializing_if = "Option::is_none")]
mobile: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IdentityCreateResponse {
#[serde(rename = "identity_id")]
pub identity_id: String,
}
impl ApiResponseTrait for IdentityCreateResponse {}
#[cfg(test)]
#[allow(unused_imports)]
mod tests {
#[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");
}
}