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
---
Cargo.lock | 1 +
Cargo.toml | 3 ++-
src/main.rs | 12 +++++++++--
src/message/mod.rs | 2 +-
src/pushbullet/mod.rs | 54 ++++++++++++++++++++++++++++++++++++++++++--------
src/pushbullet/user.rs | 21 ++++++++++++++++++++
6 files changed, 81 insertions(+), 12 deletions(-)
create mode 100644 src/pushbullet/user.rs
diff --git a/Cargo.lock b/Cargo.lock
index fb839df..ed42d80 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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)",
diff --git a/Cargo.toml b/Cargo.toml
index 7ecd3f5..27545db 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,4 +7,5 @@ authors = ["David Li "]
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 = ::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