btcpayserver/BTCPayServer.Rating/Providers/BitflyerRateProvider.cs

36 lines
1.4 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System;
2020-03-25 16:48:01 +01:00
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Rating;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Services.Rates
{
public class BitflyerRateProvider : IRateProvider
{
private readonly HttpClient _httpClient;
public BitflyerRateProvider(HttpClient httpClient)
{
_httpClient = httpClient ?? new HttpClient();
}
public RateSourceInfo RateSourceInfo => new RateSourceInfo("bitflyer", "Bitflyer", "https://api.bitflyer.com/v1/ticker");
2020-03-25 16:48:01 +01:00
public async Task<PairRate[]> GetRatesAsync(CancellationToken cancellationToken)
{
using var response = await _httpClient.GetAsync("https://api.bitflyer.jp/v1/ticker", cancellationToken);
2020-03-25 16:48:01 +01:00
var jobj = await response.Content.ReadAsAsync<JObject>(cancellationToken);
2020-03-25 17:21:16 +01:00
if (jobj.Property("error_message")?.Value?.Value<string>() is string err)
{
throw new Exception($"Error from bitflyer: {err}");
}
2020-03-25 16:48:01 +01:00
var bid = jobj.Property("best_bid").Value.Value<decimal>();
var ask = jobj.Property("best_ask").Value.Value<decimal>();
var rates = new PairRate[1];
rates[0] = new PairRate(CurrencyPair.Parse(jobj.Property("product_code").Value.Value<string>()), new BidAsk(bid, ask));
return rates;
}
}
}