Struct TokenManager

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for TokenManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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, 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
Source§

impl<T> ErasedDestructor for T
where T: 'static,