using System; using System.Configuration; using Validators = Kreta.Core.Configuration.Validators; namespace Kreta.Naplo.Configuration.Kreta { /// /// Authorization configuration section /// /// public class KretaNaploApiConfiguration : ConfigurationSection, IKretaNaploApiConfiguration { /// /// Name of section /// const string SectionName = "KretaNaploApi"; /// /// The instance /// static Lazy instance; /// /// Gets the instance. /// /// /// The instance. /// public static KretaNaploApiConfiguration Instance { get { return instance.Value; } } /// /// Gets the kreta API key. /// /// /// The kreta API key. /// /// .NET ConfigurationProperty constructor is buggy: it sets the default value of string type to string.Empty (not null), then /// calls all validator for default value too. So in normal way you can't configure e.g. "MinLength" constaint, wihtout explicitly set "Default" /// to a string with length larger than "MinLength". The workaround is to write a custom valitor and set "DefaultValue" to a special /// string that theese validators knows, in order to skip validations when ConfigurationProperty calls them for default values. /// This is a known issue in .NET framework. [ConfigurationProperty(nameof(ApiKey), IsRequired = true, DefaultValue = Validators.StringValidator.SkipValidationForDefaultValue)] [Validators.StringValidator(false, MinLength = 36, MaxLength = 256)] public string ApiKey { get { return (string)this[nameof(ApiKey)]; } } /// /// Gets the base URL. /// /// /// The base URL. /// /// .NET ConfigurationProperty constructor is buggy: it sets the default value of string type to string.Empty (not null), then /// calls all validator for default value too. So in normal way you can't configure e.g. "MinLength" constaint, wihtout explicitly set "Default" /// to a string with length larger than "MinLength". The workaround is to write a custom valitor and set "DefaultValue" to a special /// string that theese validators knows, in order to skip validations when ConfigurationProperty calls them for default values. /// This is a known issue in .NET framework. [ConfigurationProperty(nameof(BaseUrl), IsRequired = true, DefaultValue = Validators.StringValidator.SkipValidationForDefaultValue)] [Validators.UrlValidator(Validators.UrlType.HttpBaseUrl)] public string BaseUrl { get { return (string)this[nameof(BaseUrl)]; } } /// /// Initializes a new instance of the class. /// static KretaNaploApiConfiguration() { instance = new Lazy(() => { var section = (KretaNaploApiConfiguration)ConfigurationManager.GetSection(SectionName); if (section == null) { throw new ConfigurationErrorsException($"{SectionName} configuration section was not found"); } return section; }); } } }