use reqwest::Method;
use serde::Deserialize;
use serde_json::json;
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
constants::AccessTokenType,
endpoints::directory::*,
http::Transport,
req_option::RequestOption,
SDKResult,
},
impl_executable_builder_owned,
service::directory::v1::models::{Department, DepartmentIdType, UserIdType},
};
use super::DepartmentService;
#[derive(Default, Clone)]
pub struct MgetDepartmentRequest {
pub api_req: ApiRequest,
pub department_ids: Vec<String>,
pub user_id_type: Option<UserIdType>,
pub department_id_type: Option<DepartmentIdType>,
}
impl MgetDepartmentRequest {
pub fn builder() -> MgetDepartmentRequestBuilder {
MgetDepartmentRequestBuilder::default()
}
}
#[derive(Default)]
pub struct MgetDepartmentRequestBuilder {
request: MgetDepartmentRequest,
}
impl MgetDepartmentRequestBuilder {
pub fn department_ids(mut self, department_ids: Vec<String>) -> Self {
self.request.department_ids = department_ids;
self
}
pub fn add_department_id(mut self, department_id: impl ToString) -> Self {
self.request.department_ids.push(department_id.to_string());
self
}
pub fn user_id_type(mut self, user_id_type: UserIdType) -> Self {
self.request.user_id_type = Some(user_id_type);
self
}
pub fn department_id_type(mut self, department_id_type: DepartmentIdType) -> Self {
self.request.department_id_type = Some(department_id_type);
self
}
pub fn build(mut self) -> MgetDepartmentRequest {
if let Some(user_id_type) = &self.request.user_id_type {
self.request
.api_req
.query_params
.insert("user_id_type", user_id_type.to_string());
}
if let Some(department_id_type) = &self.request.department_id_type {
self.request
.api_req
.query_params
.insert("department_id_type", department_id_type.to_string());
}
let body = json!({
"department_ids": self.request.department_ids
});
self.request.api_req.body = serde_json::to_vec(&body).unwrap_or_default();
self.request
}
}
#[derive(Debug, Deserialize)]
pub struct MgetDepartmentResponseData {
pub departments: Vec<Department>,
}
#[derive(Debug, Deserialize)]
pub struct MgetDepartmentResponse {
pub data: MgetDepartmentResponseData,
}
impl ApiResponseTrait for MgetDepartmentResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl DepartmentService {
pub async fn mget(
&self,
request: MgetDepartmentRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<MgetDepartmentResponse>> {
let mut api_req = request.api_req;
api_req.http_method = Method::POST;
api_req.api_path = DIRECTORY_V1_DEPARTMENTS_MGET.to_string();
api_req.supported_access_token_types = vec![AccessTokenType::Tenant];
let api_resp = Transport::request(api_req, &self.config, option).await?;
Ok(api_resp)
}
}
impl_executable_builder_owned!(
MgetDepartmentRequestBuilder,
DepartmentService,
MgetDepartmentRequest,
BaseResponse<MgetDepartmentResponse>,
mget
);