From 166f2a38c84a1da336fac5b77f6f0b6959948382 Mon Sep 17 00:00:00 2001 From: Jaex Date: Sun, 30 Mar 2014 12:49:43 +0300 Subject: [PATCH] Pushbullet send note & link functions --- UploadersLib/FileUploaders/Pushbullet.cs | 53 ++++++++++++++++++++---- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/UploadersLib/FileUploaders/Pushbullet.cs b/UploadersLib/FileUploaders/Pushbullet.cs index cc4a70843..56867cb34 100644 --- a/UploadersLib/FileUploaders/Pushbullet.cs +++ b/UploadersLib/FileUploaders/Pushbullet.cs @@ -47,12 +47,8 @@ public Pushbullet(PushbulletSettings config) Config = config; } - public override UploadResult Upload(Stream stream, string fileName) + public UploadResult PushFile(Stream stream, string fileName) { - if (string.IsNullOrEmpty(Config.UserAPIKey)) throw new Exception("Missing API Key."); - if (Config.CurrentDevice == null) throw new Exception("No device set to push to."); - if (string.IsNullOrEmpty(Config.CurrentDevice.Key)) throw new Exception("Device key is empty."); - NameValueCollection headers = CreateAuthenticationHeader(Config.UserAPIKey, ""); Dictionary args = new Dictionary(); @@ -61,13 +57,13 @@ public override UploadResult Upload(Stream stream, string fileName) UploadResult result = UploadData(stream, "https://api.pushbullet.com/api/pushes", fileName, arguments: args, headers: headers); - PushbulletResponsePush response = JsonConvert.DeserializeObject(result.Response); + PushbulletResponsePush push = JsonConvert.DeserializeObject(result.Response); - if (response != null) + if (push != null) { if (Config.ReturnPushURL) { - result.URL = "https://www.pushbullet.com/pushes?push_iden=" + response.iden; + result.URL = "https://www.pushbullet.com/pushes?push_iden=" + push.iden; } else { @@ -78,6 +74,47 @@ public override UploadResult Upload(Stream stream, string fileName) return result; } + private string Push(string pushType, string valueType, string value, string title) + { + NameValueCollection headers = CreateAuthenticationHeader(Config.UserAPIKey, ""); + + Dictionary args = new Dictionary(); + args.Add("device_iden", Config.CurrentDevice.Key); + args.Add("type", pushType); + args.Add("title", title); + args.Add(valueType, value); + + string response = SendPostRequest("https://api.pushbullet.com/api/pushes", args, headers: headers); + + PushbulletResponsePush push = JsonConvert.DeserializeObject(response); + + if (push != null) + { + return "https://www.pushbullet.com/pushes?push_iden=" + push.iden; + } + + return null; + } + + public string PushNote(string note, string title) + { + return Push("note", "body", note, title); + } + + public string PushLink(string link, string title) + { + return Push("link", "url", link, title); + } + + public override UploadResult Upload(Stream stream, string fileName) + { + if (string.IsNullOrEmpty(Config.UserAPIKey)) throw new Exception("Missing API key."); + if (Config.CurrentDevice == null) throw new Exception("No device set to push to."); + if (string.IsNullOrEmpty(Config.CurrentDevice.Key)) throw new Exception("Missing device key."); + + return PushFile(stream, fileName); + } + public List GetDeviceList() { NameValueCollection headers = CreateAuthenticationHeader(Config.UserAPIKey, "");