7ed05abdb8
- Introduced modularity by splitting Beam into new projects: Beam.Abstractions, Beam.Models, and Beam.Downloaders. - Refactored existing classes into appropriate namespaces and projects. - Replaced specific implementations with abstractions (e.g., SourceLinkBuilder to LinkBuilder, State to IState, etc.). - Updated interfaces: added ITemplate, IArticleData, IDownloadReport, and others for improved extensibility. - Removed deprecated classes like SourceLinkBuilder and StateChangerFactory. - Enhanced link handling in downloaders by refactoring to use `string` over `SourceLink`. - Consolidated shared logic under Beam.Abstractions.
49 lines
1.8 KiB
C#
49 lines
1.8 KiB
C#
// ApiCalls.cs
|
|
using System.Collections.Concurrent;
|
|
using System.Net;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Beam {
|
|
/// <summary>
|
|
/// Executes a batch of <see cref="ApiCall"/>s using either sequential or parallel strategy.
|
|
/// </summary>
|
|
public sealed class ApiCalls {
|
|
private readonly IReadOnlyList<ApiCall> _calls;
|
|
private readonly int _maxDegree;
|
|
|
|
internal ApiCalls(IReadOnlyList<ApiCall> calls, int? maxDegree) {
|
|
_calls = calls ?? throw new ArgumentNullException(nameof(calls));
|
|
_maxDegree = Math.Max(1, maxDegree ?? 1);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs every call and returns the ordered list of <see cref="ApiResponse"/>s.
|
|
/// </summary>
|
|
public async Task<IReadOnlyList<ApiResponse>> ExecuteAsync(
|
|
ILogger<ApiResponse>? logger = null,
|
|
(int @try, int max)? tries = null,
|
|
CancellationToken ct = default) {
|
|
if (_maxDegree == 1) {
|
|
// sequential
|
|
var sequential = new List<ApiResponse>(_calls.Count);
|
|
foreach (var call in _calls)
|
|
sequential.Add(await call.GetResponse(logger, tries, ct));
|
|
return sequential;
|
|
}
|
|
|
|
// parallel
|
|
var bag = new ConcurrentBag<(int idx, ApiResponse res)>();
|
|
await Parallel.ForEachAsync(
|
|
_calls.Select((c, i) => (call: c, idx: i)),
|
|
new ParallelOptions { MaxDegreeOfParallelism = _maxDegree, CancellationToken = ct },
|
|
async (item, token) => {
|
|
var response = await item.call.GetResponse(logger, tries, token);
|
|
bag.Add((item.idx, response));
|
|
});
|
|
|
|
// keep original ordering
|
|
return bag.OrderBy(x => x.idx).Select(x => x.res).ToList();
|
|
}
|
|
}
|
|
}
|