Replaced the `switch` statement with a `switch` expression to handle HTTP methods (`GET`, `POST`, `PUT`, `DELETE`). This refactoring reduces boilerplate code and improves readability by directly assigning the HTTP request result to the `response` variable, eliminating the need for multiple `using` and `break` statements.
38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
using DigitalData.Core.Exceptions;
|
|
using MediatR;
|
|
using ReC.Application.RecActions.Queries;
|
|
using System.Net.Http;
|
|
|
|
namespace ReC.Application.RecActions.Commands;
|
|
|
|
public class InvokeRecActionCommand : ReadRecActionQuery, IRequest
|
|
{
|
|
}
|
|
|
|
public static class InvokeRecActionCommandExtensions
|
|
{
|
|
public static Task InvokeRecAction(this ISender sender, int profileId)
|
|
=> sender.Send(new InvokeRecActionCommand { ProfileId = profileId });
|
|
}
|
|
|
|
public class InvokeRecActionCommandHandler(ISender sender) : IRequestHandler<InvokeRecActionCommand>
|
|
{
|
|
public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel)
|
|
{
|
|
var actions = await sender.Send(request as ReadRecActionQuery, cancel);
|
|
|
|
foreach (var action in actions)
|
|
{
|
|
using var http = new HttpClient();
|
|
|
|
var response = action.RestType?.ToUpper().Trim() switch
|
|
{
|
|
"GET" => await http.GetAsync(action.EndpointUri, cancel),
|
|
"POST" => await http.PostAsync(action.EndpointUri, null, cancel),
|
|
"PUT" => await http.PutAsync(action.EndpointUri, null, cancel),
|
|
"DELETE" => await http.DeleteAsync(action.EndpointUri, cancel),
|
|
_ => throw new BadRequestException($"The REST type {action.RestType} is not supported.")
|
|
};
|
|
}
|
|
}
|
|
} |