btcpayserver/BTCPayServer.Rating/Providers/BitpayRateProvider.cs

30 lines
1.0 KiB
C#
Raw Permalink Normal View History

2020-06-28 10:55:27 +02:00
using System.Linq;
using System.Net.Http;
using System.Threading;
2017-09-13 08:47:34 +02:00
using System.Threading.Tasks;
2018-05-02 20:32:42 +02:00
using BTCPayServer.Rating;
using Newtonsoft.Json.Linq;
2017-09-13 08:47:34 +02:00
namespace BTCPayServer.Services.Rates
2017-09-13 08:47:34 +02:00
{
public class BitpayRateProvider : IRateProvider
{
private readonly HttpClient _httpClient;
public BitpayRateProvider(HttpClient httpClient)
{
_httpClient = httpClient ?? new HttpClient();
}
2017-09-13 08:47:34 +02:00
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
{
var response = await _httpClient.GetAsync("https://bitpay.com/rates", cancellationToken);
var jarray = (JArray)(await response.Content.ReadAsAsync<JObject>(cancellationToken))["data"];
return jarray
.Children<JObject>()
.Select(jobj => new PairRate(new CurrencyPair("BTC", jobj["code"].Value<string>()), new BidAsk(jobj["rate"].Value<decimal>())))
.Where(o => o.CurrencyPair.Right != "BTC")
.ToArray();
}
}
2017-09-13 08:47:34 +02:00
}