@ -3,16 +3,15 @@ using System.Linq;
using System.Net ;
using System.Net ;
using FluentValidation.Results ;
using FluentValidation.Results ;
using NLog ;
using NLog ;
using NzbDrone.Common.EnvironmentInfo ;
using NzbDrone.Common.Extensions ;
using NzbDrone.Common.Extensions ;
using NzbDrone.Common.Http ;
using NzbDrone.Common.Http ;
using NzbDrone.Common.Serializer ;
namespace NzbDrone.Core.Notifications.Apprise
namespace NzbDrone.Core.Notifications.Apprise
{
{
public interface IAppriseProxy
public interface IAppriseProxy
{
{
void SendNotification ( AppriseSettings settings , string title , string message ) ;
void SendNotification ( AppriseSettings settings , string title , string message ) ;
ValidationFailure Test ( AppriseSettings settings ) ;
ValidationFailure Test ( AppriseSettings settings ) ;
}
}
@ -27,11 +26,18 @@ namespace NzbDrone.Core.Notifications.Apprise
_logger = logger ;
_logger = logger ;
}
}
public void SendNotification ( AppriseSettings settings , string title , string body )
public void SendNotification ( AppriseSettings settings , string title , string message )
{
{
var requestBuilder = new HttpRequestBuilder ( settings . BaseUrl . TrimEnd ( '/' , ' ' ) ) . Post ( )
var payload = new ApprisePayload
. AddFormParameter ( "title" , title )
{
. AddFormParameter ( "body" , body ) ;
Title = title ,
Body = message ,
Type = ( AppriseNotificationType ) settings . NotificationType
} ;
var requestBuilder = new HttpRequestBuilder ( settings . BaseUrl . TrimEnd ( '/' , ' ' ) )
. Post ( )
. Accept ( HttpAccept . Json ) ;
if ( settings . ConfigurationKey . IsNotNullOrWhiteSpace ( ) )
if ( settings . ConfigurationKey . IsNotNullOrWhiteSpace ( ) )
{
{
@ -41,14 +47,14 @@ namespace NzbDrone.Core.Notifications.Apprise
}
}
else if ( settings . StatelessUrls . IsNotNullOrWhiteSpace ( ) )
else if ( settings . StatelessUrls . IsNotNullOrWhiteSpace ( ) )
{
{
requestBuilder
requestBuilder . Resource ( "/notify" ) ;
. Resource ( "/notify" )
. AddFormParameter ( "urls" , settings . StatelessUrls ) ;
payload . Urls = settings . StatelessUrls ;
}
}
if ( settings . Tags . Any ( ) )
if ( settings . Tags . Any ( ) )
{
{
requestBuilder. AddFormParameter ( "tag" , settings . Tags . Join ( "," ) ) ;
payload. Tag = settings . Tags . Join ( "," ) ;
}
}
if ( settings . AuthUsername . IsNotNullOrWhiteSpace ( ) | | settings . AuthPassword . IsNotNullOrWhiteSpace ( ) )
if ( settings . AuthUsername . IsNotNullOrWhiteSpace ( ) | | settings . AuthPassword . IsNotNullOrWhiteSpace ( ) )
@ -56,7 +62,20 @@ namespace NzbDrone.Core.Notifications.Apprise
requestBuilder . NetworkCredential = new BasicNetworkCredential ( settings . AuthUsername , settings . AuthPassword ) ;
requestBuilder . NetworkCredential = new BasicNetworkCredential ( settings . AuthUsername , settings . AuthPassword ) ;
}
}
_httpClient . Execute ( requestBuilder . Build ( ) ) ;
var request = requestBuilder . Build ( ) ;
request . Headers . ContentType = "application/json" ;
request . SetContent ( payload . ToJson ( ) ) ;
try
{
_httpClient . Execute ( request ) ;
}
catch ( HttpException ex )
{
_logger . Error ( ex , "Unable to send message" ) ;
throw new AppriseException ( "Unable to send Apprise notifications: {0}" , ex , ex . Message ) ;
}
}
}
public ValidationFailure Test ( AppriseSettings settings )
public ValidationFailure Test ( AppriseSettings settings )
@ -68,21 +87,29 @@ namespace NzbDrone.Core.Notifications.Apprise
{
{
SendNotification ( settings , title , body ) ;
SendNotification ( settings , title , body ) ;
}
}
catch ( HttpException ex )
catch ( AppriseException ex ) when ( ex . InnerException is HttpException httpException )
{
{
if ( ex . Response . StatusCode = = HttpStatusCode . Unauthorized )
if ( httpException . Response . StatusCode = = HttpStatusCode . Unauthorized )
{
{
_logger . Error ( ex , $"HTTP Auth credentials are invalid: { ex.Message}" ) ;
_logger . Error ( ex , $"HTTP Auth credentials are invalid: { 0}", ex . Message) ;
return new ValidationFailure ( "AuthUsername" , $"HTTP Auth credentials are invalid: {ex.Message}" ) ;
return new ValidationFailure ( "AuthUsername" , $"HTTP Auth credentials are invalid: {ex.Message}" ) ;
}
}
_logger . Error ( ex , "Unable to send test message. Server connection failed. Status code: {0}" , ex . Message ) ;
if ( httpException . Response . Content . IsNotNullOrWhiteSpace ( ) )
return new ValidationFailure ( "Url" , $"Unable to connect to Apprise API. Please try again later. Status code: {ex.Message}" ) ;
{
var error = Json . Deserialize < AppriseError > ( httpException . Response . Content ) ;
_logger . Error ( ex , $"Unable to send test message. Response from API: {0}" , error . Error ) ;
return new ValidationFailure ( string . Empty , $"Unable to send test message. Response from API: {error.Error}" ) ;
}
_logger . Error ( ex , "Unable to send test message. Server connection failed: ({0}) {1}" , httpException . Response . StatusCode , ex . Message ) ;
return new ValidationFailure ( "Url" , $"Unable to connect to Apprise API. Server connection failed: ({httpException.Response.StatusCode}) {ex.Message}" ) ;
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{
_logger . Error ( ex , "Unable to send test message. Status code: {0}" , ex . Message ) ;
_logger . Error ( ex , "Unable to send test message : {0}", ex . Message ) ;
return new ValidationFailure ( "Url" , $"Unable to send test message. Status code: {ex.Message}" ) ;
return new ValidationFailure ( "Url" , $"Unable to send test message : {ex.Message}") ;
}
}
return null ;
return null ;