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

52 lines
2.0 KiB
C#
Raw 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)
_logger.Warning($"Could not find callback for request [request id: {requestId}][type: {GetType().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 != null)
{
hermesRequestData.Data!.Add("redemptions", redemptions);
_logger.Debug($"Callback was found for request [request id: {requestId}][type: {GetType().Name}]");
hermesRequestData.Callback?.Invoke(hermesRequestData.Data);
}
}
}
}
}