#![allow(clippy::too_many_arguments)]
use uuid::Uuid;
use crate::AxiamError;
use crate::client::AxiamClient;
use crate::management::models;
use crate::management::page::{Page, PageRequest, collect_pages};
use crate::management::request::{Call, Verb};
use crate::management::scope::Scope;
#[derive(Clone, Copy)]
pub struct Users<'c> {
pub(crate) client: &'c AxiamClient,
pub(crate) scope: Scope,
}
impl std::fmt::Debug for Users<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Users").field("scope", &self.scope).finish()
}
}
impl<'c> Users<'c> {
pub(crate) fn new(client: &'c AxiamClient) -> Self {
Self {
client,
scope: Scope::default(),
}
}
pub async fn list(&self, page: PageRequest) -> Result<Page<models::UserResponse>, AxiamError> {
let query = page.query();
let call = Call {
operation: "users.list",
verb: Verb::Get,
path_template: "/api/v1/users",
path: "/api/v1/users".to_string(),
query: &query,
};
self.client
.management_send::<_, Page<models::UserResponse>>(call, None::<&()>)
.await
}
pub async fn list_all(
&self,
start: PageRequest,
) -> Result<Vec<models::UserResponse>, AxiamError> {
collect_pages(start, |p| self.list(p)).await
}
pub async fn create(
&self,
body: &models::CreateUserRequest,
) -> Result<models::UserResponse, AxiamError> {
let body = models::CreateUserRequestWire::from(body);
let body = &body;
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "users.create",
verb: Verb::Post,
path_template: "/api/v1/users",
path: "/api/v1/users".to_string(),
query: &query,
};
self.client
.management_send::<_, models::UserResponse>(call, Some(body))
.await
}
pub async fn get(&self, user_id: Uuid) -> Result<models::UserResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "users.get",
verb: Verb::Get,
path_template: "/api/v1/users/{user_id}",
path: format!("/api/v1/users/{user_id}"),
query: &query,
};
self.client
.management_send::<_, models::UserResponse>(call, None::<&()>)
.await
}
pub async fn update(
&self,
user_id: Uuid,
body: &models::UpdateUserRequest,
) -> Result<models::UserResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "users.update",
verb: Verb::Put,
path_template: "/api/v1/users/{user_id}",
path: format!("/api/v1/users/{user_id}"),
query: &query,
};
self.client
.management_send::<_, models::UserResponse>(call, Some(body))
.await
}
pub async fn delete(&self, user_id: Uuid) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "users.delete",
verb: Verb::Delete,
path_template: "/api/v1/users/{user_id}",
path: format!("/api/v1/users/{user_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn list_mfa_methods(
&self,
user_id: Uuid,
) -> Result<Vec<models::MfaMethodResponse>, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "users.list_mfa_methods",
verb: Verb::Get,
path_template: "/api/v1/users/{user_id}/mfa-methods",
path: format!("/api/v1/users/{user_id}/mfa-methods"),
query: &query,
};
self.client
.management_send::<_, Vec<models::MfaMethodResponse>>(call, None::<&()>)
.await
}
pub async fn delete_mfa_method(
&self,
user_id: Uuid,
method_id: &str,
) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "users.delete_mfa_method",
verb: Verb::Delete,
path_template: "/api/v1/users/{user_id}/mfa-methods/{method_id}",
path: format!("/api/v1/users/{user_id}/mfa-methods/{method_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn reset_mfa(&self, user_id: Uuid) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "users.reset_mfa",
verb: Verb::Post,
path_template: "/api/v1/users/{user_id}/reset-mfa",
path: format!("/api/v1/users/{user_id}/reset-mfa"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn unlock(&self, user_id: Uuid) -> Result<models::UserResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "users.unlock",
verb: Verb::Post,
path_template: "/api/v1/users/{user_id}/unlock",
path: format!("/api/v1/users/{user_id}/unlock"),
query: &query,
};
self.client
.management_send::<_, models::UserResponse>(call, None::<&()>)
.await
}
pub async fn list_roles(
&self,
user_id: Uuid,
) -> Result<Vec<models::RoleAssignment>, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "users.list_roles",
verb: Verb::Get,
path_template: "/api/v1/users/{user_id}/roles",
path: format!("/api/v1/users/{user_id}/roles"),
query: &query,
};
self.client
.management_send::<_, Vec<models::RoleAssignment>>(call, None::<&()>)
.await
}
pub async fn list_sessions(
&self,
user_id: Uuid,
) -> Result<Vec<models::SessionResponse>, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "users.list_sessions",
verb: Verb::Get,
path_template: "/api/v1/users/{user_id}/sessions",
path: format!("/api/v1/users/{user_id}/sessions"),
query: &query,
};
self.client
.management_send::<_, Vec<models::SessionResponse>>(call, None::<&()>)
.await
}
}