hermes-client/Hermes/Socket/Handlers/RequestAckHandler.cs

52 lines
1.7 KiB
C#
Raw Permalink Normal View History

2024-11-14 21:29:23 -05:00
using System.Text.Json;
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using HermesSocketLibrary.Socket.Data;
using Serilog;
using TwitchChatTTS.Hermes.Socket.Requests;
namespace TwitchChatTTS.Hermes.Socket.Handlers
{
public class RequestAckHandler : IWebSocketHandler
{
private readonly RequestAckManager _manager;
private readonly ILogger _logger;
public int OperationCode { get; } = 4;
public RequestAckHandler(
RequestAckManager manager,
ILogger logger
)
{
_manager = manager;
_logger = logger;
}
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
{
if (data is not RequestAckMessage message || message == null)
return;
if (message.Request == null)
{
_logger.Warning("Received a Hermes request message without a proper request.");
return;
}
_logger.Debug($"Received a Hermes request message [type: {message.Request.Type}][data: {string.Join(',', message.Request.Data?.Select(entry => entry.Key + '=' + entry.Value) ?? Array.Empty<string>())}]");
var json = message.Data?.ToString();
2024-11-14 21:29:23 -05:00
if (message.Request.Type == null)
{
return;
}
_manager.Fulfill(message.Request.Type, message.Request.RequestId, json, message.Request.Data);
}
}
public class HermesRequestData
{
public Action<IDictionary<string, object>?>? Callback { get; set; }
public IDictionary<string, object>? Data { get; set; }
}
}