| | | 1 | | using Microsoft.AspNetCore.Http; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.AspNetCore.Results; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides host-overridable defaults for mapping AsiBackbone Core outcomes into HTTP responses. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class AsiBackboneHttpResultMappingOptions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets or sets the status code returned for allowed governance decisions and successful operation results. |
| | | 12 | | /// </summary> |
| | 135 | 13 | | public int SuccessStatusCode { get; set; } = StatusCodes.Status200OK; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets or sets the status code returned for warning governance decisions. |
| | | 17 | | /// </summary> |
| | 127 | 18 | | public int WarningStatusCode { get; set; } = StatusCodes.Status200OK; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets or sets the status code returned for denied governance decisions. |
| | | 22 | | /// </summary> |
| | 133 | 23 | | public int DeniedStatusCode { get; set; } = StatusCodes.Status403Forbidden; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets or sets the status code returned for deferred governance decisions. |
| | | 27 | | /// </summary> |
| | 121 | 28 | | public int DeferredStatusCode { get; set; } = StatusCodes.Status202Accepted; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets or sets the status code returned for acknowledgment-required governance decisions. |
| | | 32 | | /// </summary> |
| | 119 | 33 | | public int AcknowledgmentRequiredStatusCode { get; set; } = StatusCodes.Status428PreconditionRequired; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets or sets the status code returned for escalation-recommended governance decisions. |
| | | 37 | | /// </summary> |
| | 117 | 38 | | public int EscalationRecommendedStatusCode { get; set; } = StatusCodes.Status409Conflict; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets the status code returned for failed operation results. |
| | | 42 | | /// </summary> |
| | 117 | 43 | | public int OperationFailureStatusCode { get; set; } = StatusCodes.Status400BadRequest; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets or sets the safe user-facing message used for non-success governance decisions. |
| | | 47 | | /// </summary> |
| | 60 | 48 | | public string GovernanceDecisionNotAllowedMessage { get; set; } = |
| | 59 | 49 | | "The governance decision did not allow immediate execution."; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets or sets the safe user-facing message used for failed operation results. |
| | | 53 | | /// </summary> |
| | 113 | 54 | | public string OperationFailureMessage { get; set; } = "The operation did not complete successfully."; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets or sets a value indicating whether reason messages may be exposed in HTTP responses. |
| | | 58 | | /// </summary> |
| | 32 | 59 | | public bool IncludeReasonMessages { get; set; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets or sets a value indicating whether trace identifiers may be exposed in HTTP responses. |
| | | 63 | | /// </summary> |
| | 22 | 64 | | public bool IncludeTraceId { get; set; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets or sets a value indicating whether policy version and policy hash values may be exposed in HTTP responses. |
| | | 68 | | /// </summary> |
| | 22 | 69 | | public bool IncludePolicyMetadata { get; set; } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Validates the configured mapping options. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <exception cref="InvalidOperationException">Thrown when a configured status code or message is invalid.</excepti |
| | | 75 | | public void Validate() |
| | | 76 | | { |
| | 64 | 77 | | ValidateStatusCode(SuccessStatusCode, nameof(SuccessStatusCode)); |
| | 62 | 78 | | ValidateStatusCode(WarningStatusCode, nameof(WarningStatusCode)); |
| | 60 | 79 | | ValidateStatusCode(DeniedStatusCode, nameof(DeniedStatusCode)); |
| | 56 | 80 | | ValidateStatusCode(DeferredStatusCode, nameof(DeferredStatusCode)); |
| | 54 | 81 | | ValidateStatusCode(AcknowledgmentRequiredStatusCode, nameof(AcknowledgmentRequiredStatusCode)); |
| | 52 | 82 | | ValidateStatusCode(EscalationRecommendedStatusCode, nameof(EscalationRecommendedStatusCode)); |
| | 50 | 83 | | ValidateStatusCode(OperationFailureStatusCode, nameof(OperationFailureStatusCode)); |
| | | 84 | | |
| | 48 | 85 | | if (string.IsNullOrWhiteSpace(GovernanceDecisionNotAllowedMessage)) |
| | | 86 | | { |
| | 2 | 87 | | throw new InvalidOperationException("A safe governance decision response message must be configured."); |
| | | 88 | | } |
| | | 89 | | |
| | 46 | 90 | | if (string.IsNullOrWhiteSpace(OperationFailureMessage)) |
| | | 91 | | { |
| | 2 | 92 | | throw new InvalidOperationException("A safe operation failure response message must be configured."); |
| | | 93 | | } |
| | 44 | 94 | | } |
| | | 95 | | |
| | | 96 | | private static void ValidateStatusCode(int statusCode, string propertyName) |
| | | 97 | | { |
| | 398 | 98 | | if (statusCode is < 100 or > 599) |
| | | 99 | | { |
| | 16 | 100 | | throw new InvalidOperationException($"{propertyName} must be a valid HTTP status code."); |
| | | 101 | | } |
| | 382 | 102 | | } |
| | | 103 | | } |