using Microsoft.Extensions.Configuration; namespace Common.Configuration.DownloadClient; /// /// Configuration for a specific download client /// public sealed record ClientConfig { /// /// Unique identifier for this client /// public string Id { get; init; } = Guid.NewGuid().ToString("N"); /// /// Friendly name for this client /// public string Name { get; init; } = string.Empty; /// /// Type of download client /// public Common.Enums.DownloadClient Type { get; init; } = Common.Enums.DownloadClient.None; /// /// Host address for the download client /// public string Host { get; init; } = string.Empty; /// /// Port for the download client /// public int Port { get; init; } /// /// Username for authentication /// public string Username { get; init; } = string.Empty; /// /// Password for authentication /// public string Password { get; init; } = string.Empty; /// /// Default category to use /// public string Category { get; init; } = string.Empty; /// /// Path to download directory /// public string Path { get; init; } = string.Empty; /// /// Whether this client is enabled /// public bool Enabled { get; init; } = true; /// /// The base URL path component, used by clients like Transmission and Deluge /// [ConfigurationKeyName("URL_BASE")] public string UrlBase { get; init; } = string.Empty; /// /// Use HTTPS protocol /// public bool UseHttps { get; init; } = false; /// /// The computed full URL for the client /// public Uri Url => new Uri($"{(UseHttps ? "https" : "http")}://{Host}:{Port}/{UrlBase.TrimStart('/').TrimEnd('/')}"); /// /// Validates the configuration /// public void Validate() { if (string.IsNullOrWhiteSpace(Id)) { throw new InvalidOperationException("Client ID cannot be empty"); } if (string.IsNullOrWhiteSpace(Name)) { throw new InvalidOperationException($"Client name cannot be empty for client ID: {Id}"); } if (string.IsNullOrWhiteSpace(Host)) { throw new InvalidOperationException($"Host cannot be empty for client ID: {Id}"); } if (Port <= 0) { throw new InvalidOperationException($"Port must be greater than 0 for client ID: {Id}"); } if (Type == Common.Enums.DownloadClient.None) { throw new InvalidOperationException($"Client type must be specified for client ID: {Id}"); } } }