Remove panics in the update_token flow (#688)

It caused a crash when the token failed to update
This commit is contained in:
Csaba Varró
2022-01-01 21:43:12 +01:00
committed by GitHub
parent 4d1388e5bd
commit f0a2cd79ff
2 changed files with 28 additions and 26 deletions

View File

@@ -77,19 +77,23 @@ impl WebApi {
.as_ref()
{
channel.send(cmd).expect("can't send message to worker");
let token = futures::executor::block_on(token_rx).unwrap();
*self.api.token.lock().expect("can't writelock api token") = Some(Token {
access_token: token.access_token,
expires_in: chrono::Duration::seconds(token.expires_in.into()),
scopes: HashSet::from_iter(token.scope),
expires_at: None,
refresh_token: None,
});
*self
.token_expiration
.write()
.expect("could not writelock token") =
Utc::now() + ChronoDuration::seconds(token.expires_in.into());
let token_option = futures::executor::block_on(token_rx).unwrap();
if let Some(token) = token_option {
*self.api.token.lock().expect("can't writelock api token") = Some(Token {
access_token: token.access_token,
expires_in: chrono::Duration::seconds(token.expires_in.into()),
scopes: HashSet::from_iter(token.scope),
expires_at: None,
refresh_token: None,
});
*self
.token_expiration
.write()
.expect("could not writelock token") =
Utc::now() + ChronoDuration::seconds(token.expires_in.into());
} else {
error!("Failed to update token");
}
} else {
error!("worker channel is not set");
}

View File

@@ -26,7 +26,7 @@ pub(crate) enum WorkerCommand {
Stop,
Seek(u32),
SetVolume(u16),
RequestToken(oneshot::Sender<Token>),
RequestToken(oneshot::Sender<Option<Token>>),
Preload(Playable),
Shutdown,
}
@@ -74,7 +74,7 @@ impl Drop for Worker {
impl Worker {
fn get_token(
&self,
sender: oneshot::Sender<Token>,
sender: oneshot::Sender<Option<Token>>,
) -> Pin<Box<dyn Future<Output = ()> + Send>> {
let client_id = config::CLIENT_ID;
let scopes = "user-read-private,playlist-read-private,playlist-read-collaborative,playlist-modify-public,playlist-modify-private,user-follow-modify,user-follow-read,user-library-read,user-library-modify,user-top-read,user-read-recently-played";
@@ -87,18 +87,16 @@ impl Worker {
.mercury()
.get(url)
.map(move |response| {
let payload = response
.as_ref()
.unwrap()
.payload
.first()
.expect("Empty payload");
let data = String::from_utf8(payload.clone()).unwrap();
let token: Token = serde_json::from_str(&data).unwrap();
info!("new token received: {:?}", token);
token
response.ok().and_then(move |response| {
let payload = response.payload.first()?;
let data = String::from_utf8(payload.clone()).ok()?;
let token: Token = serde_json::from_str(&data).ok()?;
info!("new token received: {:?}", token);
Some(token)
})
})
.map(|token| sender.send(token).unwrap()),
.map(|result| sender.send(result).unwrap()),
)
}