using Cleanuparr.Persistence.Models.Configuration.Notification; using Cleanuparr.Shared.Helpers; using Microsoft.Extensions.Logging; using Cleanuparr.Infrastructure.Json; namespace Cleanuparr.Infrastructure.Features.Notifications.Gotify; public sealed class GotifyProxy : IGotifyProxy { private readonly ILogger _logger; private readonly HttpClient _httpClient; private static readonly IReadOnlyDictionary StatusCodeMessages = new Dictionary { [401] = ("Unable to send notification | application token is invalid or unauthorized", false), [403] = ("Unable to send notification | application token is invalid or unauthorized", false), [404] = ("Unable to send notification | Gotify server not found", false), [502] = ("Unable to send notification | Gotify service unavailable", true), [503] = ("Unable to send notification | Gotify service unavailable", true), [504] = ("Unable to send notification | Gotify service unavailable", true), }; public GotifyProxy(ILogger logger, IHttpClientFactory httpClientFactory) { _logger = logger; _httpClient = httpClientFactory.CreateClient(Constants.HttpClientWithRetryName); } public async Task SendNotification(GotifyPayload payload, GotifyConfig config) { string baseUrl = config.ServerUrl.TrimEnd('/'); string url = $"{baseUrl}/message?token={config.ApplicationToken}"; using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url); await NotificationHttpSender.SendAsync( _httpClient, request, payload, CleanuparrJsonOptions.Notification, content => _logger.LogTrace("sending notification to Gotify: {Content}", content), (message, exception) => exception is null ? new GotifyException(message) : new GotifyException(message, exception), StatusCodeMessages); } }