Skip to main content

UsersClient

Struct UsersClient 

Source
pub struct UsersClient { /* private fields */ }
Expand description

Client for user management operations.

This client provides methods to list and update application users. It relies on a valid session stored in the cache, which must be obtained by first logging in via LoginClient.

§Example

use cekunit_client::api::dashboard::UsersClient;
use std::collections::HashMap;

let client = UsersClient::new()?;

// Fetch the first page of users, sorted by name ascending
let html = client.get_users_list(Some(1), Some("name"), Some("asc"))?;

// Update an existing user (ID = 42)
let mut updates = HashMap::new();
updates.insert("name", "New Name");
updates.insert("email", "new@example.com");
client.update_user("42", updates)?;

Implementations§

Source§

impl UsersClient

Source

pub fn new() -> Result<Self, ApiError>

Creates a new UsersClient with default configuration loaded from environment variables.

This is a convenience constructor that loads the configuration and creates a cache manager.

§Errors

Returns ApiError if:

  • Environment variables are missing or invalid.
  • The cache directory cannot be created.
  • The HTTP client cannot be built.
Source

pub fn with_config(config: EnvConfig) -> Result<Self, ApiError>

Creates a new UsersClient with a given configuration.

This allows using a pre‑loaded configuration, for example when sharing configuration between multiple clients.

§Arguments
  • config - The environment configuration to use.
§Errors

Returns ApiError if:

  • The cache directory cannot be created.
  • The HTTP client cannot be built.
Source

pub fn with_config_and_cache( config: EnvConfig, cache_manager: CacheManager, ) -> Result<Self, ApiError>

Creates a new UsersClient with a given configuration and an existing cache manager.

This is useful when sharing the same cache (and thus the same session) across multiple clients.

§Arguments
  • config - The environment configuration.
  • cache_manager - An existing cache manager (typically from the main client).
§Errors

Returns ApiError if the HTTP client cannot be built.

Source

pub fn get_users_list( &self, page: Option<u32>, sort: Option<&str>, direction: Option<&str>, ) -> Result<String, ApiError>

Fetches the users list HTML with optional pagination and sorting.

This method sends a GET request to the users listing endpoint and returns the raw HTML of the page.

§Arguments
  • page - Optional page number (1‑based). If None, the first page is assumed.
  • sort - Optional column name to sort by (e.g., "name", "email").
  • direction - Optional sort direction ("asc" or "desc").
§Returns

The raw HTML of the users list as a String.

§Errors

Returns ApiError if:

  • No valid session exists.
  • The HTTP request fails (network, timeout).
  • The server returns a non‑success status (4xx or 5xx).
  • The response body cannot be read.
Source

pub fn update_user( &self, id: &str, data: HashMap<&str, &str>, ) -> Result<(), ApiError>

Updates an existing user’s details.

This method sends a POST request with _method=PUT to the user item endpoint. The CSRF token is automatically included, and the caller provides the fields to update.

§Arguments
  • id - The identifier of the user to update.
  • data - A map of field names to new values. The map must not include _token or _method.
§Errors

Returns ApiError if:

  • No valid session exists.
  • The HTTP request fails.
  • The server returns a non‑success status (2xx or 302 is considered success).
§Example
let mut updates = HashMap::new();
updates.insert("name", "Jane Doe");
updates.insert("email", "jane@example.com");
client.update_user("5", updates)?;
Source

pub fn get_csrf_token(&self) -> Result<String, ApiError>

Fetches a fresh CSRF token from the users list page.

This method retrieves the first page of the users list using get_users_list and extracts the CSRF token from the HTML. The token can be used for subsequent POST requests if needed.

§Returns

The CSRF token as a string.

§Errors

Returns ApiError if:

  • No valid session exists.
  • The users list page cannot be fetched.
  • No CSRF token is found in the HTML.
Source

pub fn config(&self) -> &EnvConfig

Returns a reference to the environment configuration.

Source

pub fn cache_manager(&self) -> &CacheManager

Returns a reference to the cache manager.

Trait Implementations§

Source§

impl FromContext for UsersClient

Source§

fn from_ctx(ctx: Arc<ClientContext>) -> Result<Self, ApiError>

Creates an instance of Self from the given shared context. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more