hermes-client/Hermes/Socket/Requests/GetRedemptionsAck.cs

52 lines
2.0 KiB
C#
Raw Permalink Normal View History

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<HermesRequestData> _callbacks;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public GetRedemptionsAck(
ICallbackManager<HermesRequestData> callbacks,
JsonSerializerOptions options,
ILogger logger)
{
_callbacks = callbacks;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string json, IDictionary<string, object>? requestData)
{
HermesRequestData? hermesRequestData = null;
if (!string.IsNullOrEmpty(requestId))
{
hermesRequestData = _callbacks.Take(requestId);
if (hermesRequestData == null)
2024-11-08 11:11:24 -05:00
_logger.Warning($"Could not find callback for request [request id: {requestId}][type: {Name}]");
else if (hermesRequestData.Data == null)
hermesRequestData.Data = new Dictionary<string, object>();
}
IEnumerable<Redemption>? redemptions = JsonSerializer.Deserialize<IEnumerable<Redemption>>(json, _options);
if (redemptions != null)
{
_logger.Information($"Redemptions loaded [count: {redemptions.Count()}]");
2024-11-08 11:11:24 -05:00
if (hermesRequestData?.Data != null)
{
2024-11-08 11:11:24 -05:00
hermesRequestData.Data.Add("redemptions", redemptions);
2024-11-08 11:11:24 -05:00
_logger.Debug($"Callback was found for request [request id: {requestId}][type: {Name}]");
hermesRequestData.Callback?.Invoke(hermesRequestData.Data);
}
}
}
}
}