parent
d70d351ea2
commit
adcd00d9fd
@ -1,118 +1,70 @@
|
|||||||
using System;
|
using System.Net.Http;
|
||||||
using System.Collections.Specialized;
|
|
||||||
using FluentValidation.Results;
|
|
||||||
using NLog;
|
using NLog;
|
||||||
using NzbDrone.Common.Http;
|
using NzbDrone.Common.Http;
|
||||||
|
using NzbDrone.Common.Serializer;
|
||||||
|
using NzbDrone.Core.Notifications.Webhook;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Notifications.Notifiarr
|
namespace NzbDrone.Core.Notifications.Notifiarr
|
||||||
{
|
{
|
||||||
public interface INotifiarrProxy
|
public interface INotifiarrProxy
|
||||||
{
|
{
|
||||||
void SendNotification(StringDictionary message, NotifiarrSettings settings);
|
void SendNotification(WebhookPayload payload, NotifiarrSettings settings);
|
||||||
ValidationFailure Test(NotifiarrSettings settings);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NotifiarrProxy : INotifiarrProxy
|
public class NotifiarrProxy : INotifiarrProxy
|
||||||
{
|
{
|
||||||
|
private const string URL = "https://notifiarr.com";
|
||||||
private readonly IHttpClient _httpClient;
|
private readonly IHttpClient _httpClient;
|
||||||
private readonly Logger _logger;
|
|
||||||
|
|
||||||
public NotifiarrProxy(IHttpClient httpClient, Logger logger)
|
public NotifiarrProxy(IHttpClient httpClient)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
_httpClient = httpClient;
|
||||||
_logger = logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendNotification(StringDictionary message, NotifiarrSettings settings)
|
public void SendNotification(WebhookPayload payload, NotifiarrSettings settings)
|
||||||
{
|
{
|
||||||
try
|
ProcessNotification(payload, settings);
|
||||||
{
|
|
||||||
ProcessNotification(message, settings);
|
|
||||||
}
|
|
||||||
catch (NotifiarrException ex)
|
|
||||||
{
|
|
||||||
_logger.Error(ex, "Unable to send notification");
|
|
||||||
throw new NotifiarrException("Unable to send notification");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ValidationFailure Test(NotifiarrSettings settings)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var variables = new StringDictionary();
|
|
||||||
variables.Add("Radarr_EventType", "Test");
|
|
||||||
|
|
||||||
SendNotification(variables, settings);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
catch (HttpException ex)
|
|
||||||
{
|
|
||||||
switch ((int)ex.Response.StatusCode)
|
|
||||||
{
|
|
||||||
case 401:
|
|
||||||
_logger.Error(ex, "API key is invalid: " + ex.Message);
|
|
||||||
return new ValidationFailure("APIKey", "API key is invalid");
|
|
||||||
case 400:
|
|
||||||
_logger.Error(ex, "Unable to send test message. Ensure Radarr Integration is enabled & assigned a channel on Notifiarr");
|
|
||||||
return new ValidationFailure("", "Unable to send test message. Ensure Radarr Integration is enabled & assigned a channel on Notifiarr");
|
|
||||||
case 520:
|
|
||||||
case 521:
|
|
||||||
case 522:
|
|
||||||
case 523:
|
|
||||||
case 524:
|
|
||||||
_logger.Error(ex, "Cloudflare Related HTTP Error - Unable to send test message: " + ex.Message);
|
|
||||||
return new ValidationFailure("", "Cloudflare Related HTTP Error - Unable to send test message");
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.Error(ex, "Unknown HTTP Error - Unable to send test message: " + ex.Message);
|
|
||||||
return new ValidationFailure("", "Unknown HTTP Error - Unable to send test message");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.Error(ex, "Unable to send test notification: " + ex.Message);
|
|
||||||
return new ValidationFailure("", "Unable to send test notification");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessNotification(StringDictionary message, NotifiarrSettings settings)
|
private void ProcessNotification(WebhookPayload payload, NotifiarrSettings settings)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var url = "https://notifiarr.com";
|
var request = new HttpRequestBuilder(URL + "/api/v1/notification/radarr")
|
||||||
var requestBuilder = new HttpRequestBuilder(url + "/api/v1/notification/radarr/" + settings.APIKey).Post();
|
.Accept(HttpAccept.Json)
|
||||||
requestBuilder.AddFormParameter("instanceName", settings.InstanceName).Build();
|
.SetHeader("X-API-Key", settings.APIKey)
|
||||||
|
.Build();
|
||||||
|
|
||||||
foreach (string key in message.Keys)
|
request.Method = HttpMethod.Post;
|
||||||
{
|
|
||||||
requestBuilder.AddFormParameter(key, message[key]);
|
|
||||||
}
|
|
||||||
|
|
||||||
var request = requestBuilder.Build();
|
request.Headers.ContentType = "application/json";
|
||||||
|
request.SetContent(payload.ToJson());
|
||||||
|
|
||||||
_httpClient.Post(request);
|
_httpClient.Post(request);
|
||||||
}
|
}
|
||||||
catch (HttpException ex)
|
catch (HttpException ex)
|
||||||
{
|
{
|
||||||
switch ((int)ex.Response.StatusCode)
|
var responseCode = ex.Response.StatusCode;
|
||||||
|
switch ((int)responseCode)
|
||||||
{
|
{
|
||||||
case 401:
|
case 401:
|
||||||
_logger.Error("", "API key is invalid");
|
throw new NotifiarrException("API key is invalid");
|
||||||
throw new NotifiarrException("API key is invalid", ex);
|
|
||||||
case 400:
|
case 400:
|
||||||
_logger.Error(ex, "Unable to send notification. Ensure Radarr Integration is enabled & assigned a channel on Notifiarr");
|
throw new NotifiarrException("Unable to send notification. Ensure Radarr Integration is enabled & assigned a channel on Notifiarr");
|
||||||
throw new NotifiarrException("Unable to send notification. Ensure Radarr Integration is enabled & assigned a channel on Notifiarr", ex);
|
case 502:
|
||||||
|
case 503:
|
||||||
|
case 504:
|
||||||
|
throw new NotifiarrException("Unable to send notification. Service Unavailable", ex);
|
||||||
case 520:
|
case 520:
|
||||||
case 521:
|
case 521:
|
||||||
case 522:
|
case 522:
|
||||||
case 523:
|
case 523:
|
||||||
case 524:
|
case 524:
|
||||||
_logger.Error(ex, "Cloudflare Related HTTP Error - Unable to send notification");
|
|
||||||
throw new NotifiarrException("Cloudflare Related HTTP Error - Unable to send notification", ex);
|
throw new NotifiarrException("Cloudflare Related HTTP Error - Unable to send notification", ex);
|
||||||
}
|
default:
|
||||||
|
|
||||||
throw new NotifiarrException("Unknown HTTP Error - Unable to send notification", ex);
|
throw new NotifiarrException("Unknown HTTP Error - Unable to send notification", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -0,0 +1,171 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using NzbDrone.Core.Configuration;
|
||||||
|
using NzbDrone.Core.MediaFiles;
|
||||||
|
using NzbDrone.Core.Movies;
|
||||||
|
using NzbDrone.Core.ThingiProvider;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Notifications.Webhook
|
||||||
|
{
|
||||||
|
public abstract class WebhookBase<TSettings> : NotificationBase<TSettings>
|
||||||
|
where TSettings : IProviderConfig, new()
|
||||||
|
{
|
||||||
|
private readonly IConfigFileProvider _configFileProvider;
|
||||||
|
|
||||||
|
protected WebhookBase(IConfigFileProvider configFileProvider)
|
||||||
|
: base()
|
||||||
|
{
|
||||||
|
_configFileProvider = configFileProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected WebhookGrabPayload BuildOnGrabPayload(GrabMessage message)
|
||||||
|
{
|
||||||
|
var remoteMovie = message.RemoteMovie;
|
||||||
|
var quality = message.Quality;
|
||||||
|
|
||||||
|
return new WebhookGrabPayload
|
||||||
|
{
|
||||||
|
EventType = WebhookEventType.Grab,
|
||||||
|
InstanceName = _configFileProvider.InstanceName,
|
||||||
|
Movie = new WebhookMovie(message.Movie),
|
||||||
|
RemoteMovie = new WebhookRemoteMovie(remoteMovie),
|
||||||
|
Release = new WebhookRelease(quality, remoteMovie),
|
||||||
|
DownloadClient = message.DownloadClientName,
|
||||||
|
DownloadClientType = message.DownloadClientType,
|
||||||
|
DownloadId = message.DownloadId
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected WebhookImportPayload BuildOnDownloadPayload(DownloadMessage message)
|
||||||
|
{
|
||||||
|
var movieFile = message.MovieFile;
|
||||||
|
|
||||||
|
var payload = new WebhookImportPayload
|
||||||
|
{
|
||||||
|
EventType = WebhookEventType.Download,
|
||||||
|
InstanceName = _configFileProvider.InstanceName,
|
||||||
|
Movie = new WebhookMovie(message.Movie),
|
||||||
|
RemoteMovie = new WebhookRemoteMovie(message.Movie),
|
||||||
|
MovieFile = new WebhookMovieFile(movieFile),
|
||||||
|
IsUpgrade = message.OldMovieFiles.Any(),
|
||||||
|
DownloadClient = message.DownloadClientInfo?.Name,
|
||||||
|
DownloadClientType = message.DownloadClientInfo?.Type,
|
||||||
|
DownloadId = message.DownloadId
|
||||||
|
};
|
||||||
|
|
||||||
|
if (message.OldMovieFiles.Any())
|
||||||
|
{
|
||||||
|
payload.DeletedFiles = message.OldMovieFiles.ConvertAll(x =>
|
||||||
|
new WebhookMovieFile(x)
|
||||||
|
{
|
||||||
|
Path = Path.Combine(message.Movie.Path, x.RelativePath)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected WebhookRenamePayload BuildOnMovieAdded(Movie movie)
|
||||||
|
{
|
||||||
|
return new WebhookRenamePayload
|
||||||
|
{
|
||||||
|
EventType = WebhookEventType.MovieAdded,
|
||||||
|
InstanceName = _configFileProvider.InstanceName,
|
||||||
|
Movie = new WebhookMovie(movie)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected WebhookMovieFileDeletePayload BuildOnMovieFileDelete(MovieFileDeleteMessage deleteMessage)
|
||||||
|
{
|
||||||
|
return new WebhookMovieFileDeletePayload
|
||||||
|
{
|
||||||
|
EventType = WebhookEventType.MovieFileDelete,
|
||||||
|
InstanceName = _configFileProvider.InstanceName,
|
||||||
|
Movie = new WebhookMovie(deleteMessage.Movie),
|
||||||
|
MovieFile = new WebhookMovieFile(deleteMessage.MovieFile),
|
||||||
|
DeleteReason = deleteMessage.Reason
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected WebhookMovieDeletePayload BuildOnMovieDelete(MovieDeleteMessage deleteMessage)
|
||||||
|
{
|
||||||
|
return new WebhookMovieDeletePayload
|
||||||
|
{
|
||||||
|
EventType = WebhookEventType.MovieDelete,
|
||||||
|
InstanceName = _configFileProvider.InstanceName,
|
||||||
|
Movie = new WebhookMovie(deleteMessage.Movie),
|
||||||
|
DeletedFiles = deleteMessage.DeletedFiles
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected WebhookRenamePayload BuildOnRenamePayload(Movie movie, List<RenamedMovieFile> renamedFiles)
|
||||||
|
{
|
||||||
|
return new WebhookRenamePayload
|
||||||
|
{
|
||||||
|
EventType = WebhookEventType.Rename,
|
||||||
|
InstanceName = _configFileProvider.InstanceName,
|
||||||
|
Movie = new WebhookMovie(movie),
|
||||||
|
RenamedMovieFiles = renamedFiles.ConvertAll(x => new WebhookRenamedMovieFile(x))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected WebhookHealthPayload BuildHealthPayload(HealthCheck.HealthCheck healthCheck)
|
||||||
|
{
|
||||||
|
return new WebhookHealthPayload
|
||||||
|
{
|
||||||
|
EventType = WebhookEventType.Health,
|
||||||
|
InstanceName = _configFileProvider.InstanceName,
|
||||||
|
Level = healthCheck.Type,
|
||||||
|
Message = healthCheck.Message,
|
||||||
|
Type = healthCheck.Source.Name,
|
||||||
|
WikiUrl = healthCheck.WikiUrl?.ToString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected WebhookApplicationUpdatePayload BuildApplicationUpdatePayload(ApplicationUpdateMessage updateMessage)
|
||||||
|
{
|
||||||
|
return new WebhookApplicationUpdatePayload
|
||||||
|
{
|
||||||
|
EventType = WebhookEventType.ApplicationUpdate,
|
||||||
|
InstanceName = _configFileProvider.InstanceName,
|
||||||
|
Message = updateMessage.Message,
|
||||||
|
PreviousVersion = updateMessage.PreviousVersion.ToString(),
|
||||||
|
NewVersion = updateMessage.NewVersion.ToString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected WebhookPayload BuildTestPayload()
|
||||||
|
{
|
||||||
|
return new WebhookGrabPayload
|
||||||
|
{
|
||||||
|
EventType = WebhookEventType.Test,
|
||||||
|
InstanceName = _configFileProvider.InstanceName,
|
||||||
|
Movie = new WebhookMovie
|
||||||
|
{
|
||||||
|
Id = 1,
|
||||||
|
Title = "Test Title",
|
||||||
|
Year = 1970,
|
||||||
|
FolderPath = "C:\\testpath",
|
||||||
|
ReleaseDate = "1970-01-01"
|
||||||
|
},
|
||||||
|
RemoteMovie = new WebhookRemoteMovie
|
||||||
|
{
|
||||||
|
TmdbId = 1234,
|
||||||
|
ImdbId = "5678",
|
||||||
|
Title = "Test title",
|
||||||
|
Year = 1970
|
||||||
|
},
|
||||||
|
Release = new WebhookRelease
|
||||||
|
{
|
||||||
|
Indexer = "Test Indexer",
|
||||||
|
Quality = "Test Quality",
|
||||||
|
QualityVersion = 1,
|
||||||
|
ReleaseGroup = "Test Group",
|
||||||
|
ReleaseTitle = "Test Title",
|
||||||
|
Size = 9999999
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue