@ -1,7 +1,10 @@
using System ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Net ;
using System.Net ;
using FluentValidation.Results ;
using FluentValidation.Results ;
using NLog ;
using NLog ;
using NzbDrone.Common.Extensions ;
using RestSharp ;
using RestSharp ;
using NzbDrone.Core.Rest ;
using NzbDrone.Core.Rest ;
@ -9,49 +12,82 @@ namespace NzbDrone.Core.Notifications.PushBullet
{
{
public interface IPushBulletProxy
public interface IPushBulletProxy
{
{
void SendNotification ( string title , string message , string apiKey , string deviceId ) ;
void SendNotification ( string title , string message , PushBulletSettings settings ) ;
ValidationFailure Test ( PushBulletSettings settings ) ;
ValidationFailure Test ( PushBulletSettings settings ) ;
}
}
public class PushBulletProxy : IPushBulletProxy
public class PushBulletProxy : IPushBulletProxy
{
{
private readonly Logger _logger ;
private readonly Logger _logger ;
private const string URL = "https://api.pushbullet.com/ api /pushes";
private const string URL = "https://api.pushbullet.com/ v2 /pushes";
public PushBulletProxy ( Logger logger )
public PushBulletProxy ( Logger logger )
{
{
_logger = logger ;
_logger = logger ;
}
}
public void SendNotification ( string title , string message , string apiKey , string deviceId )
public void SendNotification ( string title , string message , PushBulletSettings settings )
{
{
var client = RestClientFactory . BuildClient ( URL ) ;
var channelTags = GetIds ( settings . ChannelTags ) ;
var request = BuildRequest ( deviceId ) ;
var deviceIds = GetIds ( settings . DeviceIds ) ;
var error = false ;
request . AddParameter ( "type" , "note" ) ;
request . AddParameter ( "title" , title ) ;
request . AddParameter ( "body" , message ) ;
client . Authenticator = new HttpBasicAuthenticator ( apiKey , String . Empty ) ;
client . ExecuteAndValidate ( request ) ;
}
public RestRequest BuildRequest ( string deviceId )
{
var request = new RestRequest ( Method . POST ) ;
long integerId ;
if ( Int64. TryParse ( deviceId , out integerId ) )
if ( channelTags . Any ( ) )
{
{
request . AddParameter ( "device_id" , integerId ) ;
foreach ( var channelTag in channelTags )
}
{
var request = BuildChannelRequest ( channelTag ) ;
try
{
SendNotification ( title , message , request , settings ) ;
}
catch ( PushBulletException ex )
{
_logger . ErrorException ( "Unable to send test message to: " + channelTag , ex ) ;
error = true ;
}
}
}
else
else
{
{
request . AddParameter ( "device_iden" , deviceId ) ;
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 ;
}
}
}
}
return request ;
if ( error )
{
throw new PushBulletException ( "Unable to send PushBullet notifications to all channels or devices" ) ;
}
}
}
public ValidationFailure Test ( PushBulletSettings settings )
public ValidationFailure Test ( PushBulletSettings settings )
@ -61,7 +97,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
const string title = "Sonarr - Test Notification" ;
const string title = "Sonarr - Test Notification" ;
const string body = "This is a test message from Sonarr" ;
const string body = "This is a test message from Sonarr" ;
SendNotification ( title , body , settings .ApiKey , settings . DeviceId );
SendNotification ( title , body , settings );
}
}
catch ( RestException ex )
catch ( RestException ex )
{
{
@ -82,5 +118,63 @@ namespace NzbDrone.Core.Notifications.PushBullet
return null ;
return null ;
}
}
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 ( ) ;
}
}
}
}
}