aboutsummaryrefslogtreecommitdiff
path: root/src/pushbullet
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2015-11-02 17:39:28 -0500
committerDavid Li <li.davidm96@gmail.com>2015-11-02 17:39:28 -0500
commitfc0ef639499765507cc0ab7718b5cbc87eebdb96 (patch)
treeeb331ea7ddd3cf41d87dc8d2dfa7ec6413da0804 /src/pushbullet
parent4554b2a4533181886446985c9858f1e49deb4670 (diff)
Implement user info
Diffstat (limited to 'src/pushbullet')
-rw-r--r--src/pushbullet/mod.rs54
-rw-r--r--src/pushbullet/user.rs21
2 files changed, 67 insertions, 8 deletions
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)
+ }
+}