btcpayserver/BTCPayServer/Services/Rates/FallbackRateProvider.cs

37 lines
1,018 B
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2018-05-03 03:32:42 +09:00
using BTCPayServer.Rating;
namespace BTCPayServer.Services.Rates
{
public class FallbackRateProvider : IRateProvider
{
IRateProvider[] _Providers;
2018-05-03 03:32:42 +09:00
public bool Used { get; set; }
public FallbackRateProvider(IRateProvider[] providers)
{
if (providers == null)
throw new ArgumentNullException(nameof(providers));
_Providers = providers;
}
2018-05-03 03:32:42 +09:00
public async Task<ExchangeRates> GetRatesAsync()
{
2018-05-03 03:32:42 +09:00
Used = true;
foreach (var p in _Providers)
{
try
{
return await p.GetRatesAsync().ConfigureAwait(false);
}
2018-05-03 03:32:42 +09:00
catch(Exception ex) { Exceptions.Add(ex); }
}
2018-05-03 03:32:42 +09:00
return new ExchangeRates();
}
2018-05-03 03:32:42 +09:00
public List<Exception> Exceptions { get; set; } = new List<Exception>();
}
}