Skip to content

Commit 9ff155f

Browse files
committed
cleanup new_session API
1 parent b4c8dd6 commit 9ff155f

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

spotify_player/src/cli/handlers.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ fn try_connect_to_client(socket: &UdpSocket, configs: &config::Configs) -> Resul
153153
let rt = tokio::runtime::Runtime::new()?;
154154

155155
// create a Spotify API client
156-
// TODO: figure out how to create session for CLI use case
157156
let client = client::Client::new(auth_config);
157+
rt.block_on(client.new_session(None, false))
158+
.context("new session")?;
158159

159160
// create a client socket for handling CLI commands
160161
let client_socket = rt.block_on(tokio::net::UdpSocket::bind(("127.0.0.1", port)))?;

spotify_player/src/client/mod.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use std::io::Write;
88

99
use anyhow::Context as _;
1010
use anyhow::Result;
11-
use librespot_core::authentication::Credentials;
12-
use librespot_core::session::Session;
1311
use reqwest::StatusCode;
1412
use rspotify::{
1513
http::Query,
@@ -81,30 +79,46 @@ impl Client {
8179
}
8280

8381
/// Create a new client session
84-
pub async fn new_session(&self, state: &SharedState, reauth: bool) -> Result<()> {
82+
pub async fn new_session(&self, state: Option<&SharedState>, reauth: bool) -> Result<()> {
8583
let session = self.auth_config.new_session();
8684
let creds = auth::get_creds(&self.auth_config, reauth)
8785
.await
8886
.context("get credentials")?;
8987
*self.session.lock().await = Some(session.clone());
9088

89+
#[allow(unused_mut)]
90+
let mut connected = false;
91+
9192
#[cfg(feature = "streaming")]
92-
if state.is_streaming_enabled() {
93-
self.new_streaming_connection(state.clone(), session, creds)
93+
if let Some(state) = state {
94+
if state.is_streaming_enabled() {
95+
self.new_streaming_connection(state.clone(), session.clone(), creds.clone())
96+
.await
97+
.context("new streaming connection")?;
98+
connected = true;
99+
}
100+
}
101+
102+
if !connected {
103+
// if session is not connected (triggered by `new_streaming_connection`), connect to the session
104+
session
105+
.connect(creds, true)
94106
.await
95-
.context("new streaming connection")?;
107+
.context("connect to a session")?;
96108
}
97109

98110
tracing::info!("Used a new session for Spotify client.");
99111

100112
self.refresh_token().await.context("refresh auth token")?;
101113

102-
// reset the application's caches
103-
state.data.write().caches = MemoryCaches::new();
114+
if let Some(state) = state {
115+
// reset the application's caches
116+
state.data.write().caches = MemoryCaches::new();
104117

105-
self.initialize_playback(state)
106-
.await
107-
.context("initialize playback")?;
118+
self.initialize_playback(state)
119+
.await
120+
.context("initialize playback")?;
121+
}
108122

109123
Ok(())
110124
}
@@ -113,7 +127,7 @@ impl Client {
113127
pub async fn check_valid_session(&self, state: &SharedState) -> Result<()> {
114128
if self.session().await.is_invalid() {
115129
tracing::info!("Client's current session is invalid, creating a new session...");
116-
self.new_session(state, false)
130+
self.new_session(Some(state), false)
117131
.await
118132
.context("create new client session")?;
119133
}
@@ -125,8 +139,8 @@ impl Client {
125139
pub async fn new_streaming_connection(
126140
&self,
127141
state: SharedState,
128-
session: Session,
129-
creds: Credentials,
142+
session: librespot_core::Session,
143+
creds: librespot_core::authentication::Credentials,
130144
) -> Result<()> {
131145
let new_conn =
132146
crate::streaming::new_connection(self.clone(), state, session, creds).await?;
@@ -290,7 +304,7 @@ impl Client {
290304
}
291305
#[cfg(feature = "streaming")]
292306
ClientRequest::RestartIntegratedClient => {
293-
self.new_session(state, false).await?;
307+
self.new_session(Some(state), false).await?;
294308
}
295309
ClientRequest::GetCurrentUser => {
296310
let user = self.current_user().await?;

spotify_player/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ fn init_logging(cache_folder: &std::path::Path) -> Result<()> {
7474

7575
#[tokio::main]
7676
async fn start_app(state: &state::SharedState) -> Result<()> {
77-
// librespot depends on hyper-rustls which requires a crypto provider to be set up.
78-
// TODO: see if this can be fixed upstream
79-
rustls::crypto::aws_lc_rs::default_provider()
80-
.install_default()
81-
.unwrap();
82-
8377
let configs = config::get_config();
8478

8579
if !state.is_daemon {
@@ -126,7 +120,7 @@ async fn start_app(state: &state::SharedState) -> Result<()> {
126120
let auth_config = auth::AuthConfig::new(configs)?;
127121
let client = client::Client::new(auth_config);
128122
client
129-
.new_session(state, true)
123+
.new_session(Some(state), true)
130124
.await
131125
.context("initialize new Spotify session")?;
132126

@@ -228,6 +222,12 @@ async fn start_app(state: &state::SharedState) -> Result<()> {
228222
}
229223

230224
fn main() -> Result<()> {
225+
// librespot depends on hyper-rustls which requires a crypto provider to be set up.
226+
// TODO: see if this can be fixed upstream
227+
rustls::crypto::aws_lc_rs::default_provider()
228+
.install_default()
229+
.unwrap();
230+
231231
// parse command line arguments
232232
let args = cli::init_cli()?.get_matches();
233233

0 commit comments

Comments
 (0)