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.
This commit is contained in:
tekh 2025-11-27 14:51:43 +01:00
parent 21e3171e11
commit a84b5531b7
2 changed files with 10 additions and 3 deletions

View File

@ -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);
}

View File

@ -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();
}