Skip to content
Open
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c90a758
Making this access_token_from_user_client_id refresh if needed
alexjyong Feb 12, 2025
888a0d0
Update cd.yml
alexjyong Feb 12, 2025
3683810
Update cd.yml
alexjyong Feb 12, 2025
dc37c24
Update spotify.rs
alexjyong Feb 12, 2025
0b901ad
Update cd.yml
alexjyong Feb 12, 2025
b91cff0
logging and token refresh
alexjyong Feb 12, 2025
7fc5163
.
alexjyong Feb 12, 2025
60771de
Update token.rs
alexjyong Feb 13, 2025
080e08b
Update spotify.rs
alexjyong Feb 13, 2025
f38e8ef
Update spotify.rs
alexjyong Feb 13, 2025
065a07e
removing extra code
alexjyong Feb 13, 2025
a74cf73
Merge branch 'revamp-token-refreshing' of https://github.com/alexjyon…
alexjyong Feb 13, 2025
9f4af7f
test
alexjyong Feb 13, 2025
cbd231b
.
alexjyong Feb 14, 2025
de90d16
revert test
alexjyong Feb 14, 2025
a2edb9b
.
alexjyong Feb 14, 2025
87efc9d
Update spotify.rs
alexjyong Feb 27, 2025
02cce2b
Update spotify.rs
alexjyong Feb 27, 2025
00d44ad
Update spotify.rs
alexjyong Feb 27, 2025
6d7f9b7
Update cd.yml
alexjyong Feb 27, 2025
03a483b
Update cd.yml
alexjyong Feb 27, 2025
c726207
lint fix
alexjyong Feb 27, 2025
41f8570
lint check
alexjyong Feb 27, 2025
197be3e
.
alexjyong Feb 27, 2025
3eae5ae
trying to pass ci tests
alexjyong Feb 27, 2025
98f1281
Merge branch 'aome510:master' into master
alexjyong Jun 4, 2025
ca8248b
Merge pull request #2 from aome510/master
alexjyong Jun 4, 2025
4322547
Merge branch 'aome510:master' into master
alexjyong Jul 8, 2025
9400ec9
Merge branch 'master' into revamp-token-refreshing
alexjyong Jul 8, 2025
6647628
adding workflow dispatch abilities to the cd workflow for on demand b…
alexjyong Jul 8, 2025
b6d657b
Update cd.yml
alexjyong Jul 8, 2025
ebe1fc4
removing this
alexjyong Jul 8, 2025
8dd8182
Update cd.yml
alexjyong Jul 8, 2025
9e0fab5
Merge branch 'aome510:master' into master
alexjyong Aug 1, 2025
75690be
Merge branch 'master' into revamp-token-refreshing
alexjyong Aug 1, 2025
f6ee459
Merge branch 'master' into revamp-token-refreshing
alexjyong Aug 1, 2025
16f65e1
Merge branch 'revamp-token-refreshing' of https://github.com/alexjyon…
alexjyong Aug 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions spotify_player/src/client/spotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,34 @@ impl Spotify {
/// The function may retrieve a new token and update the current token
/// stored inside the client if the old one is expired.
pub async fn access_token(&self) -> Result<String> {
let should_update = match self.token.lock().await.unwrap().as_ref() {
Some(token) => token.is_expired(),
None => true,
};
if should_update {
let token_guard = self.token.lock().await.unwrap();

if let Some(token) = token_guard.as_ref() {
tracing::info!(
"Current token: {}, expires at: {:?}",
token.access_token,
token.expires_at
);
} else {
tracing::warn!("No token is currently stored.");
}

// If the token is expired, we get a new one
if token_guard.as_ref().is_none_or(rspotify::Token::is_expired) {
tracing::info!("Token expired, restarting session...");
//trying out this awful hack

self.refresh_token().await?;
let session = self.session().await;
session.shutdown();
}

match self.token.lock().await.unwrap().as_ref() {
Some(token) => Ok(token.access_token.clone()),
None => Err(anyhow!(
"failed to get the authentication token stored inside the client."
)),
if let Some(token) = token_guard.as_ref() {
Ok(token.access_token.clone())
} else {
Err(anyhow!(
"Failed to get the authentication token stored inside the client."
))
}
}

Expand Down Expand Up @@ -122,13 +137,27 @@ impl BaseClient for Spotify {
let session = self.session().await;
let old_token = self.token.lock().await.unwrap().clone();

tracing::info!(
"Attempting to refresh token. Current token is: {:?}",
old_token.as_ref().map(|t| &t.access_token)
);

if session.is_invalid() {
tracing::error!("Failed to get a new token: invalid session");
return Ok(old_token);
}

match token::get_token_rspotify(&session, SPOTIFY_CLIENT_ID).await {
Ok(token) => Ok(Some(token)),
Ok(token) => {
{
let mut token_guard = self.token.lock().await.unwrap();
*token_guard = Some(token.clone());
}

tracing::info!("Got new token: {token:?}");

Ok(Some(token))
}
Err(err) => {
tracing::error!("Failed to get a new token: {err:#}");
Ok(old_token)
Expand Down