| | 1 | | using System; |
| | 2 | | using System.ComponentModel.DataAnnotations; |
| | 3 | | using System.Globalization; |
| | 4 | | using System.Net.Mail; |
| | 5 | | using Microsoft.Extensions.Configuration; |
| | 6 | |
|
| | 7 | | namespace ApplicationCore |
| | 8 | | { |
| | 9 | | public interface IWebConfiguration |
| | 10 | | { |
| | 11 | | CommentConfig Comment { get; } |
| | 12 | | GitHubConfig GitHub { get; } |
| | 13 | | TextAnalyticsConfig TextAnalytics { get; } |
| | 14 | | } |
| | 15 | |
|
| | 16 | | public class WebConfiguration : IWebConfiguration |
| | 17 | | { |
| | 18 | | public CommentConfig Comment => _configProvider.GetConfig<CommentConfig>(_config); |
| | 19 | |
|
| | 20 | | public GitHubConfig GitHub => _configProvider.GetConfig<GitHubConfig>(_config); |
| | 21 | |
|
| | 22 | | public TextAnalyticsConfig TextAnalytics => _configProvider.GetConfig<TextAnalyticsConfig>(_config); |
| | 23 | |
|
| | 24 | | private readonly IConfigurationRoot _config; |
| | 25 | | private readonly IConfigProvider _configProvider; |
| | 26 | |
|
| | 27 | | public WebConfiguration(IConfigurationRoot config, IConfigProvider provider) |
| | 28 | | { |
| | 29 | | _config = config; |
| | 30 | | _configProvider = provider; |
| | 31 | | } |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public class CommentConfig |
| | 35 | | { |
| 47 | 36 | | public Uri WebsiteUrl { get; set; } = null!; |
| | 37 | |
|
| 2 | 38 | | public MailAddress FallbackCommitEmailAddress => new MailAddress(FallbackCommitEmail); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// <see cref="MailAddress"/> is non-serializable. This property is used as a proxy when |
| | 42 | | /// <see cref="BinderOptions.BindNonPublicProperties"/> is set to <c>true</c> and when binding the section to |
| | 43 | | /// the actual config instance. |
| | 44 | | /// </summary> |
| 32 | 45 | | private string FallbackCommitEmail { get; set; } = ""; |
| | 46 | |
|
| 45 | 47 | | public CommentConfig() { } |
| | 48 | |
|
| 11 | 49 | | public CommentConfig(string website, string fallbackCommitEmailAddress) |
| 11 | 50 | | { |
| 11 | 51 | | WebsiteUrl = GetValidUri(website); |
| 9 | 52 | | FallbackCommitEmail = GetValidEmail(fallbackCommitEmailAddress); |
| 8 | 53 | | } |
| | 54 | |
|
| | 55 | | private Uri GetValidUri(string website) |
| 11 | 56 | | { |
| 11 | 57 | | if (!Uri.TryCreate(website, UriKind.Absolute, out var uri) |
| 11 | 58 | | && (uri == null || (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))) |
| 2 | 59 | | { |
| 2 | 60 | | throw new ArgumentException( |
| 2 | 61 | | string.Format( |
| 2 | 62 | | CultureInfo.InvariantCulture, |
| 2 | 63 | | CommentResources.WebsiteInvalidUriErrorMessage, |
| 2 | 64 | | website), |
| 2 | 65 | | nameof(website)); |
| | 66 | | } |
| | 67 | |
|
| 9 | 68 | | return uri; |
| 9 | 69 | | } |
| | 70 | |
|
| | 71 | | private string GetValidEmail(string email) |
| 9 | 72 | | { |
| 9 | 73 | | if(!new EmailAddressAttribute().IsValid(email)) |
| 1 | 74 | | { |
| 1 | 75 | | throw new ArgumentException( |
| 1 | 76 | | string.Format( |
| 1 | 77 | | CultureInfo.InvariantCulture, |
| 1 | 78 | | CommentResources.FallbackCommitEmailInvalidErrorMessage, |
| 1 | 79 | | email), |
| 1 | 80 | | nameof(email)); |
| | 81 | | } |
| | 82 | |
|
| 8 | 83 | | return email; |
| 8 | 84 | | } |
| | 85 | | } |
| | 86 | |
|
| | 87 | | public class GitHubConfig |
| | 88 | | { |
| | 89 | | public string Token { get; set; } = ""; |
| | 90 | | public string PullRequestRepository { get; set; } = ""; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | public class TextAnalyticsConfig |
| | 94 | | { |
| | 95 | | public string SubscriptionKey { get; set; } = ""; |
| | 96 | | public string Region { get; set; } = ""; |
| | 97 | | public string Language { get; set; } = ""; |
| | 98 | | } |
| | 99 | | } |