use reqwest::Method;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
config::Config,
constants::AccessTokenType,
endpoints::{approval::*, EndpointBuilder},
http::Transport,
req_option::RequestOption,
SDKResult,
},
service::approval::models::{DepartmentIdType, UserIdType},
};
pub struct ExternalApprovalService {
pub config: Config,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateExternalApprovalRequest {
pub approval_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub external_url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub callback_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub config: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateExternalApprovalResponse {
pub approval_code: String,
}
impl ApiResponseTrait for CreateExternalApprovalResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetExternalApprovalResponse {
pub approval_name: String,
pub description: Option<String>,
pub external_url: String,
pub callback_url: Option<String>,
pub config: Option<serde_json::Value>,
pub status: String,
pub create_time: Option<String>,
pub update_time: Option<String>,
}
impl ApiResponseTrait for GetExternalApprovalResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl ExternalApprovalService {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn create(
&self,
request: CreateExternalApprovalRequest,
user_id_type: Option<UserIdType>,
department_id_type: Option<DepartmentIdType>,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<CreateExternalApprovalResponse>> {
let mut query_params = HashMap::new();
if let Some(user_id_type) = user_id_type {
query_params.insert("user_id_type", user_id_type.as_str().to_string());
}
if let Some(department_id_type) = department_id_type {
query_params.insert(
"department_id_type",
department_id_type.as_str().to_string(),
);
}
let api_req = ApiRequest {
http_method: Method::POST,
api_path: APPROVAL_V4_EXTERNAL_APPROVALS.to_string(),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
query_params,
body: serde_json::to_vec(&request)?,
..Default::default()
};
Transport::request(api_req, &self.config, option).await
}
pub async fn get(
&self,
approval_code: &str,
user_id_type: Option<UserIdType>,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<GetExternalApprovalResponse>> {
let mut query_params = HashMap::new();
if let Some(user_id_type) = user_id_type {
query_params.insert("user_id_type", user_id_type.as_str().to_string());
}
let api_req = ApiRequest {
http_method: Method::GET,
api_path: EndpointBuilder::replace_param(
APPROVAL_V4_EXTERNAL_APPROVAL_GET,
"approval_code",
approval_code,
),
supported_access_token_types: vec![AccessTokenType::Tenant, AccessTokenType::User],
query_params,
..Default::default()
};
Transport::request(api_req, &self.config, option).await
}
}