using Json.LitJson; using System; using System.Collections.Generic; namespace ServiceClientGenerator { /// /// Algorithms for validating request and response integrity for supported operations. /// These are the algorithms supported by the .NET SDK, and the superset of the modeled /// response algorithms for a given operation. /// public enum ChecksumAlgorithm { CRC32C, CRC32, SHA256, SHA1 } /// /// Represents the request and response checksum configuration for a given operation, /// read from the "httpChecksum" trait applied at the operation-level in a service model. /// public class ChecksumConfiguration { /// /// Indicates if checksum MUST be present in the operation's HTTP request /// public bool RequestChecksumRequired { get; set; } /// /// Represents the member name of the operation input member used to configure the request checksum behavior. /// The operation input member MUST target an enum representing the supported checksum algorithms. /// public string RequestAlgorithmMember { get; set; } /// /// Represents the member name of the operation input member used to opt-in for the best effort validation of the checksums returned /// on the response. The operation input member MUST target an enum and the only supported enum value is "ENABLED". /// public string RequestValidationModeMember { get; set; } /// /// List of checksum algorithms for which clients should perform checksum validation /// if the checksum data value is present in the response headers. /// public List ResponseAlgorithms { get; set; } public ChecksumConfiguration(JsonData data) { if (data[ServiceModel.RequestChecksumRequiredKey] != null) { if (data[ServiceModel.RequestChecksumRequiredKey].IsBoolean) { RequestChecksumRequired = (bool)data[ServiceModel.RequestChecksumRequiredKey]; } else { throw new ArgumentOutOfRangeException($"{data[ServiceModel.RequestChecksumRequiredKey]} is not a valid boolean value for {ServiceModel.RequestChecksumRequiredKey}"); } } RequestAlgorithmMember = data.SafeGetString(ServiceModel.RequestAlgorithmMemberKey); RequestValidationModeMember = data.SafeGetString(ServiceModel.RequestValidationModeMemberKey); ResponseAlgorithms = ParseList(data[ServiceModel.ResponseAlgorithmsKey]); } public static List ParseList(JsonData jsonData) { var algorithms = new List(); if (jsonData != null && jsonData.IsArray) { var invalidAlgorithms = new List(); foreach (JsonData rawAlgorithm in jsonData) { if (Enum.TryParse((string)rawAlgorithm, false, out var parsedAlgorithm)) { algorithms.Add(parsedAlgorithm); } else { invalidAlgorithms.Add(rawAlgorithm.ToString()); } } if (invalidAlgorithms.Count > 0) { throw new ArgumentOutOfRangeException($"Found one or more more unsupported values for {nameof(ChecksumAlgorithm)}: {string.Join(",", invalidAlgorithms)}"); } } return algorithms; } } }