Remove panics in the update_token flow (#688)
It caused a crash when the token failed to update
This commit is contained in:
@@ -77,19 +77,23 @@ impl WebApi {
|
|||||||
.as_ref()
|
.as_ref()
|
||||||
{
|
{
|
||||||
channel.send(cmd).expect("can't send message to worker");
|
channel.send(cmd).expect("can't send message to worker");
|
||||||
let token = futures::executor::block_on(token_rx).unwrap();
|
let token_option = futures::executor::block_on(token_rx).unwrap();
|
||||||
*self.api.token.lock().expect("can't writelock api token") = Some(Token {
|
if let Some(token) = token_option {
|
||||||
access_token: token.access_token,
|
*self.api.token.lock().expect("can't writelock api token") = Some(Token {
|
||||||
expires_in: chrono::Duration::seconds(token.expires_in.into()),
|
access_token: token.access_token,
|
||||||
scopes: HashSet::from_iter(token.scope),
|
expires_in: chrono::Duration::seconds(token.expires_in.into()),
|
||||||
expires_at: None,
|
scopes: HashSet::from_iter(token.scope),
|
||||||
refresh_token: None,
|
expires_at: None,
|
||||||
});
|
refresh_token: None,
|
||||||
*self
|
});
|
||||||
.token_expiration
|
*self
|
||||||
.write()
|
.token_expiration
|
||||||
.expect("could not writelock token") =
|
.write()
|
||||||
Utc::now() + ChronoDuration::seconds(token.expires_in.into());
|
.expect("could not writelock token") =
|
||||||
|
Utc::now() + ChronoDuration::seconds(token.expires_in.into());
|
||||||
|
} else {
|
||||||
|
error!("Failed to update token");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
error!("worker channel is not set");
|
error!("worker channel is not set");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ pub(crate) enum WorkerCommand {
|
|||||||
Stop,
|
Stop,
|
||||||
Seek(u32),
|
Seek(u32),
|
||||||
SetVolume(u16),
|
SetVolume(u16),
|
||||||
RequestToken(oneshot::Sender<Token>),
|
RequestToken(oneshot::Sender<Option<Token>>),
|
||||||
Preload(Playable),
|
Preload(Playable),
|
||||||
Shutdown,
|
Shutdown,
|
||||||
}
|
}
|
||||||
@@ -74,7 +74,7 @@ impl Drop for Worker {
|
|||||||
impl Worker {
|
impl Worker {
|
||||||
fn get_token(
|
fn get_token(
|
||||||
&self,
|
&self,
|
||||||
sender: oneshot::Sender<Token>,
|
sender: oneshot::Sender<Option<Token>>,
|
||||||
) -> Pin<Box<dyn Future<Output = ()> + Send>> {
|
) -> Pin<Box<dyn Future<Output = ()> + Send>> {
|
||||||
let client_id = config::CLIENT_ID;
|
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";
|
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()
|
.mercury()
|
||||||
.get(url)
|
.get(url)
|
||||||
.map(move |response| {
|
.map(move |response| {
|
||||||
let payload = response
|
response.ok().and_then(move |response| {
|
||||||
.as_ref()
|
let payload = response.payload.first()?;
|
||||||
.unwrap()
|
|
||||||
.payload
|
let data = String::from_utf8(payload.clone()).ok()?;
|
||||||
.first()
|
let token: Token = serde_json::from_str(&data).ok()?;
|
||||||
.expect("Empty payload");
|
info!("new token received: {:?}", token);
|
||||||
let data = String::from_utf8(payload.clone()).unwrap();
|
Some(token)
|
||||||
let token: Token = serde_json::from_str(&data).unwrap();
|
})
|
||||||
info!("new token received: {:?}", token);
|
|
||||||
token
|
|
||||||
})
|
})
|
||||||
.map(|token| sender.send(token).unwrap()),
|
.map(|result| sender.send(result).unwrap()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user