Implement Client::ensure_token
This commit is contained in:
parent
f28d128c20
commit
cce6471a93
|
@ -9,7 +9,7 @@ use url::{form_urlencoded, Url};
|
||||||
|
|
||||||
use error::OAuth2Error;
|
use error::OAuth2Error;
|
||||||
use provider::Provider;
|
use provider::Provider;
|
||||||
use token::{Token, Expiring};
|
use token::{Token, Lifetime, Expiring};
|
||||||
|
|
||||||
use self::response::FromResponse;
|
use self::response::FromResponse;
|
||||||
pub mod response;
|
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));
|
let token = try!(P::Token::from_response_inherit(&json, &token));
|
||||||
Ok(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)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in New Issue