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