using Beam.Models; namespace Beam.Downloaders; public sealed class UnitDownloaderOptionsBuilder { private DownloadTarget _target = DownloadTarget.URL; private HttpClient _client = new HttpClient(); private FailurePredicateOptionsBuilder _failureOptionsBuilder = new(); private FailurePredicateOptions? _failurePredicateOptionsOverride = null; private SkipPredicateOptionsBuilder _skipPredicateOptionsBuilder = new(); private SkipPredicateOptions? _skipPredicateOptionsOverride = null; private FragmentOptions? _fragmentOptions; private AsyncTransformer? _asyncTransformer; private string? _downloadFolder = null; private int _bufferSize = 80 * 1024; private bool _followRedirects = true; public UnitDownloaderOptionsBuilder WithTarget(DownloadTarget target) { _target = target; return this; } public UnitDownloaderOptionsBuilder WithFollowRedirects(bool followRedirects) { _followRedirects = followRedirects; return this; } public UnitDownloaderOptionsBuilder WithClient(HttpClient client) { _client = client ?? throw new System.ArgumentNullException(nameof(client)); return this; } public UnitDownloaderOptionsBuilder WithFailurePredicateOptions(FailurePredicateOptions? options) { _failurePredicateOptionsOverride = options; return this; } public UnitDownloaderOptionsBuilder WithFailurePredicates(System.Action> configure) { if (configure == null) throw new System.ArgumentNullException(nameof(configure)); configure(_failureOptionsBuilder); return this; } public UnitDownloaderOptionsBuilder WithFragmentOptions(FragmentOptions? options) { _fragmentOptions = options; return this; } public UnitDownloaderOptionsBuilder WithSkipPredicates(Action> configure) { if (configure == null) throw new ArgumentNullException(nameof(configure)); configure(_skipPredicateOptionsBuilder); return this; } public UnitDownloaderOptionsBuilder WithSkipPredicateOptions( SkipPredicateOptions skipPredicateOptions) { _skipPredicateOptionsOverride = skipPredicateOptions; return this; } public UnitDownloaderOptionsBuilder WithFragments(System.Action configure) { if (configure == null) throw new System.ArgumentNullException(nameof(configure)); var b = new FragmentOptionsBuilder(); configure(b); _fragmentOptions = b.Build(); return this; } public UnitDownloaderOptionsBuilder WithAsyncTransformer(AsyncTransformer transformer) { _asyncTransformer = transformer ?? throw new System.ArgumentNullException(nameof(transformer)); return this; } public UnitDownloaderOptionsBuilder WithDownloadFolder(string? downloadFolder) { _downloadFolder = downloadFolder; return this; } public UnitDownloaderOptionsBuilder WithBufferSize(int bytes) { if (bytes <= 0) throw new System.ArgumentOutOfRangeException(nameof(bytes)); _bufferSize = bytes; return this; } public UnitDownloaderOptions Build() { if (_asyncTransformer == null) throw new System.InvalidOperationException("AsyncTransformer must be provided."); _failurePredicateOptionsOverride ??= _failureOptionsBuilder.Build(); _skipPredicateOptionsOverride ??= _skipPredicateOptionsBuilder.Build(); return new UnitDownloaderOptions { Target = _target, Client = _client, FailurePredicateOptions = _failurePredicateOptionsOverride, SkipPredicateOptions = _skipPredicateOptionsOverride, FollowRedirects = _followRedirects, FragmentOptions = _fragmentOptions, AsyncTransformer = _asyncTransformer, DownloadFolder = _downloadFolder, BufferSize = _bufferSize }; } }