Refactor Rec Actions handling with MediatR support
Refactored the handling of "Rec Actions" by introducing the `InvokeRecActionCommandHandler` class and leveraging the MediatR library for command handling. Simplified the logic in `InvokeBatchRecActionsCommand.cs` by delegating HTTP request handling to the `sender.Send` method, which now uses `InvokeRecActionCommand`. Updated `InvokeRecActionCommand` to implement the `IRequest` interface, enabling MediatR pipeline compatibility. Added `InvokeRecActionCommandExtensions` for converting `RecActionDto` to `InvokeRecActionCommand`, improving readability. Centralized HTTP request logic in the new `InvokeRecActionCommandHandler`, which validates `RestType`, sends HTTP requests, and logs warnings for invalid actions. Removed unused `using` directives and added necessary ones for MediatR and logging. This refactoring improves modularity, testability, and maintainability by separating concerns and streamlining the code structure.
This commit is contained in:
parent
d1e8f619f5
commit
2f3a685e7d
@ -27,22 +27,7 @@ public class InvokeRecActionsCommandHandler(ISender sender, IHttpClientFactory c
|
|||||||
await semaphore.WaitAsync(cancel);
|
await semaphore.WaitAsync(cancel);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (action.RestType is null)
|
await sender.Send(action.ToInvokeCommand(), cancel);
|
||||||
{
|
|
||||||
logger?.LogWarning(
|
|
||||||
"Rec action could not be invoked because the RestType value is null. ProfileId: {ProfileId}, ActionId: {ActionId}",
|
|
||||||
action.ProfileId,
|
|
||||||
action.ActionId
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var method = new HttpMethod(action.RestType.ToUpper());
|
|
||||||
var msg = new HttpRequestMessage(method, action.EndpointUri);
|
|
||||||
|
|
||||||
var response = await http.SendAsync(msg, cancel);
|
|
||||||
var body = await response.Content.ReadAsStringAsync(cancel);
|
|
||||||
var headers = response.Headers.ToDictionary();
|
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch(Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
using ReC.Application.Common.Dto;
|
using MediatR;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using ReC.Application.Common.Dto;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using static System.Net.WebRequestMethods;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace ReC.Application.RecActions.Commands;
|
namespace ReC.Application.RecActions.Commands;
|
||||||
|
|
||||||
public record InvokeRecActionCommand : RecActionDto
|
public record InvokeRecActionCommand : RecActionDto, IRequest
|
||||||
{
|
{
|
||||||
public InvokeRecActionCommand(RecActionDto root) : base(root) { }
|
public InvokeRecActionCommand(RecActionDto root) : base(root) { }
|
||||||
|
|
||||||
@ -18,3 +17,31 @@ public static class InvokeRecActionCommandExtensions
|
|||||||
{
|
{
|
||||||
public static InvokeRecActionCommand ToInvokeCommand(this RecActionDto dto) => new(dto);
|
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)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
var method = new HttpMethod(request.RestType.ToUpper());
|
||||||
|
var msg = new HttpRequestMessage(method, request.EndpointUri);
|
||||||
|
|
||||||
|
var response = await http.SendAsync(msg, cancel);
|
||||||
|
var body = await response.Content.ReadAsStringAsync(cancel);
|
||||||
|
var headers = response.Headers.ToDictionary();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user