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
//! The [Client
//! Credentials](https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow)
//! Spotify authorization flow.

use super::{AccessToken, ClientCredentials};
use crate::model::*;
use crate::CLIENT;
use std::time::Instant;
use tokio::sync::Mutex;

/// An object that holds a reference to your client credentials, and caches access tokens with the
/// Client Credentials authorization flow.
///
/// # Examples
/// ```no_run
/// # async {
/// use aspotify::{ClientCredentials, CCFlow};
///
/// // Create a flow, taking credentials from environment variables.
/// let flow = CCFlow::new(
///     ClientCredentials::from_env()
///         .expect("CLIENT_ID or CLIENT_SECRET environment variables not set")
/// );
///
/// // Get a client credentials access token with:
/// let token = flow.send().await.unwrap();
/// # };
/// ```
#[derive(Debug)]
pub struct CCFlow<'cc> {
    credentials: &'cc ClientCredentials,
    cache: Mutex<AccessToken>,
}

impl<'cc> CCFlow<'cc> {
    /// Creates a new CCFlow from the client's credentials.
    pub fn new(credentials: &'cc ClientCredentials) -> Self {
        Self {
            credentials,
            cache: Mutex::default(),
        }
    }
    /// Get the client credentials.
    pub fn get_credentials(&self) -> &ClientCredentials {
        self.credentials
    }
    /// Return the cache or send a request.
    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)
    }
}