ReC/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs
TekH a66a70fab3 Refactor HTTP request handling in InvokeRecActionCommand
Replaced the `reqMsg` variable with `httpReq` to simplify the
creation and usage of `HttpRequestMessage`. The `request.RestType`
is now used directly to create `httpReq`, which is passed to
`http.SendAsync`. This change reduces redundancy and improves
code clarity.
2025-11-27 14:54:28 +01:00

47 lines
1.5 KiB
C#

using MediatR;
using Microsoft.Extensions.Logging;
using ReC.Application.Common;
using ReC.Application.Common.Dto;
namespace ReC.Application.RecActions.Commands;
public record InvokeRecActionCommand : RecActionDto, IRequest
{
public InvokeRecActionCommand(RecActionDto root) : base(root) { }
public InvokeRecActionCommand() { }
}
public static class InvokeRecActionCommandExtensions
{
public static InvokeRecActionCommand ToInvokeCommand(this RecActionDto dto) => new(dto);
}
public class InvokeRecActionCommandHandler(
IHttpClientFactory clientFactory,
ILogger<InvokeRecActionsCommandHandler>? logger = null
) : IRequestHandler<InvokeRecActionCommand>
{
public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel)
{
using var http = clientFactory.CreateClient();
if (request.RestType is null)
{
logger?.LogWarning(
"Rec action could not be invoked because the RestType value is null. ProfileId: {ProfileId}, ActionId: {ActionId}",
request.ProfileId,
request.ActionId
);
return;
}
using var httpReq = request.RestType
.ToHttpMethod()
.ToHttpRequestMessage(request.EndpointUri);
using var response = await http.SendAsync(httpReq, cancel);
var body = await response.Content.ReadAsStringAsync(cancel);
var headers = response.Headers.ToDictionary();
}
}