2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2017-12-08 15:04:47 +09:00
|
|
|
using System.Collections.Generic;
|
2019-03-05 17:09:17 +09:00
|
|
|
using System.Threading;
|
2017-12-08 15:04:47 +09:00
|
|
|
using System.Threading.Tasks;
|
2018-05-03 03:32:42 +09:00
|
|
|
using BTCPayServer.Rating;
|
2017-12-08 15:04:47 +09:00
|
|
|
|
|
|
|
namespace BTCPayServer.Services.Rates
|
|
|
|
{
|
|
|
|
public class FallbackRateProvider : IRateProvider
|
|
|
|
{
|
2020-06-28 22:07:48 -05:00
|
|
|
readonly IRateProvider[] _Providers;
|
2017-12-08 15:04:47 +09:00
|
|
|
public FallbackRateProvider(IRateProvider[] providers)
|
|
|
|
{
|
|
|
|
if (providers == null)
|
|
|
|
throw new ArgumentNullException(nameof(providers));
|
|
|
|
_Providers = providers;
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:11:05 +09:00
|
|
|
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
|
2017-12-08 15:04:47 +09:00
|
|
|
{
|
|
|
|
foreach (var p in _Providers)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-03-05 17:09:17 +09:00
|
|
|
return await p.GetRatesAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch when (cancellationToken.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
throw;
|
2017-12-08 15:04:47 +09:00
|
|
|
}
|
2020-06-28 17:55:27 +09:00
|
|
|
catch (Exception ex) { Exceptions.Add(ex); }
|
2017-12-08 15:04:47 +09:00
|
|
|
}
|
2020-01-17 18:11:05 +09:00
|
|
|
return Array.Empty<PairRate>();
|
2017-12-08 15:04:47 +09:00
|
|
|
}
|
2018-05-03 03:32:42 +09:00
|
|
|
|
|
|
|
public List<Exception> Exceptions { get; set; } = new List<Exception>();
|
2017-12-08 15:04:47 +09:00
|
|
|
}
|
|
|
|
}
|