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

52 lines
2.0 KiB
C#

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)
_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()}]");
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);
}
}
}
}
}