diff --git a/src/ReC.Application/ReC.Application.csproj b/src/ReC.Application/ReC.Application.csproj index 103d5b3..6cc1027 100644 --- a/src/ReC.Application/ReC.Application.csproj +++ b/src/ReC.Application/ReC.Application.csproj @@ -13,6 +13,7 @@ + diff --git a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs index 93718a4..b13eb88 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs @@ -1,7 +1,5 @@ -using DigitalData.Core.Exceptions; -using MediatR; +using MediatR; using ReC.Application.RecActions.Queries; -using System.Net.Http; namespace ReC.Application.RecActions.Commands; @@ -15,31 +13,22 @@ public static class InvokeRecActionCommandExtensions => sender.Send(new InvokeRecActionCommand { ProfileId = profileId }); } -public class InvokeRecActionCommandHandler(ISender sender) : IRequestHandler +public class InvokeRecActionCommandHandler(ISender sender, IHttpClientFactory clientFactory) : IRequestHandler { public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel) { var actions = await sender.Send(request as ReadRecActionQuery, cancel); + var http = clientFactory.CreateClient(); + foreach (var action in actions) { - using var http = new HttpClient(); - - var response = action.RestType?.ToUpper().Trim() switch - { - "GET" => await http.GetAsync(action.EndpointUri, cancel), - "POST" => await http.PostAsync(action.EndpointUri, null, cancel), - "PUT" => await http.PutAsync(action.EndpointUri, null, cancel), - "DELETE" => await http.DeleteAsync(action.EndpointUri, cancel), - "PATCH" => await http.PatchAsync(action.EndpointUri, null, cancel), - "HEAD" => await http.SendAsync(new HttpRequestMessage(HttpMethod.Head, action.EndpointUri), cancel), - "OPTIONS" => await http.SendAsync(new HttpRequestMessage(HttpMethod.Options, action.EndpointUri), cancel), - "TRACE" => await http.SendAsync(new HttpRequestMessage(HttpMethod.Trace, action.EndpointUri), cancel), - _ => throw new BadRequestException($"The REST type {action.RestType} is not supported.") - }; + 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 header = response.Headers.ToDictionary(); + var headers = response.Headers.ToDictionary(); } } -} \ No newline at end of file +}