summary history branches tags files
commit:423432c99d662652deebda3183e2718d7909a710
author:Trevor Bentley
committer:Trevor Bentley
date:Tue Apr 11 15:07:44 2017 +0200
parents:768aa22650a9542eb003f723657ca30a2130203c
Assign target device ID to Connect session instead of each action
diff --git a/src/main.rs b/src/main.rs
line changes: +3/-3
index 3e42113..2ffdb49
--- a/src/main.rs
+++ b/src/main.rs
@@ -50,7 +50,7 @@ fn main() {
         .offset_position(2)
         .build();
 
-    require(spotify.play(None, Some(&ctx)));
-    require(spotify.pause(None));
-    require(spotify.play(None, None));
+    spotify.set_target_device(None);
+    require(spotify.play(Some(&ctx)));
+    require(spotify.pause());
 }

diff --git a/src/webapi/mod.rs b/src/webapi/mod.rs
line changes: +20/-15
index 41d2458..281b20b
--- a/src/webapi/mod.rs
+++ b/src/webapi/mod.rs
@@ -84,13 +84,6 @@ pub fn request_oauth_tokens(auth_code: &str, settings: &settings::Settings) -> (
     parse_spotify_token(&json_response)
 }
 
-pub struct SpotifyConnectr {
-    settings: settings::Settings,
-    auth_code: String,
-    access_token: String,
-    refresh_token: String,
-}
-
 pub type DeviceId = String;
 
 #[derive(RustcDecodable, RustcEncodable)]
@@ -167,19 +160,28 @@ impl PlayContext {
     }
 }
 
-fn device_id_query(device: Option<DeviceId>) -> String {
+fn device_id_query(device: &Option<DeviceId>) -> String {
     match device {
-        Some(x) => format!("device_id={}", x),
-        None => "".to_string()
+        &Some(ref x) => format!("device_id={}", x),
+        &None => "".to_string()
     }
 }
 
 pub type SpotifyResponse = HttpResponse;
 
+pub struct SpotifyConnectr {
+    settings: settings::Settings,
+    auth_code: String,
+    access_token: String,
+    refresh_token: String,
+    device: Option<DeviceId>,
+}
+
 impl SpotifyConnectr {
     pub fn new(settings: settings::Settings) -> SpotifyConnectr {
         SpotifyConnectr {settings: settings, auth_code: String::new(),
-                         access_token: String::new(), refresh_token: String::new()}
+                         access_token: String::new(), refresh_token: String::new(),
+                         device: None}
     }
     pub fn connect(&mut self) {
         self.auth_code = http::authenticate(&self.settings);
@@ -195,16 +197,19 @@ impl SpotifyConnectr {
         let json_response = http::http(spotify_api::PLAYER_STATE, "", "", http::HttpMethod::GET, Some(&self.access_token)).unwrap();
         json::decode(&json_response).unwrap()
     }
-    pub fn play(&self, device: Option<DeviceId>, context: Option<&PlayContext>) -> SpotifyResponse {
-        let query = device_id_query(device);
+    pub fn set_target_device(&mut self, device: Option<DeviceId>) {
+        self.device = device;
+    }
+    pub fn play(&self, context: Option<&PlayContext>) -> SpotifyResponse {
+        let query = device_id_query(&self.device);
         let body = match context {
             Some(x) => json::encode(x).unwrap(),
             None => String::new(),
         };
         http::http(spotify_api::PLAY, &query, &body, http::HttpMethod::PUT, Some(&self.access_token))
     }
-    pub fn pause(&self, device: Option<DeviceId>) -> SpotifyResponse {
-        let query = device_id_query(device);
+    pub fn pause(&self) -> SpotifyResponse {
+        let query = device_id_query(&self.device);
         http::http(spotify_api::PAUSE, &query, "", http::HttpMethod::PUT, Some(&self.access_token))
     }
 }