use serde::{
Deserialize,
Serialize,
};
use crate::{
CoralogixRegion,
RUSTC_VERSION,
SDK_CORRELATION_ID_HEADER_NAME,
SDK_LANGUAGE_HEADER_NAME,
SDK_RUSTC_VERSION_HEADER_NAME,
SDK_VERSION,
SDK_VERSION_HEADER_NAME,
auth::{
ApiKey,
AuthContext,
},
};
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScimUser {
pub schemas: Vec<String>,
pub id: Option<String>,
pub user_name: String,
pub active: bool,
pub name: ScimUserName,
pub emails: Vec<ScimUserEmail>,
pub groups: Vec<ScimUserGroup>,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScimUserName {
pub given_name: String,
pub family_name: String,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScimUserEmail {
pub value: String,
pub r#type: Option<String>,
pub primary: bool,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScimUserGroup {
pub value: String,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ListResponse {
pub schemas: Vec<String>,
pub total_results: usize,
#[serde(rename = "Resources")]
pub resources: Vec<ScimUser>,
}
pub struct UsersClient {
target_url: String,
http_client: reqwest::Client,
api_key: ApiKey,
correlation_id: String,
}
impl UsersClient {
pub fn new(region: CoralogixRegion, auth_context: AuthContext) -> Self {
Self {
http_client: reqwest::Client::new(),
api_key: auth_context.team_level_api_key,
target_url: region.rest_endpoint() + "/scim/Users",
correlation_id: uuid::Uuid::new_v4().to_string(),
}
}
pub async fn create(&self, user: ScimUser) -> Result<ScimUser, reqwest::Error> {
let response = self
.http_client
.post(&self.target_url)
.header("Authorization", format!("Bearer {}", self.api_key.token()))
.header(SDK_VERSION_HEADER_NAME, SDK_VERSION)
.header(SDK_LANGUAGE_HEADER_NAME, "rust")
.header(SDK_RUSTC_VERSION_HEADER_NAME, RUSTC_VERSION)
.header(SDK_CORRELATION_ID_HEADER_NAME, &self.correlation_id)
.json(&user)
.send()
.await?;
response.json().await
}
pub async fn get(&self, id: &str) -> Result<ScimUser, reqwest::Error> {
let url = format!("{}/{}", self.target_url, id);
let response = self
.http_client
.get(&url)
.header("Authorization", format!("Bearer {}", self.api_key.token()))
.header(SDK_VERSION_HEADER_NAME, SDK_VERSION)
.header(SDK_LANGUAGE_HEADER_NAME, "rust")
.header(SDK_RUSTC_VERSION_HEADER_NAME, RUSTC_VERSION)
.header(SDK_CORRELATION_ID_HEADER_NAME, &self.correlation_id)
.send()
.await?;
response.json().await
}
pub async fn update(&self, user: ScimUser) -> Result<ScimUser, reqwest::Error> {
let url = format!("{}/{}", self.target_url, user.id.as_ref().unwrap());
let response = self
.http_client
.put(&url)
.header("Authorization", format!("Bearer {}", self.api_key.token()))
.header(SDK_VERSION_HEADER_NAME, SDK_VERSION)
.header(SDK_LANGUAGE_HEADER_NAME, "rust")
.header(SDK_RUSTC_VERSION_HEADER_NAME, RUSTC_VERSION)
.header(SDK_CORRELATION_ID_HEADER_NAME, &self.correlation_id)
.json(&user)
.send()
.await?;
response.json().await
}
pub async fn delete(&self, id: &str) -> Result<(), reqwest::Error> {
let url = format!("{}/{}", self.target_url, id);
let response = self
.http_client
.delete(&url)
.header("Authorization", format!("Bearer {}", self.api_key.token()))
.header(SDK_VERSION_HEADER_NAME, SDK_VERSION)
.header(SDK_LANGUAGE_HEADER_NAME, "rust")
.header(SDK_RUSTC_VERSION_HEADER_NAME, RUSTC_VERSION)
.header(SDK_CORRELATION_ID_HEADER_NAME, &self.correlation_id)
.send()
.await?;
response.error_for_status()?;
Ok(())
}
pub async fn list(&self) -> Result<Vec<ScimUser>, reqwest::Error> {
let response = self
.http_client
.get(&self.target_url)
.header("Authorization", format!("Bearer {}", self.api_key.token()))
.header(SDK_VERSION_HEADER_NAME, SDK_VERSION)
.header(SDK_LANGUAGE_HEADER_NAME, "rust")
.header(SDK_RUSTC_VERSION_HEADER_NAME, RUSTC_VERSION)
.header(SDK_CORRELATION_ID_HEADER_NAME, &self.correlation_id)
.send()
.await?;
let list_response: ListResponse = response.json().await?;
Ok(list_response.resources)
}
}