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 { 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.") }; } } }