using Microsoft.Extensions.Logging; namespace MxGateway.Client; /// /// Configures the gRPC channel used by the .NET MXAccess Gateway client. /// public sealed class MxGatewayClientOptions { public required Uri Endpoint { get; init; } public required string ApiKey { get; init; } public bool UseTls { get; init; } public string? CaCertificatePath { get; init; } public string? ServerNameOverride { get; init; } public TimeSpan ConnectTimeout { get; init; } = TimeSpan.FromSeconds(10); public TimeSpan DefaultCallTimeout { get; init; } = TimeSpan.FromSeconds(30); public TimeSpan? StreamTimeout { get; init; } public int MaxGrpcMessageBytes { get; init; } = 16 * 1024 * 1024; public MxGatewayClientRetryOptions Retry { get; init; } = new(); public ILoggerFactory? LoggerFactory { get; init; } public void Validate() { ArgumentNullException.ThrowIfNull(Endpoint); if (!Endpoint.IsAbsoluteUri) { throw new ArgumentException( "The gateway endpoint must be an absolute URI.", nameof(Endpoint)); } if (string.IsNullOrWhiteSpace(ApiKey)) { throw new ArgumentException( "The gateway API key must not be empty.", nameof(ApiKey)); } if (ConnectTimeout <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException( nameof(ConnectTimeout), "The connect timeout must be greater than zero."); } if (DefaultCallTimeout <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException( nameof(DefaultCallTimeout), "The default call timeout must be greater than zero."); } if (StreamTimeout is not null && StreamTimeout <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException( nameof(StreamTimeout), "The stream timeout must be greater than zero when configured."); } if (MaxGrpcMessageBytes <= 0) { throw new ArgumentOutOfRangeException( nameof(MaxGrpcMessageBytes), "The maximum gRPC message size must be greater than zero."); } if (UseTls && Endpoint.Scheme != Uri.UriSchemeHttps) { throw new ArgumentException( "UseTls requires an https gateway endpoint.", nameof(Endpoint)); } if (!UseTls && Endpoint.Scheme == Uri.UriSchemeHttps) { throw new ArgumentException( "An https gateway endpoint requires UseTls.", nameof(Endpoint)); } Retry.Validate(); } }