| | | 1 | | namespace TeleFlow.Telegram.Internal.Options; |
| | | 2 | | |
| | | 3 | | internal static class TelegramClientOptionsValidator |
| | | 4 | | { |
| | | 5 | | public static void Validate(TelegramClientOptions options) |
| | | 6 | | { |
| | 484 | 7 | | ArgumentNullException.ThrowIfNull(options); |
| | | 8 | | |
| | 484 | 9 | | if (string.IsNullOrWhiteSpace(options.Token)) |
| | | 10 | | { |
| | 0 | 11 | | throw new InvalidOperationException("Telegram bot token must be configured."); |
| | | 12 | | } |
| | | 13 | | |
| | 484 | 14 | | if (string.IsNullOrWhiteSpace(options.BaseUrl)) |
| | | 15 | | { |
| | 0 | 16 | | throw new InvalidOperationException("Telegram base URL must be configured."); |
| | | 17 | | } |
| | | 18 | | |
| | 484 | 19 | | if (!Uri.TryCreate(options.BaseUrl, UriKind.Absolute, out _)) |
| | | 20 | | { |
| | 0 | 21 | | throw new InvalidOperationException("Telegram base URL must be an absolute URI."); |
| | | 22 | | } |
| | | 23 | | |
| | 484 | 24 | | if (!Enum.IsDefined(options.Environment)) |
| | | 25 | | { |
| | 1 | 26 | | throw new InvalidOperationException( |
| | 1 | 27 | | $"Telegram Bot API environment '{options.Environment}' is not supported."); |
| | | 28 | | } |
| | | 29 | | |
| | 483 | 30 | | if (!TelegramBotUsernameNormalizer.TryNormalize(options.BotUsername, out _, out var botUsernameError)) |
| | | 31 | | { |
| | 5 | 32 | | throw new InvalidOperationException(botUsernameError); |
| | | 33 | | } |
| | | 34 | | |
| | 478 | 35 | | if (options.Defaults is null) |
| | | 36 | | { |
| | 0 | 37 | | throw new InvalidOperationException("Telegram bot defaults must be configured."); |
| | | 38 | | } |
| | | 39 | | |
| | 478 | 40 | | ValidateRetryAfter(options.RetryAfter); |
| | 474 | 41 | | } |
| | | 42 | | |
| | | 43 | | private static void ValidateRetryAfter(TelegramRetryAfterPolicy? policy) |
| | | 44 | | { |
| | 478 | 45 | | if (policy is null) |
| | | 46 | | { |
| | 1 | 47 | | throw new InvalidOperationException("Telegram retry-after policy must be configured."); |
| | | 48 | | } |
| | | 49 | | |
| | 477 | 50 | | if (policy.MaxRetries < 0) |
| | | 51 | | { |
| | 1 | 52 | | throw new InvalidOperationException("Telegram retry-after maximum retry count must not be negative."); |
| | | 53 | | } |
| | | 54 | | |
| | 476 | 55 | | if (policy.MaxDelay <= TimeSpan.Zero) |
| | | 56 | | { |
| | 2 | 57 | | throw new InvalidOperationException("Telegram retry-after maximum retry delay must be greater than zero."); |
| | | 58 | | } |
| | 474 | 59 | | } |
| | | 60 | | } |