diff options
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/main.rs | 12 | ||||
-rw-r--r-- | src/message/mod.rs | 2 | ||||
-rw-r--r-- | src/pushbullet/mod.rs | 54 | ||||
-rw-r--r-- | src/pushbullet/user.rs | 21 |
6 files changed, 81 insertions, 12 deletions
@@ -2,6 +2,7 @@ name = "rust-pushbullet" version = "0.1.0" dependencies = [ + "hyper 0.6.15 (registry+https://github.com/rust-lang/crates.io-index)", "notify-rust 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)", @@ -7,4 +7,5 @@ authors = ["David Li <li.davidm96@gmail.com>"] websocket = "~0.12.2" rustc-serialize = "0.3" toml = "0.1" -notify-rust = "3.0" +notify-rust = "3.0.1" +hyper = "0.6" diff --git a/src/main.rs b/src/main.rs index 7570dcb..523772c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,12 @@ +#[macro_use] extern crate hyper; +extern crate notify_rust; +extern crate rustc_serialize; +extern crate toml; +extern crate websocket; + use std::fs::File; use std::io::prelude::*; -extern crate notify_rust; -extern crate toml; use notify_rust::Notification; pub mod message; @@ -27,6 +31,10 @@ fn main() { let mut client = PBClient::new(token.as_ref()); + println!("Starting Pushbullet client..."); + + println!("{:?}", client.get_user().expect("Could not get user info from server!")); + for message in client.messages() { match message { Message::Push(Push::Mirror { diff --git a/src/message/mod.rs b/src/message/mod.rs index 0617c4d..44ba075 100644 --- a/src/message/mod.rs +++ b/src/message/mod.rs @@ -1,6 +1,6 @@ extern crate rustc_serialize; -use self::rustc_serialize::{json, Decodable, Decoder}; +use rustc_serialize::{json, Decodable, Decoder}; #[derive(Debug)] pub enum Push { diff --git a/src/pushbullet/mod.rs b/src/pushbullet/mod.rs index 1f2254e..6eff8ca 100644 --- a/src/pushbullet/mod.rs +++ b/src/pushbullet/mod.rs @@ -1,16 +1,31 @@ -extern crate websocket; +extern crate hyper; extern crate rustc_serialize; +extern crate websocket; use message::Message; +pub mod user; + +use std::io::Read; + +use websocket::Sender; +use websocket::Receiver; +use websocket::WebSocketStream; +use websocket::DataFrame; +use websocket::Client; +use websocket::client::request::Url; -use self::websocket::Sender; -use self::websocket::Receiver; -use self::websocket::WebSocketStream; -use self::websocket::DataFrame; -use self::websocket::Client; -use self::websocket::client::request::Url; +header! { (AccessToken, "Access-Token") => [String] } + +pub type Result<T> = ::std::result::Result<T, Error>; + +#[derive(Debug)] +pub enum Error { + RequestError(hyper::Error), + JsonError(rustc_serialize::json::DecoderError), +} pub struct PBClient { + token: String, client: websocket::client::Client<websocket::dataframe::DataFrame, websocket::client::sender::Sender<websocket::stream::WebSocketStream>, websocket::client::receiver::Receiver<websocket::stream::WebSocketStream>> @@ -26,11 +41,12 @@ impl PBClient { response.validate().unwrap(); PBClient { + token: token.to_string(), client: response.begin() } } - fn process_message(message: Result<websocket::Message, websocket::result::WebSocketError>) -> Option<Message> { + fn process_message(message: ::std::result::Result<websocket::Message, websocket::result::WebSocketError>) -> Option<Message> { let message = match message { Ok(m) => m, Err(e) => { @@ -51,4 +67,26 @@ impl PBClient { let mut receiver = self.client.get_mut_reciever(); // there is a typo in the API Box::new(receiver.incoming_messages().filter_map(PBClient::process_message)) } + + pub fn get_user(&self) -> Result<user::User> { + let mut client = hyper::Client::new(); + + let request = client.get("https://api.pushbullet.com/v2/users/me") + .header(AccessToken(self.token.clone())) + .send(); + + // TODO: implement From and use try! + let mut res = match request { + Ok(r) => r, + Err(e) => return Err(Error::RequestError(e)) + }; + + let mut body = String::new(); + res.read_to_string(&mut body).unwrap(); + + match user::User::parse(body.as_ref()) { + Ok(user) => Ok(user), + Err(e) => Err(Error::JsonError(e)) + } + } } diff --git a/src/pushbullet/user.rs b/src/pushbullet/user.rs new file mode 100644 index 0000000..c22dfdd --- /dev/null +++ b/src/pushbullet/user.rs @@ -0,0 +1,21 @@ +extern crate rustc_serialize; + +use rustc_serialize::{json, Decodable, Decoder}; + +#[derive(RustcDecodable,Debug)] +pub struct User { + iden: String, + email: String, + email_normalized: String, + name: String, + image_url: String, + max_upload_size: i32, +} + +impl User { + pub fn parse(user: &str) -> Result<User, json::DecoderError> { + let json = try!(json::Json::from_str(user)); + let mut decoder = json::Decoder::new(json); + Decodable::decode(&mut decoder) + } +} |