From a84b5531b742604131ccbbd8e3b87ebfd5cbf9c1 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 27 Nov 2025 14:51:43 +0100 Subject: [PATCH] Add ToHttpRequestMessage and refactor HTTP handling Introduced a new extension method `ToHttpRequestMessage` in `HttpExtensions` to simplify `HttpRequestMessage` creation with URI validation. Added `System.Diagnostics.CodeAnalysis` for `StringSyntaxAttribute` support. Refactored `InvokeRecActionCommandHandler` to use the new `ToHttpRequestMessage` method, improving readability and encapsulation. Renamed `msg` to `reqMsg` for clarity. --- src/ReC.Application/Common/HttpExtensions.cs | 7 ++++++- .../RecActions/Commands/InvokeRecActionCommand.cs | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ReC.Application/Common/HttpExtensions.cs b/src/ReC.Application/Common/HttpExtensions.cs index f00ba3f..81e013b 100644 --- a/src/ReC.Application/Common/HttpExtensions.cs +++ b/src/ReC.Application/Common/HttpExtensions.cs @@ -1,4 +1,6 @@ -namespace ReC.Application.Common; +using System.Diagnostics.CodeAnalysis; + +namespace ReC.Application.Common; public static class HttpExtensions { @@ -18,4 +20,7 @@ public static class HttpExtensions public static HttpMethod ToHttpMethod(this string method) => _methods.TryGetValue(method, out var httpMethod) ? httpMethod : new HttpMethod(method); + + public static HttpRequestMessage ToHttpRequestMessage(this HttpMethod method, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri) + => new(method, requestUri); } diff --git a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs index adf3d0e..4c92de5 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs @@ -36,9 +36,11 @@ public class InvokeRecActionCommandHandler( return; } - using var msg = new HttpRequestMessage(request.RestType.ToHttpMethod(), request.EndpointUri); + using var reqMsg = request.RestType + .ToHttpMethod() + .ToHttpRequestMessage(request.EndpointUri); - using var response = await http.SendAsync(msg, cancel); + using var response = await http.SendAsync(reqMsg, cancel); var body = await response.Content.ReadAsStringAsync(cancel); var headers = response.Headers.ToDictionary(); }