2013-09-18 07:33:31 +00:00
|
|
|
|
using System;
|
2015-06-28 08:50:19 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2014-07-04 08:09:48 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using FluentValidation.Results;
|
|
|
|
|
using NLog;
|
2015-06-28 08:50:19 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
2013-09-18 07:33:31 +00:00
|
|
|
|
using RestSharp;
|
|
|
|
|
using NzbDrone.Core.Rest;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Notifications.PushBullet
|
|
|
|
|
{
|
|
|
|
|
public interface IPushBulletProxy
|
|
|
|
|
{
|
2015-06-28 08:50:19 +00:00
|
|
|
|
void SendNotification(string title, string message, PushBulletSettings settings);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
ValidationFailure Test(PushBulletSettings settings);
|
2013-09-18 07:33:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
|
public class PushBulletProxy : IPushBulletProxy
|
2013-09-18 07:33:31 +00:00
|
|
|
|
{
|
2014-07-04 08:09:48 +00:00
|
|
|
|
private readonly Logger _logger;
|
2015-06-28 08:50:19 +00:00
|
|
|
|
private const string URL = "https://api.pushbullet.com/v2/pushes";
|
2013-09-18 07:33:31 +00:00
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
|
public PushBulletProxy(Logger logger)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-28 08:50:19 +00:00
|
|
|
|
public void SendNotification(string title, string message, PushBulletSettings settings)
|
2013-09-18 07:33:31 +00:00
|
|
|
|
{
|
2015-06-28 08:50:19 +00:00
|
|
|
|
var channelTags = GetIds(settings.ChannelTags);
|
|
|
|
|
var deviceIds = GetIds(settings.DeviceIds);
|
|
|
|
|
var error = false;
|
2014-01-09 17:04:43 +00:00
|
|
|
|
|
2015-06-28 08:50:19 +00:00
|
|
|
|
if (channelTags.Any())
|
2014-01-09 17:04:43 +00:00
|
|
|
|
{
|
2015-06-28 08:50:19 +00:00
|
|
|
|
foreach (var channelTag in channelTags)
|
|
|
|
|
{
|
|
|
|
|
var request = BuildChannelRequest(channelTag);
|
2014-01-09 17:04:43 +00:00
|
|
|
|
|
2015-06-28 08:50:19 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SendNotification(title, message, request, settings);
|
|
|
|
|
}
|
|
|
|
|
catch (PushBulletException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Unable to send test message to: " + channelTag, ex);
|
|
|
|
|
error = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-09 17:04:43 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2015-06-28 08:50:19 +00:00
|
|
|
|
if (deviceIds.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var deviceId in deviceIds)
|
|
|
|
|
{
|
|
|
|
|
var request = BuildDeviceRequest(deviceId);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SendNotification(title, message, request, settings);
|
|
|
|
|
}
|
|
|
|
|
catch (PushBulletException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Unable to send test message to: " + deviceId, ex);
|
|
|
|
|
error = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var request = BuildDeviceRequest(null);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SendNotification(title, message, request, settings);
|
|
|
|
|
}
|
|
|
|
|
catch (PushBulletException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Unable to send test message to all devices", ex);
|
|
|
|
|
error = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-09 17:04:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-28 08:50:19 +00:00
|
|
|
|
if (error)
|
|
|
|
|
{
|
|
|
|
|
throw new PushBulletException("Unable to send PushBullet notifications to all channels or devices");
|
|
|
|
|
}
|
2014-01-09 17:04:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
|
public ValidationFailure Test(PushBulletSettings settings)
|
2013-09-18 07:33:31 +00:00
|
|
|
|
{
|
2014-07-04 08:09:48 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-11-25 15:21:33 +00:00
|
|
|
|
const string title = "Sonarr - Test Notification";
|
|
|
|
|
const string body = "This is a test message from Sonarr";
|
2014-07-04 08:09:48 +00:00
|
|
|
|
|
2015-06-28 08:50:19 +00:00
|
|
|
|
SendNotification(title, body, settings);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
}
|
|
|
|
|
catch (RestException ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("API Key is invalid: " + ex.Message, ex);
|
|
|
|
|
return new ValidationFailure("ApiKey", "API Key is invalid");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.ErrorException("Unable to send test message: " + ex.Message, ex);
|
|
|
|
|
return new ValidationFailure("ApiKey", "Unable to send test message");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Unable to send test message: " + ex.Message, ex);
|
|
|
|
|
return new ValidationFailure("", "Unable to send test message");
|
|
|
|
|
}
|
2013-09-18 07:33:31 +00:00
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
|
return null;
|
2013-09-18 07:33:31 +00:00
|
|
|
|
}
|
2015-06-28 08:50:19 +00:00
|
|
|
|
|
|
|
|
|
private RestRequest BuildDeviceRequest(string deviceId)
|
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest(Method.POST);
|
|
|
|
|
long integerId;
|
|
|
|
|
|
|
|
|
|
if (Int64.TryParse(deviceId, out integerId))
|
|
|
|
|
{
|
|
|
|
|
request.AddParameter("device_id", integerId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
request.AddParameter("device_iden", deviceId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RestRequest BuildChannelRequest(string channelTag)
|
|
|
|
|
{
|
|
|
|
|
var request = new RestRequest(Method.POST);
|
|
|
|
|
request.AddParameter("channel_tag", channelTag);
|
|
|
|
|
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendNotification(string title, string message, RestRequest request, PushBulletSettings settings)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var client = RestClientFactory.BuildClient(URL);
|
|
|
|
|
|
|
|
|
|
request.AddParameter("type", "note");
|
|
|
|
|
request.AddParameter("title", title);
|
|
|
|
|
request.AddParameter("body", message);
|
|
|
|
|
|
|
|
|
|
client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, String.Empty);
|
|
|
|
|
client.ExecuteAndValidate(request);
|
|
|
|
|
}
|
|
|
|
|
catch (RestException ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("API Key is invalid: " + ex.Message, ex);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new PushBulletException("Unable to send text message: {0}", ex, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<string> GetIds(string input)
|
|
|
|
|
{
|
|
|
|
|
if (input.IsNullOrWhiteSpace()) return new List<string>();
|
|
|
|
|
|
|
|
|
|
return input.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
|
|
|
|
|
}
|
2013-09-18 07:33:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|