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§
Sourceasync fn login(&self) -> Result<String>
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
Sourcefn set_credentials(&mut self, password: &str) -> Result<()>
fn set_credentials(&mut self, password: &str) -> Result<()>
Sourcefn session_id_file(&self) -> &str
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.
Sourcefn secret(&self) -> Secret
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.
Sourcefn retry(&self) -> i32
fn retry(&self) -> i32
Returns current retry attempt count.
Used to track authentication failures and implement retry limits.
Sourcefn inc_retry(&mut self)
fn inc_retry(&mut self)
Increments the retry counter.
Called after each failed authentication attempt to track progress toward the maximum retry limit.
Sourcefn reset_retry(&mut self)
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§
Sourceasync fn get_session_id(&mut self) -> Result<String>
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
- Cache Check: Attempt to restore session from encrypted storage
- Authentication Loop: If no cache, prompt for credentials and authenticate
- Retry Logic: Handle failures with limited retry attempts
- 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
Sourcefn read_session_id(file_name: &str) -> Result<String>
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.
Sourcefn write_session_id(file_name: &str, session_id: &str) -> Result<()>
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 storedsession_id- Session identifier to save
§Returns
Result<()>- Success indicator
§Errors
Returns an error if file creation or writing fails.
Sourcefn delete_session_id(&self) -> Result<()>
fn delete_session_id(&self) -> Result<()>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".