2020-06-28 17:55:27 +09:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading;
|
2017-09-13 15:47:34 +09:00
|
|
|
using System.Threading.Tasks;
|
2018-05-03 03:32:42 +09:00
|
|
|
using BTCPayServer.Rating;
|
2019-08-29 18:12:39 +09:00
|
|
|
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
|
|
|
{
|
2020-01-17 18:11:05 +09:00
|
|
|
public class BitpayRateProvider : IRateProvider
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
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
|
|
|
|
2023-01-30 09:46:12 +09:00
|
|
|
public RateSourceInfo RateSourceInfo => new("bitpay", "Bitpay", "https://bitpay.com/rates");
|
|
|
|
|
2020-01-17 18:11:05 +09:00
|
|
|
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2023-12-19 11:44:10 +09:00
|
|
|
using var response = await _httpClient.GetAsync("https://bitpay.com/rates", cancellationToken);
|
|
|
|
if (response.Content.Headers.ContentType?.MediaType is not "application/json")
|
|
|
|
throw new HttpRequestException($"Unexpected content type when querying currency rates from Bitpay ({response.Content.Headers.ContentType?.MediaType})");
|
2019-08-29 18:12:39 +09:00
|
|
|
var jarray = (JArray)(await response.Content.ReadAsAsync<JObject>(cancellationToken))["data"];
|
2020-01-17 18:11:05 +09:00
|
|
|
return jarray
|
2019-08-29 18:12:39 +09:00
|
|
|
.Children<JObject>()
|
2020-01-17 18:11:05 +09:00
|
|
|
.Select(jobj => new PairRate(new CurrencyPair("BTC", jobj["code"].Value<string>()), new BidAsk(jobj["rate"].Value<decimal>())))
|
2019-08-29 18:12:39 +09:00
|
|
|
.Where(o => o.CurrencyPair.Right != "BTC")
|
2020-01-17 18:11:05 +09:00
|
|
|
.ToArray();
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
}
|