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;
|
2019-08-29 11:12:39 +02:00
|
|
|
using Newtonsoft.Json.Linq;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
2017-09-15 09:06:57 +02:00
|
|
|
namespace BTCPayServer.Services.Rates
|
2017-09-13 08:47:34 +02:00
|
|
|
{
|
2020-01-17 10:11:05 +01:00
|
|
|
public class BitpayRateProvider : IRateProvider
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2019-08-29 11:12:39 +02:00
|
|
|
private readonly HttpClient _httpClient;
|
|
|
|
public BitpayRateProvider(HttpClient httpClient)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2019-08-29 11:12:39 +02:00
|
|
|
_httpClient = httpClient ?? new HttpClient();
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
2020-01-17 10:11:05 +01:00
|
|
|
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2019-08-29 11:12:39 +02:00
|
|
|
var response = await _httpClient.GetAsync("https://bitpay.com/rates", cancellationToken);
|
|
|
|
var jarray = (JArray)(await response.Content.ReadAsAsync<JObject>(cancellationToken))["data"];
|
2020-01-17 10:11:05 +01:00
|
|
|
return jarray
|
2019-08-29 11:12:39 +02:00
|
|
|
.Children<JObject>()
|
2020-01-17 10:11:05 +01:00
|
|
|
.Select(jobj => new PairRate(new CurrencyPair("BTC", jobj["code"].Value<string>()), new BidAsk(jobj["rate"].Value<decimal>())))
|
2019-08-29 11:12:39 +02:00
|
|
|
.Where(o => o.CurrencyPair.Right != "BTC")
|
2020-01-17 10:11:05 +01:00
|
|
|
.ToArray();
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
}
|