using System.Text.Json; using HermesSocketLibrary.Requests.Callbacks; using HermesSocketLibrary.Requests.Messages; using Serilog; using TwitchChatTTS.Hermes.Socket.Handlers; namespace TwitchChatTTS.Hermes.Socket.Requests { public class GetRedemptionsAck : IRequestAck { public string Name => "get_redemptions"; private readonly ICallbackManager _callbacks; private readonly JsonSerializerOptions _options; private readonly ILogger _logger; public GetRedemptionsAck( ICallbackManager callbacks, JsonSerializerOptions options, ILogger logger) { _callbacks = callbacks; _options = options; _logger = logger; } public void Acknowledge(string requestId, string json, IDictionary? requestData) { HermesRequestData? hermesRequestData = null; if (!string.IsNullOrEmpty(requestId)) { hermesRequestData = _callbacks.Take(requestId); if (hermesRequestData == null) _logger.Warning($"Could not find callback for request [request id: {requestId}][type: {Name}]"); else if (hermesRequestData.Data == null) hermesRequestData.Data = new Dictionary(); } IEnumerable? redemptions = JsonSerializer.Deserialize>(json, _options); if (redemptions != null) { _logger.Information($"Redemptions loaded [count: {redemptions.Count()}]"); if (hermesRequestData?.Data != null) { hermesRequestData.Data.Add("redemptions", redemptions); _logger.Debug($"Callback was found for request [request id: {requestId}][type: {Name}]"); hermesRequestData.Callback?.Invoke(hermesRequestData.Data); } } } } }