98 lines
3.8 KiB
C#
98 lines
3.8 KiB
C#
using System;
|
|
using System.Configuration;
|
|
using Validators = Kreta.Core.Configuration.Validators;
|
|
|
|
namespace Kreta.Naplo.Configuration.Kreta
|
|
{
|
|
/// <summary>
|
|
/// Authorization configuration section
|
|
/// </summary>
|
|
/// <seealso cref="System.Configuration.ConfigurationSection" />
|
|
public class KretaNaploApiConfiguration : ConfigurationSection, IKretaNaploApiConfiguration
|
|
{
|
|
/// <summary>
|
|
/// Name of section
|
|
/// </summary>
|
|
const string SectionName = "KretaNaploApi";
|
|
|
|
/// <summary>
|
|
/// The instance
|
|
/// </summary>
|
|
static Lazy<KretaNaploApiConfiguration> instance;
|
|
|
|
/// <summary>
|
|
/// Gets the instance.
|
|
/// </summary>
|
|
/// <value>
|
|
/// The instance.
|
|
/// </value>
|
|
public static KretaNaploApiConfiguration Instance
|
|
{
|
|
get
|
|
{
|
|
return instance.Value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the kreta API key.
|
|
/// </summary>
|
|
/// <value>
|
|
/// The kreta API key.
|
|
/// </value>
|
|
/// <remarks>.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.</remarks>
|
|
[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)];
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the base URL.
|
|
/// </summary>
|
|
/// <value>
|
|
/// The base URL.
|
|
/// </value>
|
|
/// <remarks>.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.</remarks>
|
|
[ConfigurationProperty(nameof(BaseUrl), IsRequired = true, DefaultValue = Validators.StringValidator.SkipValidationForDefaultValue)]
|
|
[Validators.UrlValidator(Validators.UrlType.HttpBaseUrl)]
|
|
public string BaseUrl
|
|
{
|
|
get
|
|
{
|
|
return (string)this[nameof(BaseUrl)];
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="KretaNaploApiConfiguration"/> class.
|
|
/// </summary>
|
|
static KretaNaploApiConfiguration()
|
|
{
|
|
instance = new Lazy<KretaNaploApiConfiguration>(() =>
|
|
{
|
|
var section = (KretaNaploApiConfiguration)ConfigurationManager.GetSection(SectionName);
|
|
|
|
if (section == null)
|
|
{
|
|
throw new ConfigurationErrorsException($"{SectionName} configuration section was not found");
|
|
}
|
|
|
|
return section;
|
|
});
|
|
}
|
|
}
|
|
}
|