Implement Client::ensure_token

This commit is contained in:
Curtis McEnroe 2015-12-24 15:49:48 -05:00
parent f28d128c20
commit cce6471a93
1 changed files with 10 additions and 1 deletions

View File

@ -9,7 +9,7 @@ use url::{form_urlencoded, Url};
use error::OAuth2Error;
use provider::Provider;
use token::{Token, Expiring};
use token::{Token, Lifetime, Expiring};
use self::response::FromResponse;
pub mod response;
@ -181,6 +181,15 @@ impl<P: Provider> Client<P> where P::Token: Token<Expiring> {
let token = try!(P::Token::from_response_inherit(&json, &token));
Ok(token)
}
/// Ensures an access token is valid by refreshing it if necessary.
pub fn ensure_token(&self, token: P::Token) -> Result<P::Token, ClientError> {
if token.lifetime().expired() {
self.refresh_token(token, None)
} else {
Ok(token)
}
}
}
#[cfg(test)]