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

38 lines
1.3 KiB
C#

using Serilog;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class RequestAckManager
{
private readonly IDictionary<string, IRequestAck> _acknowledgements;
private readonly ILogger _logger;
public RequestAckManager(IEnumerable<IRequestAck> acks, ILogger logger)
{
_acknowledgements = acks.ToDictionary(a => a.Name, a => a);
_logger = logger;
}
public void Fulfill(string type, string requestId, string data, IDictionary<string, object>? requestData)
{
if (data == null)
return;
if (!_acknowledgements.TryGetValue(type, out var ack))
{
_logger.Warning($"Found unknown request type when acknowledging [type: {type}]");
return;
}
_logger.Debug($"Request acknowledgement found [type: {type}][data: {data}]");
try
{
ack.Acknowledge(requestId, data, requestData);
_logger.Debug($"Request acknowledged without error [type: {type}][data: {data}]");
}
catch (Exception ex)
{
_logger.Error(ex, "Failed to fulfill a request ackowledgement.");
}
}
}
}