From fc0ef639499765507cc0ab7718b5cbc87eebdb96 Mon Sep 17 00:00:00 2001 From: David Li Date: Mon, 2 Nov 2015 17:39:28 -0500 Subject: Implement user info --- src/pushbullet/mod.rs | 54 ++++++++++++++++++++++++++++++++++++++++++-------- src/pushbullet/user.rs | 21 ++++++++++++++++++++ 2 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 src/pushbullet/user.rs (limited to 'src/pushbullet') 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 = ::std::result::Result; + +#[derive(Debug)] +pub enum Error { + RequestError(hyper::Error), + JsonError(rustc_serialize::json::DecoderError), +} pub struct PBClient { + token: String, client: websocket::client::Client, websocket::client::receiver::Receiver> @@ -26,11 +41,12 @@ impl PBClient { response.validate().unwrap(); PBClient { + token: token.to_string(), client: response.begin() } } - fn process_message(message: Result) -> Option { + fn process_message(message: ::std::result::Result) -> Option { 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 { + 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 { + let json = try!(json::Json::from_str(user)); + let mut decoder = json::Decoder::new(json); + Decodable::decode(&mut decoder) + } +} -- cgit v1.2.3