From 7a4885c86a428e44490c7ac7ecfceb9d4c9e5b99 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 27 Nov 2025 11:45:15 +0100 Subject: [PATCH] Refactor HTTP method handling and cleanup Refactored HTTP method handling by introducing a `ToHttpMethod` extension method in `HttpExtensions.cs` to convert string representations of HTTP methods to `HttpMethod` objects. Replaced manual `HttpMethod` instantiation with the new extension method in `InvokeRecActionCommandHandler` for improved readability and reusability. Removed unused `using` directives from `HttpExtensions.cs` and cleaned up the `namespace` declaration. Added a `using` directive for `ReC.Application.Common` in `InvokeRecActionCommand.cs` to support the new extension method. --- src/ReC.Application/Common/HttpExtensions.cs | 20 ++++++++++++------- .../Commands/InvokeRecActionCommand.cs | 4 ++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/ReC.Application/Common/HttpExtensions.cs b/src/ReC.Application/Common/HttpExtensions.cs index b24f6b4..6f3b4b0 100644 --- a/src/ReC.Application/Common/HttpExtensions.cs +++ b/src/ReC.Application/Common/HttpExtensions.cs @@ -1,11 +1,17 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ReC.Application.Common; +namespace ReC.Application.Common; public static class HttpExtensions { + public static HttpMethod ToHttpMethod(this string method) => method.ToUpper() switch + { + "GET" => HttpMethod.Get, + "POST" => HttpMethod.Post, + "PUT" => HttpMethod.Put, + "DELETE" => HttpMethod.Delete, + "PATCH" => HttpMethod.Patch, + "HEAD" => HttpMethod.Head, + "OPTIONS" => HttpMethod.Options, + "TRACE" => HttpMethod.Trace, + _ => throw new ArgumentException($"Invalid HTTP method: {method}", nameof(method)), + }; } diff --git a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs index 431490c..adf3d0e 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs @@ -1,5 +1,6 @@ using MediatR; using Microsoft.Extensions.Logging; +using ReC.Application.Common; using ReC.Application.Common.Dto; namespace ReC.Application.RecActions.Commands; @@ -35,8 +36,7 @@ public class InvokeRecActionCommandHandler( return; } - var method = new HttpMethod(request.RestType.ToUpper()); - using var msg = new HttpRequestMessage(method, request.EndpointUri); + using var msg = new HttpRequestMessage(request.RestType.ToHttpMethod(), request.EndpointUri); using var response = await http.SendAsync(msg, cancel); var body = await response.Content.ReadAsStringAsync(cancel);