So I got kind of a weird format returned from the api I'm calling it's the following:
[
[
1609065000000,
"22739.71000000",
"22795.73000000",
"22700.39000000",
"22779.08000000",
"38.81782800",
1609065899999,
"883167.94526884",
2261,
"18.58965200",
"423036.58181400",
"0"
]
]
public static class StringExtensions
{
public static IEnumerable<Candlestick> ToCandleStickList(this string jsonObject)
{
var candleStickList = new List<Candlestick>();
var list = JsonConvert.DeserializeObject<List<object[]>>(jsonObject);
foreach (var obj in list)
{
candleStickList.Add(new Candlestick
{
OpenTime = (long)obj[0],
Open = double.Parse((string)obj[1]),
High = double.Parse((string)obj[2]),
Low = double.Parse((string)obj[3]),
Close = double.Parse((string)obj[4]),
Volume = double.Parse((string)obj[5]),
CloseTime = (long)obj[6],
QuoteAssetVolume = double.Parse((string)obj[7]),
NumberOfTrades = (long)obj[8],
TakerBuyBaseAssetVolume = double.Parse((string)obj[9]),
TakerBuyQuoteAssetVolume = double.Parse((string)obj[10])
});
}
return candleStickList;
}
}
public class Candlestick
{
public long OpenTime { get; set; }
public double Open { get; set; }
public double High { get; set; }
public double Low { get; set; }
public double Close { get; set; }
public double Volume { get; set; }
public long CloseTime { get; set; }
public double QuoteAssetVolume { get; set; }
public long NumberOfTrades { get; set; }
public double TakerBuyBaseAssetVolume { get; set; }
public double TakerBuyQuoteAssetVolume { get; set; }
}
I use the static method as follows
public async Task<IEnumerable<Candlestick>> GetCandleSticks(string symbol, string interval)
{
var endpoint = $"api/v3/klines?symbol={symbol}&interval={interval}";
var response = await _httpClient.GetAsync(endpoint);
if (response.IsSuccessStatusCode)
{
var result = (await response.Content.ReadAsStringAsync()).ToCandleStickList();
return result;
}
throw new Exception($"");
}
Does anyone got any suggestions for better code? Thanks in advance!