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 Task Execute(SocketClient sender, Data data) { if (data is not RequestAckMessage message || message == null) return Task.CompletedTask; if (message.Request == null) { _logger.Warning("Received a Hermes request message without a proper request."); 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())}]"); var json = message.Data?.ToString(); if (message.Request.Type == null) { _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); return Task.CompletedTask; } } public class HermesRequestData { public Action?>? Callback { get; set; } public IDictionary? Data { get; set; } } }