pub struct TokenManager { /* private fields */ }
Expand description
A manager for handling OAuth tokens.
This struct is responsible for caching an internally lazily created OAuth token. Every time you get the token, it checks if it is expired and creates a new one if necessary. Each token is valid for one hour (the maximum provided by Google).
§Example
use std::fs::File;
use oauth_fcm::TokenManager;
let mut token_manager = TokenManager::new(File::open("./tests/mock_credentials.json").expect("Failed to open file")).expect("Failed to create TokenManager");
let token = token_manager.get_token().await.expect("Failed to get token");
Implementations§
Source§impl TokenManager
impl TokenManager
Sourcepub fn new<T: Read + Debug>(credentials: T) -> Result<Self, FcmError>
pub fn new<T: Read + Debug>(credentials: T) -> Result<Self, FcmError>
Creates a new TokenManager
.
The recommended way to crate a TokenManager
is to use the
create_shared_token_manager
function in lib.rs
.
§Arguments
google_credentials_location
- A string slice that holds the path to the Google credentials JSON file.
§Errors
This function will return an error if the Google credentials could not be read or parsed.
Sourcepub async fn get_token(&mut self) -> Result<String, FcmError>
pub async fn get_token(&mut self) -> Result<String, FcmError>
Returns the current OAuth token.
This function checks if the current token is expired and refreshes it if necessary. Users normally only need this function to get the token, as it handles the token expiration internally.
§Errors
This function will return an error if the token could not be refreshed.
Sourcepub fn is_token_expired(&self) -> bool
pub fn is_token_expired(&self) -> bool
Checks if the current OAuth token is expired.
This function is used internally by get_token
and is not typically
needed by users.
Sourcepub async fn refresh_token(&mut self) -> Result<String, FcmError>
pub async fn refresh_token(&mut self) -> Result<String, FcmError>
Refreshes the current OAuth token.
This function is used internally by get_token
and is not typically
needed by users.
§Errors
This function will return an error if the token could not be refreshed.
Sourcepub async fn refresh_token_with_url(
&mut self,
auth_server_url: &str,
) -> Result<String, FcmError>
pub async fn refresh_token_with_url( &mut self, auth_server_url: &str, ) -> Result<String, FcmError>
Refreshes the current OAuth token with a custom auth server URL.
This function exists for testing purposes and is not typically needed by users.
§Arguments
auth_server_url
- A string slice that holds the custom auth server URL.
§Errors
This function will return an error if the token could not be refreshed.