summary history branches tags files
commit:d02f05230b92b510d14922e3fee27f60ec0db564
author:Trevor Bentley
committer:Trevor Bentley
date:Mon Apr 10 22:32:13 2017 +0200
parents:52f2b5f69dbecc230e2591395fb370701de88bab
Testing JSON stuff for new commands
diff --git a/src/lib.rs b/src/lib.rs
line changes: +1/-0
index dea4e52..3f683d4
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -13,4 +13,5 @@ pub mod spotify_api {
     pub const DEVICES: &'static str = "https://api.spotify.com/v1/me/player/devices";
     pub const PLAYER_STATE: &'static str = "https://api.spotify.com/v1/me/player";
     pub const PLAY: &'static str = "https://api.spotify.com/v1/me/player/play";
+    pub const PAUSE: &'static str = "https://api.spotify.com/v1/me/player/pause";
 }

diff --git a/src/main.rs b/src/main.rs
line changes: +11/-1
index 9e2f10f..43d0e20
--- a/src/main.rs
+++ b/src/main.rs
@@ -39,7 +39,17 @@ fn main() {
     }
     println!("State: {:?}", player_state);
 
-    spotify.play_uri("spotify:user:mrmekon:playlist:4XqYlbPdDUsranzjicPCgf");
+    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),
+    };
+    spotify.play(None, Some(&ctx));
+    spotify.pause(None);
+    spotify.play(None, None);
 
     //systray(player_state);
     //loop {}

diff --git a/src/webapi/mod.rs b/src/webapi/mod.rs
line changes: +41/-3
index 327d2e6..0840da9
--- a/src/webapi/mod.rs
+++ b/src/webapi/mod.rs
@@ -1,6 +1,7 @@
 extern crate rustc_serialize;
 use self::rustc_serialize::{Decodable, Decoder, json};
 use self::rustc_serialize::json::Json;
+use self::rustc_serialize::json::ToJson;
 
 use super::http;
 use super::settings;
@@ -53,7 +54,7 @@ pub struct ConnectDeviceList {
 pub struct PlayerState {
     pub timestamp: u64,
     pub device: ConnectDevice,
-    pub progress_ms: u32,
+    pub progress_ms: Option<u32>,
     pub is_playing: bool,
     pub shuffle_state: bool,
     pub repeat_state: String,
@@ -73,6 +74,28 @@ pub struct SpotifyConnectr {
     refresh_token: String,
 }
 
+pub type DeviceId = String;
+
+#[derive(RustcDecodable, RustcEncodable)]
+pub struct PlayContextOffset {
+    pub position: Option<u32>,
+    pub uri: Option<String>,
+}
+impl Default for PlayContextOffset {
+    fn default() -> PlayContextOffset { PlayContextOffset { position: None, uri: None } }
+}
+
+#[derive(RustcDecodable, RustcEncodable)]
+pub struct PlayContext {
+    pub context_uri: Option<String>,
+    pub uris: Option<Vec<String>>,
+    pub offset: Option<PlayContextOffset>,
+}
+impl Default for PlayContext {
+    fn default() -> PlayContext { PlayContext { context_uri: None, uris: None, offset: None } }
+}
+
+
 impl SpotifyConnectr {
     pub fn new(settings: settings::Settings) -> SpotifyConnectr {
         SpotifyConnectr {settings: settings, auth_code: String::new(),
@@ -92,8 +115,23 @@ 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_uri(&self, uri: &str) {
-        let query = format!("{{\"context_uri\": \"{}\"}}", uri);
+    pub fn play(&self, device: Option<DeviceId>, context: Option<&PlayContext>) {
+        let query = match context {
+            Some(x) => {
+                //json::encode(x).unwrap()
+                let jstr = json::encode(x).unwrap();
+                let jdata = Json::from_str(&jstr).unwrap();
+                let jobj = jdata.into_object().unwrap();
+                jobj.insert("device".to_string(), device.unwrap().to_json());
+                String::new()
+            },
+            None => String::new(),
+        };
+        println!("PUT query: {}", query);
         let _ = http::http(spotify_api::PLAY, &query, http::HttpMethod::PUT, Some(&self.access_token));
     }
+    pub fn pause(&self, device: Option<DeviceId>) {
+        let query = String::new();
+        let _ = http::http(spotify_api::PAUSE, &query, http::HttpMethod::PUT, Some(&self.access_token));
+    }
 }