using System.Text.Json; using Admin.NET.Plugin.AiDOP.DataPlatform.Core; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress; public sealed partial class MdpIngestDatasetController { [HttpGet("{id:long}/api-pull")] public Task ReadApiPull(long id, long? factoryId) => ApiPullAction(id, factoryId, TrialPermission, async (service, scope) => Ok(new { profiles = await service.ListProfilesAsync(scope.TenantId, scope.FactoryId, id), runs = await service.ListRunsAsync(scope.TenantId, scope.FactoryId, id), canApprove = await Can(PublishPermission) })); [HttpPost("{id:long}/api-pull/import"), RequestSizeLimit(524288)] public Task ImportApiPull(long id, long? factoryId, MdpApiPullImportInput input) => ApiPullAction(id, factoryId, TrialPermission, (_, _) => Task.FromResult(Ok(MdpApiPullImport.Read(input.Kind, input.Text, input.Selection)))); [HttpPost("{id:long}/api-pull/profiles"), RequestSizeLimit(65536)] public Task CreateApiPull(long id, long? factoryId, MdpApiPullCreateInput input, CancellationToken token) => ApiPullAction(id, factoryId, TrialPermission, async (service, scope) => Ok(new { id = await service.CreateAsync(scope.TenantId, scope.FactoryId, id, input.SourceId, user.UserId, input.Plan, token) })); [HttpPost("{id:long}/api-pull/profiles/{profile:long}/preflight")] public Task PreflightApiPull(long id, long profile, long? factoryId, CancellationToken token) => ApiPullAction(id, factoryId, TrialPermission, async (service, scope) => Ok(await service.PreflightAsync(scope.TenantId, scope.FactoryId, id, profile, user.UserId, token))); [HttpPost("{id:long}/api-pull/profiles/{profile:long}/activate")] public Task ActivateApiPull(long id, long profile, long? factoryId, CancellationToken token) => ApiPullAction(id, factoryId, PublishPermission, async (service, scope) => { await service.ActivateAsync(scope.TenantId, scope.FactoryId, id, profile, user.UserId, token); return NoContent(); }); [HttpPost("{id:long}/api-pull/profiles/{profile:long}/pause")] public Task PauseApiPull(long id, long profile, long? factoryId) => ApiPullAction(id, factoryId, PublishPermission, async (service, scope) => { await service.PauseAsync(scope.TenantId, scope.FactoryId, id, profile); return NoContent(); }); [HttpPost("{id:long}/api-pull/profiles/{profile:long}/runs")] public Task StartApiPull(long id, long profile, long? factoryId, CancellationToken token) => ApiPullAction(id, factoryId, PublishPermission, async (service, scope) => Ok(new { runId = await service.StartAsync(scope.TenantId, scope.FactoryId, id, profile, token) })); [HttpPost("{id:long}/api-pull/runs/{run:guid}/advance")] public Task AdvanceApiPull(long id, Guid run, long? factoryId, CancellationToken token) => ApiPullAction(id, factoryId, PublishPermission, async (service, scope) => Ok(new { phase = await service.AdvanceAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"), token) })); [HttpGet("{id:long}/api-pull/runs/{run:guid}/pages/{ordinal:int}/history")] public Task ReadApiPullRecoveryHistory(long id, Guid run, int ordinal, long? factoryId) => ApiPullAction(id, factoryId, TrialPermission, async (service, scope) => Ok(await service.ReadRecoveryHistoryAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"), ordinal))); [HttpPost("{id:long}/api-pull/runs/{run:guid}/pages/{ordinal:int}/attempts/{attempt:int}/raw")] public Task ExportApiPullRaw(long id, Guid run, int ordinal, int attempt, long? factoryId, CancellationToken token) => ApiPullAction(id, factoryId, PublishPermission, async (service, scope) => { var capture = await service.ExportCaptureAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"), ordinal, attempt, user.UserId, token); Response.Headers.CacheControl = "no-store"; Response.Headers["X-Content-Type-Options"] = "nosniff"; return File(capture.Payload, "application/octet-stream", $"{capture.BatchId}.raw"); }); [HttpPost("{id:long}/api-pull/runs/{run:guid}/recovery/preview"), RequestSizeLimit(4096)] public Task PreviewApiPullRecovery(long id, Guid run, long? factoryId, MdpApiPullRecoveryPreviewInput input, CancellationToken token) => ApiPullAction(id, factoryId, TrialPermission, async (service, scope) => Ok(await service.PreviewRecoveryAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"), input.ResponsePath, token))); [HttpPost("{id:long}/api-pull/runs/{run:guid}/recovery"), RequestSizeLimit(4096)] public Task RecoverApiPull(long id, Guid run, long? factoryId, MdpApiPullRecoveryInput input, CancellationToken token) => ApiPullAction(id, factoryId, PublishPermission, async (service, scope) => { await service.RecoverAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D"), user.UserId, input, token); return NoContent(); }); [HttpPost("{id:long}/api-pull/runs/{run:guid}/cancel")] public Task CancelApiPull(long id, Guid run, long? factoryId) => ApiPullAction(id, factoryId, PublishPermission, async (service, scope) => { await service.CancelAsync(scope.TenantId, scope.FactoryId, id, run.ToString("D")); return NoContent(); }); private Task ApiPullAction(long id, long? factoryId, string permission, Func> action) => WithScope(factoryId, async scope => { if (!await Can(permission)) return Forbid(); if (!await Datasets(scope).AnyAsync(row => row.Id == id)) return NotFound(); try { return await action(HttpContext.RequestServices.GetRequiredService(), scope); } catch (MdpApiPullException error) { var status = error.Code switch { "PULL_PROFILE_NOT_FOUND" or "PULL_CAPTURE_NOT_FOUND" => 404, "RAW_PAYLOAD_EXPIRED" => 410, "PULL_TARGET_FORBIDDEN" or "PULL_ADDRESS_FORBIDDEN" or "PULL_SOURCE_NOT_AUTHORIZED" or "PULL_CREDENTIAL_FORBIDDEN" => 403, "PULL_LOCAL_RATE_LIMITED" or "PULL_SOURCE_RATE_LIMITED" => 429, "PULL_TIMEOUT" => 504, "PULL_TEMPORARY" => 503, _ when error.Code.StartsWith("PULL_IMPORT", StringComparison.Ordinal) || error.Code is "PULL_CONFIG_INVALID" or "PULL_RECOVERY_INVALID" => 400, _ => 409 }; if (error.Retry is { } retry) Response.Headers.RetryAfter = Math.Clamp((int)retry.TotalSeconds, 1, 3600).ToString(); return StatusCode(status, new { code = error.Code, traceId = HttpContext.TraceIdentifier }); } catch (MdpIngestHttpException error) { return StatusCode(error.StatusCode, new { code = error.Code }); } catch (IngestContractException error) { return UnprocessableEntity(new { code = error.Code.ToString() }); } catch (Exception error) when (error is JsonException or ArgumentException) { return BadRequest(new { code = "PULL_CONFIG_INVALID" }); } catch (Exception error) when (error is System.Data.Common.DbException or SqlSugarException) { return StatusCode(503, new { code = "PULL_STORE_UNAVAILABLE", traceId = HttpContext.TraceIdentifier }); } }); } public sealed record MdpApiPullImportInput(string Kind, string Text, string? Selection); public sealed record MdpApiPullCreateInput(long SourceId, MdpApiPullPlan Plan); public sealed record MdpApiPullRecoveryPreviewInput(string ResponsePath);