public void TestAPI() { string result = APIHandler.Get("http://api.openweathermap.org/data/2.5/weather?q=" + "Dordrecht" + ",nl&APPID=" + this.APIKEY); dynamic data_ = JsonConvert.DeserializeObject(result); string city = data_.name; int timezone = data_.timezone; Assert.AreEqual("Dordrecht", city); Assert.AreEqual(7200, timezone); }
public override void DoCommand(Sentence sentence) { try { string[] words = sentence.getData().Split(" "); if (words.Length > 3) { throw new Exception(); } string city = words[1]; string command = words[2]; string workingCity = city.Substring(0, 1).ToUpper() + city.Substring(1).ToLower(); string data = APIHandler.Get("http://api.openweathermap.org/data/2.5/weather?q=" + city + ",nl&APPID=" + this.APIKEY); dynamic data_ = JsonConvert.DeserializeObject(data); switch (command) { case "temp": { this.Server.SendToUser(sentence.Sender, $"The temperature in {workingCity} is currently {Math.Round((double)(data_.main.temp - 273.15), 2)} Celcius."); break; } case "pressure": { this.Server.SendToUser(sentence.Sender, $"The pressure in {workingCity} is currently {data_.main.pressure} hPa."); break; } case "humidity": { this.Server.SendToUser(sentence.Sender, $"The humidity in {workingCity} is currently {data_.main.humidity} %."); break; } case "windspeed": { this.Server.SendToUser(sentence.Sender, $"The windspeed in {workingCity} is currently {data_.wind.speed} m/s."); break; } default: this.Server.SendToUser(sentence.Sender, $"The command \"{sentence.getData()}\" is not valid, type !help for a list of all my commands."); break; } } catch (Exception ex) { this.Server.SendToUser(sentence.Sender, $"The command \"{sentence.getData()}\" is not valid, type !help for a list of all my commands."); } }
public async Task SaveCurrenciesToDatabase(string currency) { HttpClientHandler handler = new HttpClientHandler(); HttpResponseMessage response = await APIHandler.Get("http://api.fixer.io/latest?base=" + currency, handler); if (response.IsSuccessStatusCode) { DeleteAllCurrencies(); Root rootCurrency = JsonConvert.DeserializeObject <Root>(await response.Content.ReadAsStringAsync()); foreach (var item in rootCurrency.rates) { Currency curr = new Currency(item.Key, item.Value); Console.WriteLine("Saving " + item.Key + " " + item.Value); App.Database.SaveCurrencies(curr); curr = null; } CurrencyManager.Init(currency); } }