Cleaned up request acks. Added internal service bus for internal messaging.
This commit is contained in:
52
Hermes/Socket/Requests/GetRedemptionsAck.cs
Normal file
52
Hermes/Socket/Requests/GetRedemptionsAck.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user