work
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using StellaOps.Policy.Engine.Streaming;
|
||||
|
||||
namespace StellaOps.Policy.Engine.Endpoints;
|
||||
|
||||
public static class PathScopeSimulationEndpoint
|
||||
{
|
||||
public static IEndpointRouteBuilder MapPathScopeSimulation(this IEndpointRouteBuilder routes)
|
||||
{
|
||||
routes.MapPost("/simulation/path-scope", HandleAsync)
|
||||
.WithName("PolicyEngine.PathScopeSimulation")
|
||||
.WithOpenApi();
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
private static async Task<IResult> HandleAsync(
|
||||
[FromBody] PathScopeSimulationRequest request,
|
||||
PathScopeSimulationService service,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var stream = service.StreamAsync(request, cancellationToken);
|
||||
var responseBuilder = new StringBuilder();
|
||||
|
||||
await foreach (var line in stream.ConfigureAwait(false))
|
||||
{
|
||||
responseBuilder.AppendLine(line);
|
||||
}
|
||||
|
||||
return Results.Text(responseBuilder.ToString(), "application/x-ndjson", Encoding.UTF8);
|
||||
}
|
||||
catch (PathScopeSimulationException ex)
|
||||
{
|
||||
var errorLine = JsonSerializer.Serialize(ex.Error);
|
||||
return Results.Text(errorLine + "\n", "application/x-ndjson", Encoding.UTF8, StatusCodes.Status400BadRequest);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user