Skip to main content

Session

Trait Session 

Source
pub trait Session {
    // Required methods
    async fn login(&self) -> Result<String>;
    fn set_credentials(&mut self, password: &str) -> Result<()>;
    fn session_id_file(&self) -> &str;
    fn secret(&self) -> Secret;
    fn retry(&self) -> i32;
    fn inc_retry(&mut self);
    fn reset_retry(&mut self);

    // Provided methods
    async fn get_session_id(&mut self) -> Result<String> { ... }
    fn read_session_id(file_name: &str) -> Result<String> { ... }
    fn write_session_id(file_name: &str, session_id: &str) -> Result<()> { ... }
    fn delete_session_id(&self) -> Result<()> { ... }
}
Expand description

Common session management trait for all API clients.

Provides a standardized interface for handling authentication, session caching, and credential management across different API providers.

Required Methods§

Source

async fn login(&self) -> Result<String>

Performs authentication and returns a session identifier.

This method handles the actual API authentication process using stored credentials. The returned session ID can be used for subsequent API calls.

§Returns
  • Result<String> - Session identifier on success, error on failure
§Errors

Returns an error if:

  • Network connection fails
  • Credentials are invalid
  • API returns an unexpected response format
Source

fn set_credentials(&mut self, password: &str) -> Result<()>

Sets user credentials for authentication.

Stores the provided password in memory for use during authentication. The password may be encoded or hashed depending on the API requirements.

§Arguments
  • password - User password in plain text
§Errors

Returns an error if password encoding/validation fails.

Source

fn session_id_file(&self) -> &str

Returns the filename used for session storage.

Each API client uses a unique session file to avoid conflicts. Files are stored in the application’s data directory with restricted permissions.

Source

fn secret(&self) -> Secret

Returns the secret manager for this API client.

Provides access to encrypted credential storage and interactive prompting specific to this API provider.

Source

fn retry(&self) -> i32

Returns current retry attempt count.

Used to track authentication failures and implement retry limits.

Source

fn inc_retry(&mut self)

Increments the retry counter.

Called after each failed authentication attempt to track progress toward the maximum retry limit.

Source

fn reset_retry(&mut self)

Resets the retry counter to zero.

Called after successful authentication to ensure future sessions don’t inherit retry state from previous attempts.

Provided Methods§

Source

async fn get_session_id(&mut self) -> Result<String>

Retrieves or establishes a valid session ID.

This is the main entry point for session management. It handles the complete session lifecycle including cache restoration, authentication, and retry logic.

§Process Flow
  1. Cache Check: Attempt to restore session from encrypted storage
  2. Authentication Loop: If no cache, prompt for credentials and authenticate
  3. Retry Logic: Handle failures with limited retry attempts
  4. Session Storage: Cache successful sessions for future use
§Returns
  • Result<String> - Valid session ID ready for API calls
§Errors

Returns an error if:

  • Maximum retry attempts exceeded
  • Storage operations fail
  • Network or API errors prevent authentication
Source

fn read_session_id(file_name: &str) -> Result<String>

Reads a session ID from the specified file.

Attempts to load a cached session identifier from disk storage. The session may be encrypted depending on the implementation.

§Arguments
  • file_name - Path to the session storage file
§Returns
  • Result<String> - Session ID if file exists and is readable
§Errors

Returns an error if the file doesn’t exist, is unreadable, or contains invalid session data.

Source

fn write_session_id(file_name: &str, session_id: &str) -> Result<()>

Writes a session ID to the specified file.

Stores the session identifier for future use, potentially with encryption. The file is created with restricted permissions for security.

§Arguments
  • file_name - Path where session should be stored
  • session_id - Session identifier to save
§Returns
  • Result<()> - Success indicator
§Errors

Returns an error if file creation or writing fails.

Source

fn delete_session_id(&self) -> Result<()>

Deletes the cached session file.

Removes the session cache when authentication fails or sessions expire. This forces fresh authentication on the next session request.

§Returns
  • Result<()> - Success indicator
§Errors

Returns an error if file deletion fails. Missing files are not considered errors.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§