1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-24 16:51:58 +00:00

Merge pull request #698 from srod/boxcar2

Added Boxcar 2
This commit is contained in:
Mark McDowall 2015-07-31 11:45:06 -07:00
commit 8c31af608b
6 changed files with 213 additions and 0 deletions

View file

@ -0,0 +1,65 @@
using System.Collections.Generic;
using FluentValidation.Results;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Notifications.Boxcar
{
public class Boxcar : NotificationBase<BoxcarSettings>
{
private readonly IBoxcarProxy _proxy;
public Boxcar(IBoxcarProxy proxy)
{
_proxy = proxy;
}
public override string Link
{
get { return "https://boxcar.io/client"; }
}
public override void OnGrab(string message)
{
const string title = "Episode Grabbed";
_proxy.SendNotification(title, message, Settings);
}
public override void OnDownload(DownloadMessage message)
{
const string title = "Episode Downloaded";
_proxy.SendNotification(title, message.Message, Settings);
}
public override void OnRename(Series series)
{
}
public override string Name
{
get
{
return "Boxcar";
}
}
public override bool SupportsOnRename
{
get
{
return false;
}
}
public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(_proxy.Test(Settings));
return new ValidationResult(failures);
}
}
}

View file

@ -0,0 +1,16 @@
using System;
using NzbDrone.Common.Exceptions;
namespace NzbDrone.Core.Notifications.Boxcar
{
public class BoxcarException : NzbDroneException
{
public BoxcarException(string message) : base(message)
{
}
public BoxcarException(string message, Exception innerException, params object[] args) : base(message, innerException, args)
{
}
}
}

View file

@ -0,0 +1,95 @@
using System;
using System.Linq;
using System.Net;
using FluentValidation.Results;
using NLog;
using RestSharp;
using NzbDrone.Core.Rest;
namespace NzbDrone.Core.Notifications.Boxcar
{
public interface IBoxcarProxy
{
void SendNotification(string title, string message, BoxcarSettings settings);
ValidationFailure Test(BoxcarSettings settings);
}
public class BoxcarProxy : IBoxcarProxy
{
private readonly Logger _logger;
private const string URL = "https://new.boxcar.io/api/notifications";
public BoxcarProxy(Logger logger)
{
_logger = logger;
}
public void SendNotification(string title, string message, BoxcarSettings settings)
{
var request = new RestRequest(Method.POST);
try
{
SendNotification(title, message, request, settings);
}
catch (BoxcarException ex)
{
_logger.ErrorException("Unable to send message", ex);
throw new BoxcarException("Unable to send Boxcar notifications");
}
}
public ValidationFailure Test(BoxcarSettings settings)
{
try
{
const string title = "Test Notification";
const string body = "This is a test message from Sonarr";
SendNotification(title, body, settings);
return null;
}
catch (RestException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.ErrorException("Access Token is invalid: " + ex.Message, ex);
return new ValidationFailure("Token", "Access Token is invalid");
}
_logger.ErrorException("Unable to send test message: " + ex.Message, ex);
return new ValidationFailure("Token", "Unable to send test message");
}
catch (Exception ex)
{
_logger.ErrorException("Unable to send test message: " + ex.Message, ex);
return new ValidationFailure("", "Unable to send test message");
}
}
private void SendNotification(string title, string message, RestRequest request, BoxcarSettings settings)
{
try
{
var client = RestClientFactory.BuildClient(URL);
request.AddParameter("user_credentials", settings.Token);
request.AddParameter("notification[title]", title);
request.AddParameter("notification[long_message]", message);
request.AddParameter("notification[source_name]", "Sonarr");
client.ExecuteAndValidate(request);
}
catch (RestException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.ErrorException("Access Token is invalid: " + ex.Message, ex);
throw;
}
throw new BoxcarException("Unable to send text message: " + ex.Message, ex);
}
}
}
}

View file

@ -0,0 +1,29 @@
using System.Collections.Generic;
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.Boxcar
{
public class BoxcarSettingsValidator : AbstractValidator<BoxcarSettings>
{
public BoxcarSettingsValidator()
{
RuleFor(c => c.Token).NotEmpty();
}
}
public class BoxcarSettings : IProviderConfig
{
private static readonly BoxcarSettingsValidator Validator = new BoxcarSettingsValidator();
[FieldDefinition(0, Label = "Access Token", HelpText = "Your Access Token, from your Boxcar account settings: https://new.boxcar.io/account/edit", HelpLink = "https://new.boxcar.io/account/edit")]
public string Token { get; set; }
public NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

View file

@ -719,6 +719,10 @@
<Compile Include="Metadata\MetadataType.cs" />
<Compile Include="MetadataSource\IProvideSeriesInfo.cs" />
<Compile Include="MetadataSource\ISearchForNewSeries.cs" />
<Compile Include="Notifications\Boxcar\Boxcar.cs" />
<Compile Include="Notifications\Boxcar\BoxcarException.cs" />
<Compile Include="Notifications\Boxcar\BoxcarProxy.cs" />
<Compile Include="Notifications\Boxcar\BoxcarSettings.cs" />
<Compile Include="Notifications\Plex\Models\PlexIdentity.cs" />
<Compile Include="Notifications\Plex\Models\PlexPreferences.cs" />
<Compile Include="Notifications\Plex\Models\PlexSectionItem.cs" />

View file

@ -43,6 +43,10 @@ public static IRestResponse ValidateResponse(this IRestResponse response, IRestC
{
return response;
}
case HttpStatusCode.Created:
{
return response;
}
default:
{
Logger.Warn("[{0}] [{1}] Failed. [{2}]", response.Request.Method, response.ResponseUri.ToString(), response.StatusCode);