| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System.Text.Json;
- using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
- [ApiController]
- [Route("api/v1/ingest")]
- [NonUnify]
- [Authorize(AuthenticationSchemes = MdpIngestProtocol.AuthenticationScheme)]
- public sealed class MdpIngestController(MdpIngestReceiveService receiver) : ControllerBase
- {
- private static readonly JsonSerializerOptions ResponseJson = new(JsonSerializerDefaults.Web);
- [HttpPost("{datasetKey}")]
- [ProducesResponseType(typeof(MdpIngestReceipt), StatusCodes.Status202Accepted)]
- public async Task<IActionResult> Receive([FromRoute] string datasetKey, CancellationToken cancellationToken)
- {
- if (HttpContext.Items[typeof(MdpIngestSubmission)] is not MdpIngestSubmission submission)
- return Error(StatusCodes.Status401Unauthorized, "AUTHENTICATION_REQUIRED");
- if (!string.Equals(submission.DatasetKey, datasetKey, StringComparison.Ordinal))
- return Error(StatusCodes.Status403Forbidden, "DATASET_UNAVAILABLE");
- try
- {
- var receipt = await receiver.ReceiveAsync(submission, cancellationToken);
- return IngestJson(StatusCodes.Status202Accepted, receipt);
- }
- catch (MdpIngestHttpException error)
- {
- return Error(error.StatusCode, error.Code);
- }
- catch (IngestContractException error)
- {
- var (status, code) = error.Code switch
- {
- IngestContractError.DatasetUnavailable => (StatusCodes.Status403Forbidden, "DATASET_UNAVAILABLE"),
- IngestContractError.UnknownSchema => (StatusCodes.Status422UnprocessableEntity, "UNKNOWN_SCHEMA"),
- IngestContractError.BodyTooLarge => (StatusCodes.Status413PayloadTooLarge, "BODY_TOO_LARGE"),
- IngestContractError.TooManyRecords => (StatusCodes.Status422UnprocessableEntity, "TOO_MANY_RECORDS"),
- IngestContractError.JsonTooDeep => (StatusCodes.Status422UnprocessableEntity, "JSON_TOO_DEEP"),
- IngestContractError.InvalidJson => (StatusCodes.Status400BadRequest, "INVALID_JSON"),
- _ => (StatusCodes.Status400BadRequest, "INVALID_ENVELOPE")
- };
- return Error(status, code);
- }
- }
- private ContentResult Error(int status, string code) =>
- IngestJson(status, new { code, traceId = HttpContext.TraceIdentifier });
- // 接收契约的字段名和 UTC 时间格式独立于后台页面的序列化设置。
- private static ContentResult IngestJson(int status, object body) => new()
- {
- Content = JsonSerializer.Serialize(body, ResponseJson),
- ContentType = "application/json; charset=utf-8",
- StatusCode = status
- };
- }
|