1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use crate::error::Error;
use crate::token::Token;
use crate::token_source::TokenSource;
use async_trait::async_trait;
#[derive(Debug)]
pub struct ReuseTokenSource {
target: Box<dyn TokenSource>,
current_token: std::sync::RwLock<Token>,
}
impl ReuseTokenSource {
pub(crate) fn new(target: Box<dyn TokenSource>, token: Token) -> ReuseTokenSource {
ReuseTokenSource {
target,
current_token: std::sync::RwLock::new(token),
}
}
}
#[async_trait]
impl TokenSource for ReuseTokenSource {
async fn token(&self) -> Result<Token, Error> {
{
let r_lock = self.current_token.read().unwrap();
if r_lock.valid() {
return Ok(Token {
access_token: r_lock.access_token.to_string(),
token_type: r_lock.token_type.to_string(),
expiry: r_lock.expiry,
});
}
}
let token = self.target.token().await?;
*self.current_token.write().unwrap() = token.clone();
return Ok(token);
}
}