2019-08-29 18:12:39 +09:00
|
|
|
|
using System.Linq;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-05-03 03:32:42 +09:00
|
|
|
|
using BTCPayServer.Rating;
|
2019-03-05 17:09:17 +09:00
|
|
|
|
using System.Threading;
|
2019-08-29 18:12:39 +09:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
2017-09-15 16:06:57 +09:00
|
|
|
|
namespace BTCPayServer.Services.Rates
|
2017-09-13 15:47:34 +09:00
|
|
|
|
{
|
2018-08-25 15:09:42 +09:00
|
|
|
|
public class BitpayRateProvider : IRateProvider, IHasExchangeName
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
2018-05-03 03:32:42 +09:00
|
|
|
|
public const string BitpayName = "bitpay";
|
2019-08-29 18:12:39 +09:00
|
|
|
|
private readonly HttpClient _httpClient;
|
|
|
|
|
public BitpayRateProvider(HttpClient httpClient)
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
2019-08-29 18:12:39 +09:00
|
|
|
|
_httpClient = httpClient ?? new HttpClient();
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
2018-08-25 15:09:42 +09:00
|
|
|
|
public string ExchangeName => BitpayName;
|
|
|
|
|
|
2019-03-05 17:09:17 +09:00
|
|
|
|
public async Task<ExchangeRates> GetRatesAsync(CancellationToken cancellationToken)
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
2019-08-29 18:12:39 +09:00
|
|
|
|
var response = await _httpClient.GetAsync("https://bitpay.com/rates", cancellationToken);
|
|
|
|
|
var jarray = (JArray)(await response.Content.ReadAsAsync<JObject>(cancellationToken))["data"];
|
|
|
|
|
return new ExchangeRates(jarray
|
|
|
|
|
.Children<JObject>()
|
|
|
|
|
.Select(jobj => new ExchangeRate(ExchangeName, new CurrencyPair("BTC", jobj["code"].Value<string>()), new BidAsk(jobj["rate"].Value<decimal>())))
|
|
|
|
|
.Where(o => o.CurrencyPair.Right != "BTC")
|
|
|
|
|
.ToArray());
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
}
|