diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-07-12 11:24:36 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-12 11:24:36 -0500 |
| commit | de40c346bd5086eeab846f3fe3a329da81692f93 (patch) | |
| tree | 05635ddec17ce9bc5880b7b8aac57dfbf112fed5 /src/slskd/Integrations/Webhooks/WebhookService.cs | |
| parent | 7d302ce2a65bac34f0795ce5eafb3e06b746872d (diff) | |
| parent | eed42e9c4916e2ba653e40147de546e7af3af563 (diff) | |
| download | slskd-de40c346bd5086eeab846f3fe3a329da81692f93.tar.gz | |
Merge branch 'master' into dns
Diffstat (limited to 'src/slskd/Integrations/Webhooks/WebhookService.cs')
| -rw-r--r-- | src/slskd/Integrations/Webhooks/WebhookService.cs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/slskd/Integrations/Webhooks/WebhookService.cs b/src/slskd/Integrations/Webhooks/WebhookService.cs index 62f097b7..62739d7a 100644 --- a/src/slskd/Integrations/Webhooks/WebhookService.cs +++ b/src/slskd/Integrations/Webhooks/WebhookService.cs @@ -25,6 +25,9 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Text; +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; using Serilog; using slskd.Events; @@ -40,6 +43,15 @@ public class WebhookService OptionsMonitor = optionsMonitor; HttpClientFactory = httpClientFactory; + var json = new JsonSerializerOptions(); + json.Converters.Add(new IPAddressConverter()); + json.Converters.Add(new JsonStringEnumConverter()); + json.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + json.DefaultIgnoreCondition = JsonIgnoreCondition.Never; // include properties with null values + json.Encoder = JavaScriptEncoder.Default; // IMPORTANT! don't change this to 'UnsafeRelaxedJsonEscaping'; as the name implies, it's unsafe! + + JsonSerializerOptions = json; + Events.Subscribe<Event>(nameof(WebhookService), HandleEvent); Log.Debug("{Service} initialized", nameof(WebhookService)); @@ -49,6 +61,7 @@ public class WebhookService private IOptionsMonitor<Options> OptionsMonitor { get; } private EventBus Events { get; } private IHttpClientFactory HttpClientFactory { get; } + private JsonSerializerOptions JsonSerializerOptions { get; } private async Task HandleEvent(Event data) { @@ -80,7 +93,13 @@ public class WebhookService http.DefaultRequestHeaders.TryAddWithoutValidation(header.Name, header.Value); } - var content = new StringContent(data.ToJson(), Encoding.UTF8, "application/json"); + var content = new StringContent( + content: JsonSerializer.Serialize( + value: data, + inputType: data.GetType(), // if omitted object is serialized as EventType, losing everything else + options: JsonSerializerOptions), + encoding: Encoding.UTF8, + mediaType: "application/json"); HttpStatusCode? statusCode = null; |
