use openlark_core::{
SDKResult,
api::{ApiRequest, ApiResponseTrait, ResponseFormat},
config::Config,
http::Transport,
req_option::RequestOption,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct EmployeeCreateRequestBuilder {
config: Config,
name: String,
mobile: String,
email: Option<String>,
department_ids: Vec<String>,
}
impl EmployeeCreateRequestBuilder {
pub fn new(config: Config, name: impl Into<String>, mobile: impl Into<String>) -> Self {
Self {
config,
name: name.into(),
mobile: mobile.into(),
email: None,
department_ids: Vec::new(),
}
}
pub fn email(mut self, email: impl Into<String>) -> Self {
self.email = Some(email.into());
self
}
pub fn department_id(mut self, department_id: impl Into<String>) -> Self {
self.department_ids.push(department_id.into());
self
}
pub fn department_ids(
mut self,
department_ids: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
self.department_ids
.extend(department_ids.into_iter().map(Into::into));
self
}
pub async fn execute(self) -> SDKResult<EmployeeCreateResponse> {
self.execute_with_options(RequestOption::default()).await
}
pub async fn execute_with_options(
self,
option: RequestOption,
) -> SDKResult<EmployeeCreateResponse> {
let url = "/open-apis/directory/v1/employees".to_string();
let request = EmployeeCreateRequest {
name: self.name,
mobile: self.mobile,
email: self.email,
department_ids: self.department_ids,
};
let req: ApiRequest<EmployeeCreateResponse> =
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 EmployeeCreateRequest {
#[serde(rename = "name")]
name: String,
#[serde(rename = "mobile")]
mobile: String,
#[serde(rename = "email", skip_serializing_if = "Option::is_none")]
email: Option<String>,
#[serde(rename = "department_ids", skip_serializing_if = "Vec::is_empty")]
department_ids: Vec<String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct EmployeeCreateResponse {
#[serde(rename = "employee_id")]
pub employee_id: String,
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "mobile")]
pub mobile: String,
#[serde(rename = "created_at")]
pub created_at: i64,
}
impl ApiResponseTrait for EmployeeCreateResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[deprecated(note = "renamed to EmployeeCreateRequestBuilder, will be removed in v1.0 (#271)")]
pub type EmployeeCreateBuilder = EmployeeCreateRequestBuilder;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_builder_basic() {
let config = openlark_core::config::Config::builder()
.app_id("test_app")
.app_secret("test_secret")
.build();
let request = EmployeeCreateRequestBuilder::new(
config.clone(),
"test".to_string(),
"test".to_string(),
)
.email("test".to_string())
.department_id("test".to_string());
let _ = request;
}
}