From 7c00060f7489c7de75a895acfc0a77a49e25b76d Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Wed, 26 Nov 2025 21:35:53 +0100 Subject: [PATCH] Refactor HTTP handling in InvokeRecActionCommand Updated `ReC.Application.csproj` to include `Microsoft.Extensions.Http` for improved HTTP client management. Refactored `InvokeRecActionCommandHandler` to use `IHttpClientFactory` for creating `HttpClient` instances, enhancing resource efficiency. Simplified HTTP method handling by replacing the switch statement with a dynamic `HttpMethod` and `HttpRequestMessage` approach. Removed redundant `using` directives and renamed `header` to `headers` for clarity. Removed `BadRequestException` for unsupported REST types, streamlining error handling. --- src/ReC.Application/ReC.Application.csproj | 1 + .../Commands/InvokeRecActionCommand.cs | 29 ++++++------------- 2 files changed, 10 insertions(+), 20 deletions(-) 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 +}