using MyWeather.Models; using System.Net.Http; using System.Threading.Tasks; using static Newtonsoft.Json.JsonConvert; namespace MyWeather.Services { public enum Units { Imperial, Metric } public class WeatherService { const string WeatherCoordinatesUri = "http://api.openweathermap.org/data/2.5/weather?lat={0}&lon={1}&units={2}&appid=fc9f6c524fc093759cd28d41fda89a1b"; const string WeatherCityUri = "http://api.openweathermap.org/data/2.5/weather?q={0}&units={1}&appid=fc9f6c524fc093759cd28d41fda89a1b"; const string ForecaseUri = "http://api.openweathermap.org/data/2.5/forecast?id={0}&units={1}&appid=fc9f6c524fc093759cd28d41fda89a1b"; const string ForecaseCoordinatesUri = "http://api.openweathermap.org/data/2.5/forecast?lat={0}&lon={1}&units={2}&appid=6861b5f152084f4824be91d7b3dbce22"; public async Task GetWeather(double latitude, double longitude, Units units = Units.Imperial) { using (var client = new HttpClient()) { var url = string.Format(WeatherCoordinatesUri, latitude, longitude, units.ToString().ToLower()); var json = await client.GetStringAsync(url); if (string.IsNullOrWhiteSpace(json)) return null; return DeserializeObject(json); } } public async Task GetWeather(string city, Units units = Units.Imperial) { using (var client = new HttpClient()) { var url = string.Format(WeatherCityUri, city, units.ToString().ToLower()); var json = await client.GetStringAsync(url); if (string.IsNullOrWhiteSpace(json)) return null; return DeserializeObject(json); } } public Task GetForecast(WeatherRoot weather, Units units = Units.Imperial) { if (weather.CityId == 0) return GetForecast(weather.Coordinates.Latitude, weather.Coordinates.Longitude, units); return GetForecast(weather.CityId, units); } public async Task GetForecast(int id, Units units = Units.Imperial) { using (var client = new HttpClient()) { var url = string.Format(ForecaseUri, id, units.ToString().ToLower()); var json = await client.GetStringAsync(url); if (string.IsNullOrWhiteSpace(json)) return null; return DeserializeObject(json); } } public async Task GetForecast(double lat, double lon, Units units = Units.Imperial) { using (var client = new HttpClient()) { var url = string.Format(ForecaseCoordinatesUri, lat, lon, units.ToString().ToLower()); var json = await client.GetStringAsync(url); if (string.IsNullOrWhiteSpace(json)) return null; return DeserializeObject(json); } } } }