Last active
February 7, 2019 11:47
-
-
Save icebeam7/1960b73e4c40287b50be4881bdf61b65 to your computer and use it in GitHub Desktop.
WeatherBotv4 - WeatherService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using System.Net.Http; | |
using Newtonsoft.Json; | |
using WeatherBotv4.Helpers; | |
using WeatherBotv4.Models; | |
namespace WeatherBotv4.Services | |
{ | |
public static class WeatherService | |
{ | |
public static async Task<WeatherModel> GetWeather(string city) | |
{ | |
var query = $"{Constants.OpenWeatherMapURL}?q={city}&appid={Constants.OpenWeatherMapKey}"; | |
using (var client = new HttpClient()) | |
{ | |
var getWeather = await client.GetAsync(query); | |
if (getWeather.IsSuccessStatusCode) | |
{ | |
var json = await getWeather.Content.ReadAsStringAsync(); | |
var weather = JsonConvert.DeserializeObject<WeatherModel>(json); | |
weather.main.temp = weather.main.temp - 273.15; | |
return weather; | |
} | |
} | |
return default(WeatherModel); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment