diff --git a/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs index e993fea..b9c4d3e 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionsCommand.cs @@ -27,22 +27,7 @@ public class InvokeRecActionsCommandHandler(ISender sender, IHttpClientFactory c await semaphore.WaitAsync(cancel); try { - if (action.RestType is null) - { - logger?.LogWarning( - "Rec action could not be invoked because the RestType value is null. ProfileId: {ProfileId}, ActionId: {ActionId}", - action.ProfileId, - action.ActionId - ); - return; - } - - var method = new HttpMethod(action.RestType.ToUpper()); - var msg = new HttpRequestMessage(method, action.EndpointUri); - - var response = await http.SendAsync(msg, cancel); - var body = await response.Content.ReadAsStringAsync(cancel); - var headers = response.Headers.ToDictionary(); + await sender.Send(action.ToInvokeCommand(), cancel); } catch(Exception ex) { diff --git a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs index 2821435..bd56113 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs @@ -1,13 +1,12 @@ -using ReC.Application.Common.Dto; +using MediatR; +using Microsoft.Extensions.Logging; +using ReC.Application.Common.Dto; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using static System.Net.WebRequestMethods; namespace ReC.Application.RecActions.Commands; -public record InvokeRecActionCommand : RecActionDto +public record InvokeRecActionCommand : RecActionDto, IRequest { public InvokeRecActionCommand(RecActionDto root) : base(root) { } @@ -18,3 +17,31 @@ public static class InvokeRecActionCommandExtensions { public static InvokeRecActionCommand ToInvokeCommand(this RecActionDto dto) => new(dto); } + +public class InvokeRecActionCommandHandler( + IHttpClientFactory clientFactory, + ILogger? logger = null + ) : IRequestHandler +{ + public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel) + { + var http = clientFactory.CreateClient(); + + if (request.RestType is null) + { + logger?.LogWarning( + "Rec action could not be invoked because the RestType value is null. ProfileId: {ProfileId}, ActionId: {ActionId}", + request.ProfileId, + request.ActionId + ); + return; + } + + var method = new HttpMethod(request.RestType.ToUpper()); + var msg = new HttpRequestMessage(method, request.EndpointUri); + + var response = await http.SendAsync(msg, cancel); + var body = await response.Content.ReadAsStringAsync(cancel); + var headers = response.Headers.ToDictionary(); + } +} \ No newline at end of file