public async Task <CoinGeckoSimplePriceModel> getPrice(string vs_currencies = "rub", string ids = "bitcoin") { using (HttpClient client = new HttpClient()) { try { using (HttpResponseMessage response = await client.GetAsync(BaseUrl + "simple/price?vs_currencies=" + vs_currencies + "&ids=" + ids)) { response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(responseBody))) { DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(CoinGeckoSimplePriceModel)); CoinGeckoSimplePriceModel bsObj2 = (CoinGeckoSimplePriceModel)deserializer.ReadObject(ms); bsObj2.time = DateTime.Now; responseBody = null; return(bsObj2); } } } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); } } return(null); }
protected override void ScheduleBodyAsyncAction() { SetStatus("Запрос к API-CoinGecko (не-авторизованый)"); CoinGeckoSimplePriceModel answer = client.getPrice().Result; if (answer is null) { SetStatus("Ошибка получения данных с сервера API-CoinGecko", StatusTypes.ErrorStatus); SetStatus(null); return; } CurrentBtcRate = answer.bitcoin.rub; RatesBTC.Add(answer); }