mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-15 09:42:56 -07:00
Add webhook notification and API logic
This commit is contained in:
parent
faba87cdc6
commit
cdc002ecd7
8 changed files with 216 additions and 2 deletions
10
src/Ombi.Api.Webhook/IWebhookApi.cs
Normal file
10
src/Ombi.Api.Webhook/IWebhookApi.cs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Ombi.Api.Webhook
|
||||||
|
{
|
||||||
|
public interface IWebhookApi
|
||||||
|
{
|
||||||
|
Task PushAsync(string endpoint, string accessToken, IReadOnlyDictionary<string, string> parameters);
|
||||||
|
}
|
||||||
|
}
|
15
src/Ombi.Api.Webhook/Ombi.Api.Webhook.csproj
Normal file
15
src/Ombi.Api.Webhook/Ombi.Api.Webhook.csproj
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
<AssemblyVersion>3.0.0.0</AssemblyVersion>
|
||||||
|
<FileVersion>3.0.0.0</FileVersion>
|
||||||
|
<Version></Version>
|
||||||
|
<PackageVersion></PackageVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Ombi.Api\Ombi.Api.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
36
src/Ombi.Api.Webhook/WebhookApi.cs
Normal file
36
src/Ombi.Api.Webhook/WebhookApi.cs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Ombi.Api.Webhook
|
||||||
|
{
|
||||||
|
public class WebhookApi : IWebhookApi
|
||||||
|
{
|
||||||
|
private static readonly CamelCasePropertyNamesContractResolver _nameResolver = new CamelCasePropertyNamesContractResolver();
|
||||||
|
|
||||||
|
public WebhookApi(IApi api)
|
||||||
|
{
|
||||||
|
_api = api;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly IApi _api;
|
||||||
|
|
||||||
|
public async Task PushAsync(string baseUrl, string accessToken, IReadOnlyDictionary<string, string> parameters)
|
||||||
|
{
|
||||||
|
var request = new Request("/", baseUrl, HttpMethod.Post);
|
||||||
|
request.AddHeader("Access-Token", accessToken);
|
||||||
|
request.ApplicationJsonContentType();
|
||||||
|
|
||||||
|
var body = parameters.ToDictionary(
|
||||||
|
x => _nameResolver.GetResolvedPropertyName(x.Key),
|
||||||
|
x => x.Value
|
||||||
|
);
|
||||||
|
|
||||||
|
request.AddJsonBody(body);
|
||||||
|
|
||||||
|
await _api.Request(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
src/Ombi.Core/Models/UI/WebhookNotificationViewModel.cs
Normal file
15
src/Ombi.Core/Models/UI/WebhookNotificationViewModel.cs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Ombi.Settings.Settings.Models.Notifications;
|
||||||
|
using Ombi.Store.Entities;
|
||||||
|
|
||||||
|
namespace Ombi.Core.Models.UI
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The view model for the notification settings page
|
||||||
|
/// </summary>
|
||||||
|
/// <seealso cref="WebhookSettings" />
|
||||||
|
public class WebhookNotificationViewModel : WebhookSettings
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace Ombi.Notifications.Agents
|
||||||
|
{
|
||||||
|
public interface IWebhookNotification : INotification
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
116
src/Ombi.Notifications/Agents/WebhookNotification.cs
Normal file
116
src/Ombi.Notifications/Agents/WebhookNotification.cs
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Ombi.Api.Webhook;
|
||||||
|
using Ombi.Core.Settings;
|
||||||
|
using Ombi.Helpers;
|
||||||
|
using Ombi.Notifications.Models;
|
||||||
|
using Ombi.Settings.Settings.Models;
|
||||||
|
using Ombi.Settings.Settings.Models.Notifications;
|
||||||
|
using Ombi.Store.Entities;
|
||||||
|
using Ombi.Store.Repository;
|
||||||
|
using Ombi.Store.Repository.Requests;
|
||||||
|
|
||||||
|
namespace Ombi.Notifications.Agents
|
||||||
|
{
|
||||||
|
public class WebhookNotification : BaseNotification<WebhookSettings>, IWebhookNotification
|
||||||
|
{
|
||||||
|
public WebhookNotification(IWebhookApi api, ISettingsService<WebhookSettings> sn, ILogger<WebhookNotification> log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t,
|
||||||
|
ISettingsService<CustomizationSettings> s, IRepository<RequestSubscription> sub, IMusicRequestRepository music,
|
||||||
|
IRepository<UserNotificationPreferences> userPref) : base(sn, r, m, t, s, log, sub, music, userPref)
|
||||||
|
{
|
||||||
|
Api = api;
|
||||||
|
Logger = log;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string NotificationName => "WebhookNotification";
|
||||||
|
|
||||||
|
private IWebhookApi Api { get; }
|
||||||
|
private ILogger<WebhookNotification> Logger { get; }
|
||||||
|
|
||||||
|
protected override bool ValidateConfiguration(WebhookSettings settings)
|
||||||
|
{
|
||||||
|
return settings.Enabled && !string.IsNullOrEmpty(settings.WebhookUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task NewRequest(NotificationOptions model, WebhookSettings settings)
|
||||||
|
{
|
||||||
|
await Run(model, settings, NotificationType.NewRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected override async Task NewIssue(NotificationOptions model, WebhookSettings settings)
|
||||||
|
{
|
||||||
|
await Run(model, settings, NotificationType.Issue);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task IssueComment(NotificationOptions model, WebhookSettings settings)
|
||||||
|
{
|
||||||
|
await Run(model, settings, NotificationType.IssueComment);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task IssueResolved(NotificationOptions model, WebhookSettings settings)
|
||||||
|
{
|
||||||
|
await Run(model, settings, NotificationType.IssueResolved);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task AddedToRequestQueue(NotificationOptions model, WebhookSettings settings)
|
||||||
|
{
|
||||||
|
await Run(model, settings, NotificationType.ItemAddedToFaultQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task RequestDeclined(NotificationOptions model, WebhookSettings settings)
|
||||||
|
{
|
||||||
|
await Run(model, settings, NotificationType.RequestDeclined);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task RequestApproved(NotificationOptions model, WebhookSettings settings)
|
||||||
|
{
|
||||||
|
await Run(model, settings, NotificationType.RequestApproved);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task AvailableRequest(NotificationOptions model, WebhookSettings settings)
|
||||||
|
{
|
||||||
|
await Run(model, settings, NotificationType.RequestAvailable);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task Send(NotificationMessage model, WebhookSettings settings)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Api.PushAsync(settings.WebhookUrl, settings.ApplicationToken, model.Data);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger.LogError(LoggingEvents.WebhookNotification, e, "Failed to send webhook notification");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task Test(NotificationOptions model, WebhookSettings settings)
|
||||||
|
{
|
||||||
|
var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!";
|
||||||
|
var notification = new NotificationMessage
|
||||||
|
{
|
||||||
|
Message = message,
|
||||||
|
};
|
||||||
|
await Send(notification, settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task Run(NotificationOptions model, WebhookSettings settings, NotificationType type)
|
||||||
|
{
|
||||||
|
var parsed = await LoadTemplate(NotificationAgent.Webhook, type, model);
|
||||||
|
if (parsed.Disabled)
|
||||||
|
{
|
||||||
|
Logger.LogInformation($"Template {type} is disabled for {NotificationAgent.Webhook}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var notification = new NotificationMessage
|
||||||
|
{
|
||||||
|
Data = parsed.Data,
|
||||||
|
};
|
||||||
|
|
||||||
|
await Send(notification, settings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
namespace Ombi.Settings.Settings.Models.Notifications
|
||||||
|
{
|
||||||
|
public class WebhookSettings : Settings
|
||||||
|
{
|
||||||
|
public bool Enabled { get; set; }
|
||||||
|
public string WebhookUrl { get; set; }
|
||||||
|
public string ApplicationToken { get; set; }
|
||||||
|
}
|
||||||
|
}
|
11
src/Ombi.sln
11
src/Ombi.sln
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio 15
|
# Visual Studio Version 16
|
||||||
VisualStudioVersion = 15.0.27130.2027
|
VisualStudioVersion = 16.0.29519.87
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ombi", "Ombi\Ombi.csproj", "{C987AA67-AFE1-468F-ACD3-EAD5A48E1F6A}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ombi", "Ombi\Ombi.csproj", "{C987AA67-AFE1-468F-ACD3-EAD5A48E1F6A}"
|
||||||
EndProject
|
EndProject
|
||||||
|
@ -100,6 +100,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ombi.Helpers.Tests", "Ombi.
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ombi.Api.Gotify", "Ombi.Api.Gotify\Ombi.Api.Gotify.csproj", "{105EA346-766E-45B8-928B-DE6991DCB7EB}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ombi.Api.Gotify", "Ombi.Api.Gotify\Ombi.Api.Gotify.csproj", "{105EA346-766E-45B8-928B-DE6991DCB7EB}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ombi.Api.Webhook", "Ombi.Api.Webhook\Ombi.Api.Webhook.csproj", "{E2186FDA-D827-4781-8663-130AC382F12C}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -262,6 +264,10 @@ Global
|
||||||
{105EA346-766E-45B8-928B-DE6991DCB7EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{105EA346-766E-45B8-928B-DE6991DCB7EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{105EA346-766E-45B8-928B-DE6991DCB7EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{105EA346-766E-45B8-928B-DE6991DCB7EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{105EA346-766E-45B8-928B-DE6991DCB7EB}.Release|Any CPU.Build.0 = Release|Any CPU
|
{105EA346-766E-45B8-928B-DE6991DCB7EB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{E2186FDA-D827-4781-8663-130AC382F12C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{E2186FDA-D827-4781-8663-130AC382F12C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{E2186FDA-D827-4781-8663-130AC382F12C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{E2186FDA-D827-4781-8663-130AC382F12C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -300,6 +306,7 @@ Global
|
||||||
{4FA21A20-92F4-462C-B929-2C517A88CC56} = {9293CA11-360A-4C20-A674-B9E794431BF5}
|
{4FA21A20-92F4-462C-B929-2C517A88CC56} = {9293CA11-360A-4C20-A674-B9E794431BF5}
|
||||||
{CC8CEFCD-0CB6-45BB-845F-508BCAB5BDC3} = {6F42AB98-9196-44C4-B888-D5E409F415A1}
|
{CC8CEFCD-0CB6-45BB-845F-508BCAB5BDC3} = {6F42AB98-9196-44C4-B888-D5E409F415A1}
|
||||||
{105EA346-766E-45B8-928B-DE6991DCB7EB} = {9293CA11-360A-4C20-A674-B9E794431BF5}
|
{105EA346-766E-45B8-928B-DE6991DCB7EB} = {9293CA11-360A-4C20-A674-B9E794431BF5}
|
||||||
|
{E2186FDA-D827-4781-8663-130AC382F12C} = {9293CA11-360A-4C20-A674-B9E794431BF5}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {192E9BF8-00B4-45E4-BCCC-4C215725C869}
|
SolutionGuid = {192E9BF8-00B4-45E4-BCCC-4C215725C869}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue