Refactor HTTP handling in InvokeRecActionCommand
Updated `ReC.Application.csproj` to include `Microsoft.Extensions.Http` for improved HTTP client management. Refactored `InvokeRecActionCommandHandler` to use `IHttpClientFactory` for creating `HttpClient` instances, enhancing resource efficiency. Simplified HTTP method handling by replacing the switch statement with a dynamic `HttpMethod` and `HttpRequestMessage` approach. Removed redundant `using` directives and renamed `header` to `headers` for clarity. Removed `BadRequestException` for unsupported REST types, streamlining error handling.
This commit is contained in:
parent
0837bca6cb
commit
7c00060f74
@ -13,6 +13,7 @@
|
||||
<PackageReference Include="DigitalData.Core.Exceptions" Version="1.1.0" />
|
||||
<PackageReference Include="MediatR" Version="13.1.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.11" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
using DigitalData.Core.Exceptions;
|
||||
using MediatR;
|
||||
using MediatR;
|
||||
using ReC.Application.RecActions.Queries;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace ReC.Application.RecActions.Commands;
|
||||
|
||||
@ -15,31 +13,22 @@ public static class InvokeRecActionCommandExtensions
|
||||
=> sender.Send(new InvokeRecActionCommand { ProfileId = profileId });
|
||||
}
|
||||
|
||||
public class InvokeRecActionCommandHandler(ISender sender) : IRequestHandler<InvokeRecActionCommand>
|
||||
public class InvokeRecActionCommandHandler(ISender sender, IHttpClientFactory clientFactory) : IRequestHandler<InvokeRecActionCommand>
|
||||
{
|
||||
public async Task Handle(InvokeRecActionCommand request, CancellationToken cancel)
|
||||
{
|
||||
var actions = await sender.Send(request as ReadRecActionQuery, cancel);
|
||||
|
||||
var http = clientFactory.CreateClient();
|
||||
|
||||
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),
|
||||
"PATCH" => await http.PatchAsync(action.EndpointUri, null, cancel),
|
||||
"HEAD" => await http.SendAsync(new HttpRequestMessage(HttpMethod.Head, action.EndpointUri), cancel),
|
||||
"OPTIONS" => await http.SendAsync(new HttpRequestMessage(HttpMethod.Options, action.EndpointUri), cancel),
|
||||
"TRACE" => await http.SendAsync(new HttpRequestMessage(HttpMethod.Trace, action.EndpointUri), cancel),
|
||||
_ => throw new BadRequestException($"The REST type {action.RestType} is not supported.")
|
||||
};
|
||||
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 header = response.Headers.ToDictionary();
|
||||
var headers = response.Headers.ToDictionary();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user