use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::client::{get_owner, Client, Response};
use crate::error::Result;
use crate::serde_util::null_to_default;
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default)]
pub struct Provider {
pub owner: String,
pub name: String,
pub created_time: String,
pub display_name: String,
pub category: String,
pub r#type: String,
pub sub_type: String,
pub method: String,
pub client_id: String,
pub client_secret: String,
pub client_id2: String,
pub client_secret2: String,
pub cert: String,
pub custom_auth_url: String,
pub custom_token_url: String,
pub custom_user_info_url: String,
pub custom_logout_url: String,
pub custom_logo: String,
pub scopes: String,
#[serde(deserialize_with = "null_to_default")]
pub user_mapping: HashMap<String, String>,
#[serde(deserialize_with = "null_to_default")]
pub http_headers: HashMap<String, String>,
pub host: String,
pub port: i32,
pub disable_ssl: bool,
pub ssl_mode: String,
pub title: String,
pub content: String,
pub receiver: String,
pub region_id: String,
pub sign_name: String,
pub template_code: String,
pub app_id: String,
pub endpoint: String,
pub intranet_endpoint: String,
pub domain: String,
pub bucket: String,
pub path_prefix: String,
pub metadata: String,
pub id_p: String,
pub issuer_url: String,
pub enable_sign_authn_request: bool,
pub email_regex: String,
pub provider_url: String,
pub enable_proxy: bool,
pub enable_pkce: bool,
pub state: String,
}
impl Client {
pub async fn get_providers(&self) -> Result<Vec<Provider>> {
self.get_list(
"get-providers",
&[("owner", &self.config.organization_name)],
)
.await
}
pub async fn get_provider(&self, name: &str) -> Result<Option<Provider>> {
self.get_object("get-provider", &[("id", &self.get_id(name))])
.await
}
pub async fn get_pagination_providers(
&self,
p: i32,
page_size: i32,
query: &[(&str, &str)],
) -> Result<(Vec<Provider>, i32)> {
self.get_pagination(
"get-providers",
&self.config.organization_name,
p,
page_size,
query,
)
.await
}
pub async fn add_provider(&self, provider: &Provider) -> Result<bool> {
Ok(self.modify_provider("add-provider", provider, &[]).await?.1)
}
pub async fn update_provider(&self, provider: &Provider) -> Result<bool> {
Ok(self
.modify_provider("update-provider", provider, &[])
.await?
.1)
}
pub async fn delete_provider(&self, provider: &Provider) -> Result<bool> {
Ok(self
.modify_provider("delete-provider", provider, &[])
.await?
.1)
}
async fn modify_provider(
&self,
action: &str,
provider: &Provider,
columns: &[&str],
) -> Result<(Response, bool)> {
let mut provider = provider.clone();
provider.owner = get_owner(&provider.owner, &self.config.organization_name);
let id = format!("{}/{}", provider.owner, provider.name);
self.modify_object(action, &id, &provider, columns).await
}
}