From e529027587e0102ccb910b96ce0e0b4346132b0d Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 15 Dec 2025 12:48:10 +0100 Subject: [PATCH] Refactor HTTP method handling to use RestType enum Replaced string-based HTTP method mapping with a strongly-typed RestType enum for improved type safety. Updated the ToHttpMethod extension to accept RestType, validate its value, and convert it to the appropriate HttpMethod. This reduces errors from invalid or misspelled method names. --- src/ReC.Application/Common/HttpExtensions.cs | 31 +++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/ReC.Application/Common/HttpExtensions.cs b/src/ReC.Application/Common/HttpExtensions.cs index 81e013b..cecc0e7 100644 --- a/src/ReC.Application/Common/HttpExtensions.cs +++ b/src/ReC.Application/Common/HttpExtensions.cs @@ -1,25 +1,28 @@ -using System.Diagnostics.CodeAnalysis; +using ReC.Domain.Constants; +using System.Diagnostics.CodeAnalysis; namespace ReC.Application.Common; public static class HttpExtensions { - private static readonly Dictionary _methods = new(StringComparer.OrdinalIgnoreCase) + private static readonly Dictionary _methods = new() { - ["GET"] = HttpMethod.Get, - ["POST"] = HttpMethod.Post, - ["PUT"] = HttpMethod.Put, - ["DELETE"] = HttpMethod.Delete, - ["PATCH"] = HttpMethod.Patch, - ["HEAD"] = HttpMethod.Head, - ["OPTIONS"] = HttpMethod.Options, - ["TRACE"] = HttpMethod.Trace, - ["CONNECT"] = HttpMethod.Connect + [RestType.Get] = HttpMethod.Get, + [RestType.Post] = HttpMethod.Post, + [RestType.Put] = HttpMethod.Put, + [RestType.Delete] = HttpMethod.Delete, + [RestType.Patch] = HttpMethod.Patch, + [RestType.Head] = HttpMethod.Head, + [RestType.Options] = HttpMethod.Options, + [RestType.Trace] = HttpMethod.Trace, + [RestType.Connect] = HttpMethod.Connect }; - public static HttpMethod ToHttpMethod(this string method) => _methods.TryGetValue(method, out var httpMethod) - ? httpMethod - : new HttpMethod(method); + public static HttpMethod ToHttpMethod(this RestType method) => !method.IsValid() + ? throw new ArgumentOutOfRangeException(nameof(method), $"The RestType value '{method}' is not valid.") + : _methods.TryGetValue(method, out var httpMethod) + ? httpMethod + : new HttpMethod(method.ToHttpMethodName()); public static HttpRequestMessage ToHttpRequestMessage(this HttpMethod method, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri) => new(method, requestUri);