summary history branches tags files
commit:c84a696ba07c69cf79ce7f327b6faedc7e97ac06
author:Trevor Bentley
committer:Trevor Bentley
date:Tue Apr 11 12:46:02 2017 +0200
parents:b1dbbba75b075aaf422502caf88c76cd34941f35
Display options for action results
diff --git a/src/http/mod.rs b/src/http/mod.rs
line changes: +2/-2
index 3fcda88..bfa700e
--- a/src/http/mod.rs
+++ b/src/http/mod.rs
@@ -53,8 +53,8 @@ impl fmt::Display for HttpResponse {
         };
         let _ = write!(f, "Code: {}\n", code);
         match self.data {
-            Ok(ref s) => {write!(f, "{}\n", s)}
-            Err(ref s) => {write!(f, "ERROR: {}\n", s)}
+            Ok(ref s) => {write!(f, "Response: {}", s)}
+            Err(ref s) => {write!(f, "ERROR: {}", s)}
         }
     }
 }

diff --git a/src/main.rs b/src/main.rs
line changes: +17/-13
index 0b15a28..045579b
--- a/src/main.rs
+++ b/src/main.rs
@@ -34,24 +34,28 @@ fn main() {
     let device_list = spotify.request_device_list();
     let player_state = spotify.request_player_state();
 
-    for dev in device_list.devices {
-        println!("{:?}", dev);
-    }
-    println!("State: {:?}", player_state);
+    println!("Devices:\n{}", device_list);
+    println!("State:\n{}", player_state);
 
-    let offset = connectr::PlayContextOffset {
-        position: Some(5), uri: Some("blah".to_string()),
-    };
     let ctx = connectr::PlayContext {
         context_uri: Some("spotify:user:mrmekon:playlist:4XqYlbPdDUsranzjicPCgf".to_string()),
         uris: Some(vec!["one".to_string(), "two".to_string()]),
-        offset: Some(offset),
+        offset: Some(connectr::PlayContextOffset{position: Some(5), uri: Some("blah".to_string())}),
     };
-    spotify.play(Some("deviceid".to_string()), Some(&ctx));
-    spotify.pause(Some("deviceid".to_string()));
-    spotify.play(None, Some(&ctx));
-    spotify.pause(None);
-    spotify.play(None, None);
+    let res = spotify.play(Some("deviceid".to_string()), Some(&ctx));
+    println!("result: {}", res);
+
+    let res = spotify.pause(Some("deviceid".to_string()));
+    println!("result: {}", res);
+
+    let res = spotify.play(None, Some(&ctx));
+    println!("result: {}", res);
+
+    let res = spotify.pause(None);
+    println!("result: {}", res);
+
+    let res = spotify.play(None, None);
+    println!("result: {}", res);
 
     //systray(player_state);
     //loop {}

diff --git a/src/webapi/mod.rs b/src/webapi/mod.rs
line changes: +24/-4
index 234c38b..c41713b
--- a/src/webapi/mod.rs
+++ b/src/webapi/mod.rs
@@ -1,3 +1,5 @@
+use std::fmt;
+
 extern crate rustc_serialize;
 use self::rustc_serialize::{Decodable, Decoder, json};
 use self::rustc_serialize::json::Json;
@@ -6,6 +8,7 @@ use self::rustc_serialize::json::ToJson;
 use super::http;
 use super::settings;
 use super::spotify_api;
+use super::http::HttpResponse;
 
 pub fn parse_spotify_token(json: &str) -> (String, String) {
     let json_data = Json::from_str(&json).unwrap();
@@ -50,6 +53,15 @@ pub struct ConnectDeviceList {
     pub devices: Vec<ConnectDevice>
 }
 
+impl fmt::Display for ConnectDeviceList {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        for dev in &self.devices {
+            let _ = write!(f, "{:?}\n", dev);
+        }
+        Ok(())
+    }
+}
+
 #[derive(RustcDecodable, RustcEncodable, Debug)]
 pub struct PlayerState {
     pub timestamp: u64,
@@ -60,6 +72,12 @@ pub struct PlayerState {
     pub repeat_state: String,
 }
 
+impl fmt::Display for PlayerState {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{:?}\n", self)
+    }
+}
+
 pub fn request_oauth_tokens(auth_code: &str, settings: &settings::Settings) -> (String, String) {
     let query = format!("grant_type=authorization_code&code={}&redirect_uri=http://127.0.0.1:{}&client_id={}&client_secret={}",
                         auth_code, settings.port, settings.client_id, settings.secret);
@@ -118,6 +136,8 @@ fn append_device_id(json: String, device: Option<DeviceId>) -> String {
     }
 }
 
+pub type SpotifyResponse = HttpResponse;
+
 impl SpotifyConnectr {
     pub fn new(settings: settings::Settings) -> SpotifyConnectr {
         SpotifyConnectr {settings: settings, auth_code: String::new(),
@@ -137,15 +157,15 @@ 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>) {
+    pub fn play(&self, device: Option<DeviceId>, context: Option<&PlayContext>) -> SpotifyResponse {
         let query = match context {
             Some(x) => append_device_id(json::encode(x).unwrap(), device),
             None => String::new(),
         };
-        let _ = http::http(spotify_api::PLAY, &query, http::HttpMethod::PUT, Some(&self.access_token));
+        http::http(spotify_api::PLAY, &query, http::HttpMethod::PUT, Some(&self.access_token))
     }
-    pub fn pause(&self, device: Option<DeviceId>) {
+    pub fn pause(&self, device: Option<DeviceId>) -> SpotifyResponse {
         let query = append_device_id(String::new(), device);
-        let _ = http::http(spotify_api::PAUSE, &query, http::HttpMethod::PUT, Some(&self.access_token));
+        http::http(spotify_api::PAUSE, &query, http::HttpMethod::PUT, Some(&self.access_token))
     }
 }