Overview
The Growatt SDK automatically manages session state, including expiry tracking and automatic re-authentication. This ensures your API calls continue to work seamlessly even when sessions expire.Session Duration
By default, sessions last for 30 minutes. You can customize this duration when creating the client.Default Duration
Custom Duration
Via Environment Variable
Session State Methods
is_logged_in()
Check the current authentication status.Signature
Returns
Returns
true if a session exists, false otherwise. Does not validate session expiry.Example
is_logged_in() only checks if a session was established. It does not validate whether the session has expired. Use is_session_valid() for expiry checks.is_session_valid()
Validates whether the current session has expired.Signature
Returns
Returns
true if session exists and has not expired, false otherwiseBehavior
- Compares current time with session expiry timestamp
- Returns
falseif no session expiry is set - Does not make network requests
ensure_session()
Ensures a valid session exists, automatically re-authenticating if needed.Signature
Returns
Returns
Ok(()) if session is valid or re-authentication succeeded, Err(GrowattError::NotLoggedIn) if no credentials are storedBehavior
- Checks if session is logged in and valid
- If session is invalid or expired, attempts to re-authenticate using stored credentials
- Returns error if no credentials were stored (manual
login()required) - Called automatically by all API methods before making requests
ensure_session() is a private method called internally by all API methods. You don’t need to call it manually.Automatic Session Renewal
All API methods automatically check and renew sessions before making requests. This is handled by the internalcheck_login() method.
How It Works
- When you call any API method (e.g.,
get_plants()), it callscheck_login()first check_login()callsensure_session()- If the session is expired,
ensure_session()automatically re-authenticates using stored credentials - The API request proceeds with a fresh session
Example
Session Lifecycle
Error Scenarios
No Credentials Stored
If the session expires and no credentials were stored, API methods will return an error:Network Failures During Renewal
Best Practices
Credential Storage: Always use
login() with credentials to enable automatic session renewal. This allows the SDK to seamlessly handle expired sessions.Source Reference
is_logged_in()implementation: src/lib.rs:746is_session_valid()implementation: src/lib.rs:228ensure_session()implementation: src/lib.rs:237- Session duration configuration: src/lib.rs:96, src/lib.rs:146
- Auto-renewal in API methods: src/lib.rs:286