2013-09-18 07:33:31 +00:00
|
|
|
|
using System;
|
2015-06-28 08:50:19 +00:00
|
|
|
|
using System.Linq;
|
2014-07-04 08:09:48 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using FluentValidation.Results;
|
|
|
|
|
using NLog;
|
2013-09-18 07:33:31 +00:00
|
|
|
|
using RestSharp;
|
|
|
|
|
using NzbDrone.Core.Rest;
|
2016-01-28 17:10:55 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
2016-12-09 02:06:56 +00:00
|
|
|
|
using RestSharp.Authenticators;
|
2013-09-18 07:33:31 +00:00
|
|
|
|
|
|
|
|
|
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 error = false;
|
2014-01-09 17:04:43 +00:00
|
|
|
|
|
2015-06-28 19:45:02 +00:00
|
|
|
|
if (settings.ChannelTags.Any())
|
2014-01-09 17:04:43 +00:00
|
|
|
|
{
|
2015-06-28 19:45:02 +00:00
|
|
|
|
foreach (var channelTag in settings.ChannelTags)
|
2015-06-28 08:50:19 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
|
_logger.Error(ex, "Unable to send test message to: " + channelTag);
|
2015-06-28 08:50:19 +00:00
|
|
|
|
error = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-09 17:04:43 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2015-06-28 19:45:02 +00:00
|
|
|
|
if (settings.DeviceIds.Any())
|
2015-06-28 08:50:19 +00:00
|
|
|
|
{
|
2015-06-28 19:45:02 +00:00
|
|
|
|
foreach (var deviceId in settings.DeviceIds)
|
2015-06-28 08:50:19 +00:00
|
|
|
|
{
|
|
|
|
|
var request = BuildDeviceRequest(deviceId);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SendNotification(title, message, request, settings);
|
|
|
|
|
}
|
|
|
|
|
catch (PushBulletException ex)
|
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
|
_logger.Error(ex, "Unable to send test message to: " + deviceId);
|
2015-06-28 08:50:19 +00:00
|
|
|
|
error = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var request = BuildDeviceRequest(null);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SendNotification(title, message, request, settings);
|
|
|
|
|
}
|
|
|
|
|
catch (PushBulletException ex)
|
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
|
_logger.Error(ex, "Unable to send test message to all devices");
|
2015-06-28 08:50:19 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2017-01-12 02:59:13 +00:00
|
|
|
|
const string title = "Radarr - Test Notification";
|
2017-01-10 10:21:09 +00:00
|
|
|
|
const string body = "This is a test message from Radarr";
|
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)
|
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
|
_logger.Error(ex, "API Key is invalid: " + ex.Message);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
return new ValidationFailure("ApiKey", "API Key is invalid");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-11 21:13:42 +00:00
|
|
|
|
_logger.Error(ex, "Unable to send test message: " + ex.Message);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
return new ValidationFailure("ApiKey", "Unable to send test message");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
|
_logger.Error(ex, "Unable to send test message: " + ex.Message);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
if (long.TryParse(deviceId, out integerId))
|
2015-06-28 08:50:19 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
2016-01-28 17:10:55 +00:00
|
|
|
|
if (settings.SenderId.IsNotNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
request.AddParameter("source_device_iden", settings.SenderId);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, string.Empty);
|
2015-06-28 08:50:19 +00:00
|
|
|
|
client.ExecuteAndValidate(request);
|
|
|
|
|
}
|
|
|
|
|
catch (RestException ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
|
_logger.Error(ex, "API Key is invalid: " + ex.Message);
|
2015-06-28 08:50:19 +00:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new PushBulletException("Unable to send text message: {0}", ex, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-18 07:33:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|