| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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<WeatherRoot> 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<WeatherRoot>(json);
- }
- }
- public async Task<WeatherRoot> 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<WeatherRoot>(json);
- }
- }
- public Task<WeatherForecastRoot> 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<WeatherForecastRoot> 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<WeatherForecastRoot>(json);
- }
- }
- public async Task<WeatherForecastRoot> 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<WeatherForecastRoot>(json);
- }
- }
- }
- }
|