hermes-socket-library/Requests/RequestManager.cs

41 lines
1.2 KiB
C#

using HermesSocketLibrary.Socket.Data;
using Serilog;
namespace HermesSocketLibrary.Requests
{
public class RequestManager : IRequestManager
{
private readonly IDictionary<string, IRequest> _requests;
private readonly ILogger _logger;
public RequestManager(IEnumerable<IRequest> requests, ILogger logger)
{
_logger = logger;
_requests = requests.ToDictionary(r => r.Name, r => r);
}
public async Task<RequestResult> Grant(string sender, RequestMessage? message)
{
if (message == null || message.Type == null)
return new RequestResult(false, null);
if (!_requests.TryGetValue(message.Type, out IRequest? request) || request == null)
{
_logger.Warning($"Did not find request type '{message.Type}'.");
return new RequestResult(false, null);
}
try
{
return await request.Grant(sender, message.Data);
}
catch (Exception e)
{
_logger.Error(e, $"Failed to grant a request of type '{message.Type}'.");
}
return new RequestResult(false, null);
}
}
}