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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use super::{AccessToken, ClientCredentials};
use crate::model::*;
use crate::CLIENT;
use std::time::Instant;
use tokio::sync::Mutex;
#[derive(Debug)]
pub struct CCFlow<'cc> {
credentials: &'cc ClientCredentials,
cache: Mutex<AccessToken>,
}
impl<'cc> CCFlow<'cc> {
pub fn new(credentials: &'cc ClientCredentials) -> Self {
Self {
credentials,
cache: Mutex::default(),
}
}
pub fn get_credentials(&self) -> &ClientCredentials {
self.credentials
}
pub async fn send(&self) -> Result<AccessToken, EndpointError<AuthenticationError>> {
{
let cache = self.cache.lock().await;
if Instant::now() < cache.expires {
return Ok(cache.clone());
}
}
let response = CLIENT
.post("https://accounts.spotify.com/api/token")
.basic_auth(&self.credentials.id, Some(&self.credentials.secret))
.header("Content-Type", "application/x-www-form-urlencoded")
.body("grant_type=client_credentials")
.send()
.await?;
let status = response.status();
let text = response.text().await?;
if !status.is_success() {
return Err(EndpointError::SpotifyError(serde_json::from_str(&text)?));
}
let token = serde_json::from_str::<AccessToken>(&text)?;
*self.cache.lock().await = token.clone();
Ok(token)
}
}