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()
|
||||
{
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -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()),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user