diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-11-04 08:21:43 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-04 08:21:43 -0600 |
| commit | e7aefd66f1992dcecd9454df0771e1f29fae2b1d (patch) | |
| tree | 14da14b514ab3755d03e025f95903eda47c789e0 /src/slskd/Core | |
| parent | 5ebb679f4f7a24649e5117931e06138a9375110f (diff) | |
| parent | 7f9a694da61bb046343d9f98ec2b5cd8b444e56b (diff) | |
| download | slskd-e7aefd66f1992dcecd9454df0771e1f29fae2b1d.tar.gz | |
Merge pull request #1238 from slskd/webhooks
Add the ability to call HTTP endpoints (webhooks) when application events are raised
Diffstat (limited to 'src/slskd/Core')
| -rw-r--r-- | src/slskd/Core/API/Controllers/ApplicationController.cs | 12 | ||||
| -rw-r--r-- | src/slskd/Core/Constants.cs | 23 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 100 |
3 files changed, 134 insertions, 1 deletions
diff --git a/src/slskd/Core/API/Controllers/ApplicationController.cs b/src/slskd/Core/API/Controllers/ApplicationController.cs index 790b6b3d..be646ae7 100644 --- a/src/slskd/Core/API/Controllers/ApplicationController.cs +++ b/src/slskd/Core/API/Controllers/ApplicationController.cs @@ -26,6 +26,7 @@ namespace slskd.Core.API using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; + using Serilog; /// <summary> /// Application. @@ -53,6 +54,7 @@ namespace slskd.Core.API private IStateMonitor<State> ApplicationStateMonitor { get; } private IOptionsMonitor<Options> OptionsMonitor { get; } private IHostApplicationLifetime Lifetime { get; } + private ILogger Log { get; } = Serilog.Log.ForContext<ApplicationController>(); /// <summary> /// Gets the current state of the application. @@ -149,5 +151,13 @@ namespace slskd.Core.API return PhysicalFile(file, "application/octet-stream", "slskd.dmp"); } + + [HttpPost("loopback")] + [Authorize(Policy = AuthPolicy.Any)] + public IActionResult Loopback([FromBody] object body) + { + Log.Information("Loopback POST: {Body}", body); + return Ok(); + } } -} +}
\ No newline at end of file diff --git a/src/slskd/Core/Constants.cs b/src/slskd/Core/Constants.cs new file mode 100644 index 00000000..380a3519 --- /dev/null +++ b/src/slskd/Core/Constants.cs @@ -0,0 +1,23 @@ +// <copyright file="Constants.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd; + +public static class Constants +{ + public static readonly string IgnoreCertificateErrors = nameof(IgnoreCertificateErrors); +}
\ No newline at end of file diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 306f9b87..e5f16b1e 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2006,6 +2006,12 @@ namespace slskd public class IntegrationOptions { /// <summary> + /// Gets webhook configuration. + /// </summary> + [Validate] + public Dictionary<string, WebhookOptions> Webhooks { get; init; } = []; + + /// <summary> /// Gets script configuration. /// </summary> [Validate] @@ -2024,6 +2030,100 @@ namespace slskd public PushbulletOptions Pushbullet { get; init; } = new PushbulletOptions(); /// <summary> + /// Webhook configuration. + /// </summary> + public class WebhookOptions + { + /// <summary> + /// Gets the list of Event types that trigger the webhook. + /// </summary> + [Enum(typeof(EventType))] + public string[] On { get; init; } = Array.Empty<string>(); + + /// <summary> + /// Gets details about the webhook call. + /// </summary> + [Validate] + public WebhookHttpOptions Call { get; init; } = new WebhookHttpOptions(); + + /// <summary> + /// Gets the time to wait before timing out, in milliseconds. + /// </summary> + [Range(500, int.MaxValue)] + public int Timeout { get; init; } = 5000; + + /// <summary> + /// Gets the retry configuration. + /// </summary> + [Validate] + public RetryOptions Retry { get; init; } = new RetryOptions(); + } + + /// <summary> + /// Webhook HTTP options. + /// </summary> + public class WebhookHttpOptions : IValidatableObject + { + /// <summary> + /// Gets the fully qualified URL for the webhook. + /// </summary> + [NotNullOrWhiteSpace] + public string Url { get; init; } + + /// <summary> + /// Gets the HTTP headers to include with the webhook. + /// </summary> + [Validate] + public WebhookHttpHeader[] Headers { get; init; } = []; + + /// <summary> + /// Gets a value indicating whether HTTPS certificate errors should be ignored. + /// </summary> + public bool IgnoreCertificateErrors { get; init; } = false; + + public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) + { + var results = new List<ValidationResult>(); + + if (!Url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && !Url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) + { + results.Add(new ValidationResult($"The {nameof(Url)} field must contain a fully qualified URL, including protocol (e.g. http:// or https://)")); + } + + return results; + } + } + + /// <summary> + /// Webhook HTTP header configuration. + /// </summary> + public class WebhookHttpHeader + { + /// <summary> + /// Gets the name of the header. + /// </summary> + [NotNullOrWhiteSpace] + public string Name { get; init; } + + /// <summary> + /// Gets the header's value. + /// </summary> + public string Value { get; init; } + } + + /// <summary> + /// Retry configuration. + /// </summary> + public class RetryOptions + { + /// <summary> + /// Gets the number of attempts to make before failing. + /// </summary> + [Range(1, int.MaxValue)] + public int Attempts { get; init; } = 1; + } + + /// <summary> /// Script configuration. /// </summary> public class ScriptOptions |
