Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Last active February 7, 2019 11:47
Show Gist options
  • Save icebeam7/1960b73e4c40287b50be4881bdf61b65 to your computer and use it in GitHub Desktop.
Save icebeam7/1960b73e4c40287b50be4881bdf61b65 to your computer and use it in GitHub Desktop.
WeatherBotv4 - WeatherService.cs
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