commit: | 27d718e3e1ec45b80422550bb05c00a9127fbd34 |
author: | Trevor Bentley |
committer: | Trevor Bentley |
date: | Tue Apr 11 13:50:33 2017 +0200 |
parents: | c84a696ba07c69cf79ce7f327b6faedc7e97ac06 |
diff --git a/src/http/mod.rs b/src/http/mod.rs line changes: +17/-10 index bfa700e..8e6df9e --- a/src/http/mod.rs +++ b/src/http/mod.rs
@@ -26,7 +26,7 @@ pub enum HttpMethod { pub type HttpErrorString = String; pub struct HttpResponse { - code: Option<u32>, + pub code: Option<u32>, data: Result<String, HttpErrorString>, }
@@ -59,31 +59,38 @@ impl fmt::Display for HttpResponse { } } -pub fn http(url: &str, query: &str, method: HttpMethod, access_token: Option<&str>) -> HttpResponse { +pub fn http(url: &str, query: &str, body: &str, + method: HttpMethod, access_token: Option<&str>) -> HttpResponse { let enc_query = percent_encoding::utf8_percent_encode(&query, percent_encoding::QUERY_ENCODE_SET).collect::<String>(); let mut data = match method { - HttpMethod::POST => { enc_query.as_bytes() } - _ => { query.as_bytes() } + HttpMethod::POST => { enc_query.as_bytes() }, + _ => { body.as_bytes() }, + //_ => { query.as_bytes() } + }; + let query_url = &format!("{}?{}", url, query); + let url = match method { + HttpMethod::GET | HttpMethod::PUT => match query.len() { + 0 => url, + _ => query_url, + }, + _ => url + }; let mut response = None; let mut json_bytes = Vec::<u8>::new(); { let mut easy = Easy::new(); + easy.url(url).unwrap(); match method { - HttpMethod::GET => { - let get_url = format!("{}?{}", url, query); - easy.url(&get_url).unwrap(); - } HttpMethod::POST => { - easy.url(url).unwrap(); easy.post(true).unwrap(); easy.post_field_size(data.len() as u64).unwrap(); } HttpMethod::PUT => { - easy.url(url).unwrap(); easy.put(true).unwrap(); easy.post_field_size(data.len() as u64).unwrap(); } + _ => {} } match access_token {
diff --git a/src/main.rs b/src/main.rs line changes: +15/-16 index 045579b..93d08f1 --- a/src/main.rs +++ b/src/main.rs
@@ -1,6 +1,7 @@ extern crate connectr; use connectr::settings; +use connectr::SpotifyResponse; use std::process;
@@ -23,6 +24,13 @@ use std::process; // w.wait_for_message(); //} +fn require(response: SpotifyResponse) { + match response.code.unwrap() { + 200 ... 299 => (), + _ => panic!("{}", response) + } +} + fn main() { let settings = match settings::read_settings() { Some(s) => s,
@@ -39,23 +47,14 @@ fn main() { 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(connectr::PlayContextOffset{position: Some(5), uri: Some("blah".to_string())}), + offset: Some(connectr::PlayContextOffset{position: Some(2),..Default::default()}), + ..Default::default() }; - 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); + require(spotify.play(None, Some(&ctx))); + require(spotify.pause(None)); + require(spotify.play(None, Some(&ctx))); + require(spotify.pause(None)); + require(spotify.play(None, None)); //systray(player_state); //loop {}
diff --git a/src/webapi/mod.rs b/src/webapi/mod.rs line changes: +16/-27 index c41713b..d9a4cd5 --- a/src/webapi/mod.rs +++ b/src/webapi/mod.rs
@@ -3,7 +3,6 @@ use std::fmt; 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;
@@ -81,7 +80,7 @@ impl fmt::Display for PlayerState { 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); - let json_response = http::http(spotify_api::TOKEN, &query, http::HttpMethod::POST, None).unwrap(); + let json_response = http::http(spotify_api::TOKEN, &query, "", http::HttpMethod::POST, None).unwrap(); parse_spotify_token(&json_response) }
@@ -112,27 +111,16 @@ pub struct PlayContext { impl Default for PlayContext { fn default() -> PlayContext { PlayContext { context_uri: None, uris: None, offset: None } } } - -fn append_to_json_string<T>(json: String, key: String, value: T) -> String - where T: ToJson { - // Highly wasteful implementation. struct -> string -> obj -> string - let jdata = Json::from_str(&json).unwrap(); - let mut jobj = jdata.into_object().unwrap(); - jobj.insert(key, value.to_json()); - Json::Object(jobj).to_string() +impl PlayContext { + pub fn new() -> PlayContext { + PlayContext::default() + } } -fn append_device_id(json: String, device: Option<DeviceId>) -> String { +fn device_id_query(device: Option<DeviceId>) -> String { match device { - Some(x) => { - // Convert empty string to empty JSON - let json = match json.len() { - 0 => "{}".to_string(), - _ => json - }; - append_to_json_string(json, "device_id".to_string(), x) - }, - None => json + Some(x) => format!("device_id={}", x), + None => "".to_string() } }
@@ -150,22 +138,23 @@ impl SpotifyConnectr { self.refresh_token = refresh_token; } pub fn request_device_list(&self) -> ConnectDeviceList { - let json_response = http::http(spotify_api::DEVICES, "", http::HttpMethod::GET, Some(&self.access_token)).unwrap(); + let json_response = http::http(spotify_api::DEVICES, "", "", http::HttpMethod::GET, Some(&self.access_token)).unwrap(); json::decode(&json_response).unwrap() } pub fn request_player_state(&self) -> PlayerState { - let json_response = http::http(spotify_api::PLAYER_STATE, "", http::HttpMethod::GET, Some(&self.access_token)).unwrap(); + 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 = match context { - Some(x) => append_device_id(json::encode(x).unwrap(), device), + let query = device_id_query(device); + let body = match context { + Some(x) => json::encode(x).unwrap(), None => String::new(), }; - http::http(spotify_api::PLAY, &query, http::HttpMethod::PUT, Some(&self.access_token)) + http::http(spotify_api::PLAY, &query, &body, http::HttpMethod::PUT, Some(&self.access_token)) } pub fn pause(&self, device: Option<DeviceId>) -> SpotifyResponse { - let query = append_device_id(String::new(), device); - http::http(spotify_api::PAUSE, &query, http::HttpMethod::PUT, Some(&self.access_token)) + let query = device_id_query(device); + http::http(spotify_api::PAUSE, &query, "", http::HttpMethod::PUT, Some(&self.access_token)) } }