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

58 lines
2.1 KiB
C#
Raw Normal View History

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;
}
2024-12-28 21:19:28 +00:00
public Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
{
if (data is not RequestAckMessage message || message == null)
2024-12-28 21:19:28 +00:00
return Task.CompletedTask;
if (message.Request == null)
{
_logger.Warning("Received a Hermes request message without a proper request.");
2024-12-28 21:19:28 +00:00
return Task.CompletedTask;
}
_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-15 02:29:23 +00:00
if (message.Request.Type == null)
{
2024-12-28 21:19:28 +00:00
_logger.Warning("Request type is null. Unknown what acknowlegement this is for.");
return Task.CompletedTask;
}
if (!string.IsNullOrWhiteSpace(message.Error))
{
_logger.Warning("An error occured while processing the request on the server: " + message.Error);
return Task.CompletedTask;
}
_manager.Fulfill(message.Request.Type, message.Request.RequestId, json, message.Request.Data);
2024-12-28 21:19:28 +00:00
return Task.CompletedTask;
}
}
public class HermesRequestData
{
public Action<IDictionary<string, object>?>? Callback { get; set; }
public IDictionary<string, object>? Data { get; set; }
}
}