Skip to content

Commit

Permalink
core: SessionHandle -> SessionService
Browse files Browse the repository at this point in the history
  • Loading branch information
jpochyla committed Jul 10, 2021
1 parent 122606b commit 7f5ee36
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 31 deletions.
8 changes: 4 additions & 4 deletions psst-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use psst_core::{
connection::Credentials,
error::Error,
item_id::{ItemId, ItemIdType},
session::{SessionConfig, SessionHandle},
session::{SessionConfig, SessionService},
};
use std::{env, io, io::BufRead, path::PathBuf, thread};

Expand All @@ -22,7 +22,7 @@ fn main() {
env::var("SPOTIFY_USERNAME").unwrap(),
env::var("SPOTIFY_PASSWORD").unwrap(),
);
let session = SessionHandle::new();
let session = SessionService::new();

let connection = session
.connect(SessionConfig {
Expand All @@ -40,7 +40,7 @@ fn main() {
processing.join().unwrap();
}

fn start(track_id: &str, session: SessionHandle) -> Result<(), Error> {
fn start(track_id: &str, session: SessionService) -> Result<(), Error> {
let cdn = Cdn::new(session.clone(), None)?;
let cache = Cache::new(PathBuf::from("cache"))?;
let item_id = ItemId::from_base62(track_id, ItemIdType::Track).unwrap();
Expand All @@ -56,7 +56,7 @@ fn start(track_id: &str, session: SessionHandle) -> Result<(), Error> {
}

fn play_item(
session: SessionHandle,
session: SessionService,
cdn: CdnHandle,
cache: CacheHandle,
item: PlaybackItem,
Expand Down
6 changes: 3 additions & 3 deletions psst-core/src/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use serde::Deserialize;

use crate::{error::Error, session::SessionHandle};
use crate::{error::Error, session::SessionService};

// Client ID of the official Web Spotify front-end.
const CLIENT_ID: &str = "65b708073fc0480ea92a077233ca87bd";
Expand All @@ -32,7 +32,7 @@ impl AccessToken {
}
}

pub fn request(session: &SessionHandle) -> Result<Self, Error> {
pub fn request(session: &SessionService) -> Result<Self, Error> {
#[derive(Deserialize)]
struct MercuryAccessToken {
#[serde(rename = "expiresIn")]
Expand Down Expand Up @@ -68,7 +68,7 @@ impl TokenProvider {
}
}

pub fn get(&self, session: &SessionHandle) -> Result<AccessToken, Error> {
pub fn get(&self, session: &SessionService) -> Result<AccessToken, Error> {
let mut token = self
.token
.lock()
Expand Down
18 changes: 9 additions & 9 deletions psst-core/src/audio_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
item_id::{ItemId, ItemIdType},
metadata::{Fetch, ToAudioPath},
protocol::metadata::Track,
session::SessionHandle,
session::SessionService,
};

const PREVIOUS_TRACK_THRESHOLD: Duration = Duration::from_secs(3);
Expand Down Expand Up @@ -50,7 +50,7 @@ pub struct PlaybackItem {
impl PlaybackItem {
fn load(
&self,
session: &SessionHandle,
session: &SessionService,
cdn: CdnHandle,
cache: CacheHandle,
config: &PlaybackConfig,
Expand All @@ -70,7 +70,7 @@ impl PlaybackItem {

fn load_audio_path(
item_id: ItemId,
session: &SessionHandle,
session: &SessionService,
cache: &CacheHandle,
config: &PlaybackConfig,
) -> Result<AudioPath, Error> {
Expand All @@ -84,7 +84,7 @@ fn load_audio_path(

fn load_audio_path_from_track_or_alternative(
item_id: ItemId,
session: &SessionHandle,
session: &SessionService,
cache: &CacheHandle,
config: &PlaybackConfig,
) -> Result<AudioPath, Error> {
Expand Down Expand Up @@ -120,7 +120,7 @@ fn load_audio_path_from_track_or_alternative(
Ok(path)
}

fn get_country_code(session: &SessionHandle, cache: &CacheHandle) -> Option<String> {
fn get_country_code(session: &SessionService, cache: &CacheHandle) -> Option<String> {
if let Some(cached_country_code) = cache.get_country_code() {
Some(cached_country_code)
} else {
Expand All @@ -134,7 +134,7 @@ fn get_country_code(session: &SessionHandle, cache: &CacheHandle) -> Option<Stri

fn load_track(
item_id: ItemId,
session: &SessionHandle,
session: &SessionService,
cache: &CacheHandle,
) -> Result<Track, Error> {
if let Some(cached_track) = cache.get_track(item_id) {
Expand All @@ -150,7 +150,7 @@ fn load_track(

fn load_audio_key(
path: &AudioPath,
session: &SessionHandle,
session: &SessionService,
cache: &CacheHandle,
) -> Result<AudioKey, Error> {
if let Some(cached_key) = cache.get_audio_key(path.item_id, path.file_id) {
Expand All @@ -175,7 +175,7 @@ pub struct LoadedPlaybackItem {
pub struct Player {
state: PlayerState,
preload: PreloadState,
session: SessionHandle,
session: SessionService,
cdn: CdnHandle,
cache: CacheHandle,
config: PlaybackConfig,
Expand All @@ -189,7 +189,7 @@ pub struct Player {

impl Player {
pub fn new(
session: SessionHandle,
session: SessionService,
cdn: CdnHandle,
cache: CacheHandle,
config: PlaybackConfig,
Expand Down
6 changes: 3 additions & 3 deletions psst-core/src/cdn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ use std::{
use serde::Deserialize;

use crate::{
access_token::TokenProvider, error::Error, item_id::FileId, session::SessionHandle,
access_token::TokenProvider, error::Error, item_id::FileId, session::SessionService,
util::default_ureq_agent_builder,
};

pub type CdnHandle = Arc<Cdn>;

pub struct Cdn {
session: SessionHandle,
session: SessionService,
agent: ureq::Agent,
token_provider: TokenProvider,
}

impl Cdn {
pub fn new(session: SessionHandle, proxy_url: Option<&str>) -> Result<CdnHandle, Error> {
pub fn new(session: SessionService, proxy_url: Option<&str>) -> Result<CdnHandle, Error> {
let agent = default_ureq_agent_builder(proxy_url)?.build();
Ok(Arc::new(Self {
session,
Expand Down
4 changes: 2 additions & 2 deletions psst-core/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use crate::{
error::Error,
item_id::{FileId, ItemId, ItemIdType},
protocol::metadata::{Restriction, Track},
session::SessionHandle,
session::SessionService,
};

pub trait Fetch: MessageRead<'static> {
fn uri(id: ItemId) -> String;
fn fetch(session: &SessionHandle, id: ItemId) -> Result<Self, Error> {
fn fetch(session: &SessionService, id: ItemId) -> Result<Self, Error> {
session.connected()?.get_mercury_protobuf(Self::uri(id))
}
}
Expand Down
4 changes: 2 additions & 2 deletions psst-core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct SessionConfig {
}

#[derive(Clone)]
pub struct SessionHandle {
pub struct SessionService {
inner: Arc<Mutex<InnerHandle>>,
}

Expand Down Expand Up @@ -84,7 +84,7 @@ impl InnerHandle {
}
}

impl SessionHandle {
impl SessionService {
pub fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(InnerHandle {
Expand Down
4 changes: 2 additions & 2 deletions psst-gui/src/controller/playback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use psst_core::{
audio_player::{PlaybackConfig, PlaybackItem, Player, PlayerCommand, PlayerEvent},
cache::Cache,
cdn::Cdn,
session::SessionHandle,
session::SessionService,
};
#[cfg(target_os = "windows")]
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
Expand Down Expand Up @@ -48,7 +48,7 @@ impl PlaybackController {

fn open_audio_output_and_start_threads(
&mut self,
session: SessionHandle,
session: SessionService,
config: PlaybackConfig,
event_sink: ExtEventSink,
widget_id: WidgetId,
Expand Down
6 changes: 3 additions & 3 deletions psst-gui/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ use druid::{
im::{HashSet, Vector},
Data, Lens,
};
use psst_core::session::SessionHandle;
use psst_core::session::SessionService;
use std::{sync::Arc, time::Duration};

#[derive(Clone, Data, Lens)]
pub struct AppState {
#[data(ignore)]
pub session: SessionHandle,
pub session: SessionService,

pub route: Nav,
pub history: Vector<Nav>,
Expand All @@ -58,7 +58,7 @@ pub struct AppState {
impl Default for AppState {
fn default() -> Self {
Self {
session: SessionHandle::new(),
session: SessionService::new(),
route: Nav::Home,
history: Vector::new(),
config: Config::default(),
Expand Down
6 changes: 3 additions & 3 deletions psst-gui/src/webapi/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use druid::{im::Vector, image, Data};
use once_cell::sync::OnceCell;
use psst_core::{
access_token::TokenProvider, session::SessionHandle, util::default_ureq_agent_builder,
access_token::TokenProvider, session::SessionService, util::default_ureq_agent_builder,
};
use serde::{de::DeserializeOwned, Deserialize};
use std::{
Expand All @@ -24,15 +24,15 @@ use ureq::{Agent, Request, Response};
use super::cache::WebApiCache;

pub struct WebApi {
session: SessionHandle,
session: SessionService,
agent: Agent,
cache: WebApiCache,
token_provider: TokenProvider,
}

impl WebApi {
pub fn new(
session: SessionHandle,
session: SessionService,
proxy_url: Option<&str>,
cache_base: Option<PathBuf>,
) -> Self {
Expand Down

0 comments on commit 7f5ee36

Please sign in to comment.