// ApiCallsBuilder.cs
using System.Net;
namespace Beam {
///
/// Fluent builder for .
///
public sealed class ApiCallsBuilder {
private readonly List _calls = [];
private int? _parallelism = 1; // default = sequential
public ApiCallsBuilder Add(ApiCall call) {
_calls.Add(call ?? throw new ArgumentNullException(nameof(call)));
return this;
}
public ApiCallsBuilder AddRange(IEnumerable calls) {
_calls.AddRange(calls ?? throw new ArgumentNullException(nameof(calls)));
return this;
}
/// Adds the same call times.
public ApiCallsBuilder Repeat(ApiCall prototype, int times) {
if (times < 1) throw new ArgumentOutOfRangeException(nameof(times));
for (var i = 0; i < times; i++)
Add(prototype);
return this;
}
/// Run with the specified degree of parallelism (≥ 1).
public ApiCallsBuilder UseParallel(int maxDegree) => SetDegree(Math.Max(1, maxDegree));
/// Run sequentially (same as UseParallel(1)).
public ApiCallsBuilder UseSequential() => SetDegree(1);
private ApiCallsBuilder SetDegree(int degree) {
_parallelism = degree;
return this;
}
public ApiCalls Build() {
if (_calls.Count == 0)
throw new InvalidOperationException("At least one ApiCall is required.");
return new ApiCalls(_calls, _parallelism);
}
}
}