mirror of https://github.com/Radarr/Radarr
Added NMA support, also added generic HttpStatusCode handling
This commit is contained in:
parent
de012aec21
commit
eb7dda0629
|
@ -4,6 +4,7 @@ using NLog;
|
|||
using Nancy;
|
||||
using NzbDrone.Api.Extensions;
|
||||
using NzbDrone.Core;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
using HttpStatusCode = Nancy.HttpStatusCode;
|
||||
|
||||
namespace NzbDrone.Api.ErrorManagement
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using NzbDrone.Common.Exceptions;
|
||||
|
||||
namespace NzbDrone.Core.Exceptions
|
||||
{
|
||||
public class BadRequestException : DownstreamException
|
||||
{
|
||||
public BadRequestException(HttpStatusCode statusCode, string message) : base(statusCode, message)
|
||||
{
|
||||
}
|
||||
|
||||
public BadRequestException(HttpStatusCode statusCode, string message, params object[] args) : base(statusCode, message, args)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
using System.Net;
|
||||
using NzbDrone.Common.Exceptions;
|
||||
|
||||
namespace NzbDrone.Core.Exceptions
|
||||
{
|
||||
public class DownstreamException : NzbDroneException
|
||||
{
|
||||
public HttpStatusCode StatusCode { get; private set; }
|
||||
|
||||
public DownstreamException(HttpStatusCode statusCode, string message, params object[] args) : base(message, args)
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
}
|
||||
|
||||
public DownstreamException(HttpStatusCode statusCode, string message)
|
||||
: base(message)
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
using System.Net;
|
||||
using NzbDrone.Common.Exceptions;
|
||||
|
||||
namespace NzbDrone.Core
|
||||
namespace NzbDrone.Core.Exceptions
|
||||
{
|
||||
public class NzbDroneClientException : NzbDroneException
|
||||
{
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace NzbDrone.Core.Exceptions
|
||||
{
|
||||
public static class StatusCodeToExceptions
|
||||
{
|
||||
public static void VerifyStatusCode(this HttpStatusCode statusCode, string message = null)
|
||||
{
|
||||
if (String.IsNullOrEmpty(message))
|
||||
{
|
||||
message = statusCode.ToString();
|
||||
}
|
||||
|
||||
switch (statusCode)
|
||||
{
|
||||
case HttpStatusCode.BadRequest:
|
||||
throw new BadRequestException(statusCode, message);
|
||||
|
||||
case HttpStatusCode.Unauthorized:
|
||||
throw new UnauthorizedAccessException(message);
|
||||
|
||||
case HttpStatusCode.PaymentRequired:
|
||||
throw new DownstreamException(statusCode, message);
|
||||
|
||||
case HttpStatusCode.InternalServerError:
|
||||
throw new DownstreamException(statusCode, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using System.Net;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
|
||||
namespace NzbDrone.Core.MetadataSource.Trakt
|
||||
{
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.NotifyMyAndroid
|
||||
{
|
||||
public class NotifyMyAndroid : NotificationBase<NotifyMyAndroidSettings>
|
||||
{
|
||||
private readonly INotifyMyAndroidProxy _notifyMyAndroidProxy;
|
||||
|
||||
public NotifyMyAndroid(INotifyMyAndroidProxy notifyMyAndroidProxy)
|
||||
{
|
||||
_notifyMyAndroidProxy = notifyMyAndroidProxy;
|
||||
}
|
||||
|
||||
public override string Name
|
||||
{
|
||||
get { return "NotifyMyAndroid"; }
|
||||
}
|
||||
|
||||
public override string ImplementationName
|
||||
{
|
||||
get { return "NotifyMyAndroid"; }
|
||||
}
|
||||
|
||||
public override string Link
|
||||
{
|
||||
get { return "http://www.notifymyandroid.com/"; }
|
||||
}
|
||||
|
||||
public override void OnGrab(string message)
|
||||
{
|
||||
const string title = "Episode Grabbed";
|
||||
|
||||
_notifyMyAndroidProxy.SendNotification(title, message, Settings.ApiKey, (NotifyMyAndroidPriority)Settings.Priority);
|
||||
}
|
||||
|
||||
public override void OnDownload(string message, Series series)
|
||||
{
|
||||
const string title = "Episode Downloaded";
|
||||
|
||||
_notifyMyAndroidProxy.SendNotification(title, message, Settings.ApiKey, (NotifyMyAndroidPriority)Settings.Priority);
|
||||
}
|
||||
|
||||
public override void AfterRename(Series series)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
namespace NzbDrone.Core.Notifications.NotifyMyAndroid
|
||||
{
|
||||
public enum NotifyMyAndroidPriority
|
||||
{
|
||||
VeryLow = -2,
|
||||
Moderate = -1,
|
||||
Normal = 0,
|
||||
High = 1,
|
||||
Emergency = 2
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Xml.Linq;
|
||||
using NLog.LayoutRenderers;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
using NzbDrone.Core.Messaging;
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
using RestSharp;
|
||||
using NzbDrone.Core.Rest;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.NotifyMyAndroid
|
||||
{
|
||||
public interface INotifyMyAndroidProxy
|
||||
{
|
||||
void SendNotification(string title, string message, string apiKye, NotifyMyAndroidPriority priority);
|
||||
}
|
||||
|
||||
public class NotifyMyAndroidProxy : INotifyMyAndroidProxy, IExecute<TestNotifyMyAndroidCommand>
|
||||
{
|
||||
private const string URL = "https://www.notifymyandroid.com/publicapi";
|
||||
|
||||
public void SendNotification(string title, string message, string apiKey, NotifyMyAndroidPriority priority)
|
||||
{
|
||||
var client = new RestClient(URL);
|
||||
var request = new RestRequest("notify", Method.POST);
|
||||
request.RequestFormat = DataFormat.Xml;
|
||||
request.AddParameter("apikey", apiKey);
|
||||
request.AddParameter("application", "NzbDrone");
|
||||
request.AddParameter("event", title);
|
||||
request.AddParameter("description", message);
|
||||
request.AddParameter("priority", (int)priority);
|
||||
|
||||
var response = client.ExecuteAndValidate(request);
|
||||
ValidateResponse(response);
|
||||
}
|
||||
|
||||
private void Verify(string apiKey)
|
||||
{
|
||||
var client = new RestClient(URL);
|
||||
var request = new RestRequest("verify", Method.GET);
|
||||
request.RequestFormat = DataFormat.Xml;
|
||||
request.AddParameter("apikey", apiKey, ParameterType.GetOrPost);
|
||||
|
||||
var response = client.ExecuteAndValidate(request);
|
||||
ValidateResponse(response);
|
||||
}
|
||||
|
||||
private void ValidateResponse(IRestResponse response)
|
||||
{
|
||||
var xDoc = XDocument.Parse(response.Content);
|
||||
var nma = xDoc.Descendants("nma").Single();
|
||||
var error = nma.Descendants("error").SingleOrDefault();
|
||||
|
||||
if (error != null)
|
||||
{
|
||||
((HttpStatusCode)Convert.ToInt32(error.Attribute("code").Value)).VerifyStatusCode(error.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public void Execute(TestNotifyMyAndroidCommand message)
|
||||
{
|
||||
const string title = "Test Notification";
|
||||
const string body = "This is a test message from NzbDrone";
|
||||
Verify(message.ApiKey);
|
||||
SendNotification(title, body, message.ApiKey, (NotifyMyAndroidPriority)message.Priority);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using NzbDrone.Core.Annotations;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.NotifyMyAndroid
|
||||
{
|
||||
public class NotifyMyAndroidSettings : INotifcationSettings
|
||||
{
|
||||
[FieldDefinition(0, Label = "API Key", HelpLink = "http://www.notifymyandroid.com/")]
|
||||
public String ApiKey { get; set; }
|
||||
|
||||
[FieldDefinition(1, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(NotifyMyAndroidPriority))]
|
||||
public Int32 Priority { get; set; }
|
||||
|
||||
public bool IsValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return !String.IsNullOrWhiteSpace(ApiKey) && Priority != null & Priority >= -1 && Priority <= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
using NzbDrone.Core.Messaging.Commands;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.NotifyMyAndroid
|
||||
{
|
||||
public class TestNotifyMyAndroidCommand : Command
|
||||
{
|
||||
|
||||
public override bool SendUpdatesToClient
|
||||
{
|
||||
get
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public string ApiKey { get; set; }
|
||||
public int Priority { get; set; }
|
||||
}
|
||||
}
|
|
@ -210,6 +210,10 @@
|
|||
<Compile Include="Download\DownloadClientProvider.cs" />
|
||||
<Compile Include="Download\DownloadClientType.cs" />
|
||||
<Compile Include="Download\SabQueueItem.cs" />
|
||||
<Compile Include="Exceptions\BadRequestException.cs" />
|
||||
<Compile Include="Exceptions\DownstreamException.cs" />
|
||||
<Compile Include="Exceptions\NzbDroneClientException.cs" />
|
||||
<Compile Include="Exceptions\StatusCodeToExceptions.cs" />
|
||||
<Compile Include="IndexerSearch\SeriesSearchService.cs" />
|
||||
<Compile Include="IndexerSearch\SeriesSearchCommand.cs" />
|
||||
<Compile Include="IndexerSearch\EpisodeSearchService.cs" />
|
||||
|
@ -245,7 +249,11 @@
|
|||
<Compile Include="Messaging\Events\IEventAggregator.cs" />
|
||||
<Compile Include="Messaging\Events\IHandle.cs" />
|
||||
<Compile Include="MetadataSource\Trakt\TraktException.cs" />
|
||||
<Compile Include="NzbDroneClientException.cs" />
|
||||
<Compile Include="Notifications\NotifyMyAndroid\NotifyMyAndroid.cs" />
|
||||
<Compile Include="Notifications\NotifyMyAndroid\NotifyMyAndroidPriority.cs" />
|
||||
<Compile Include="Notifications\NotifyMyAndroid\NotifyMyAndroidProxy.cs" />
|
||||
<Compile Include="Notifications\NotifyMyAndroid\NotifyMyAndroidSettings.cs" />
|
||||
<Compile Include="Notifications\NotifyMyAndroid\TestNotifyMyAndroidCommand.cs" />
|
||||
<Compile Include="Instrumentation\LoggerExtensions.cs" />
|
||||
<Compile Include="MediaFiles\Commands\BackendCommandAttribute.cs" />
|
||||
<Compile Include="Messaging\Commands\BackendCommandAttribute.cs" />
|
||||
|
|
Loading…
Reference in New Issue