WeatherService.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using MyWeather.Models;
  2. using System.Net.Http;
  3. using System.Threading.Tasks;
  4. using static Newtonsoft.Json.JsonConvert;
  5. namespace MyWeather.Services
  6. {
  7. public enum Units
  8. {
  9. Imperial,
  10. Metric
  11. }
  12. public class WeatherService
  13. {
  14. const string WeatherCoordinatesUri = "http://api.openweathermap.org/data/2.5/weather?lat={0}&lon={1}&units={2}&appid=fc9f6c524fc093759cd28d41fda89a1b";
  15. const string WeatherCityUri = "http://api.openweathermap.org/data/2.5/weather?q={0}&units={1}&appid=fc9f6c524fc093759cd28d41fda89a1b";
  16. const string ForecaseUri = "http://api.openweathermap.org/data/2.5/forecast?id={0}&units={1}&appid=fc9f6c524fc093759cd28d41fda89a1b";
  17. const string ForecaseCoordinatesUri = "http://api.openweathermap.org/data/2.5/forecast?lat={0}&lon={1}&units={2}&appid=6861b5f152084f4824be91d7b3dbce22";
  18. public async Task<WeatherRoot> GetWeather(double latitude, double longitude, Units units = Units.Imperial)
  19. {
  20. using (var client = new HttpClient())
  21. {
  22. var url = string.Format(WeatherCoordinatesUri, latitude, longitude, units.ToString().ToLower());
  23. var json = await client.GetStringAsync(url);
  24. if (string.IsNullOrWhiteSpace(json))
  25. return null;
  26. return DeserializeObject<WeatherRoot>(json);
  27. }
  28. }
  29. public async Task<WeatherRoot> GetWeather(string city, Units units = Units.Imperial)
  30. {
  31. using (var client = new HttpClient())
  32. {
  33. var url = string.Format(WeatherCityUri, city, units.ToString().ToLower());
  34. var json = await client.GetStringAsync(url);
  35. if (string.IsNullOrWhiteSpace(json))
  36. return null;
  37. return DeserializeObject<WeatherRoot>(json);
  38. }
  39. }
  40. public Task<WeatherForecastRoot> GetForecast(WeatherRoot weather, Units units = Units.Imperial)
  41. {
  42. if (weather.CityId == 0)
  43. return GetForecast(weather.Coordinates.Latitude, weather.Coordinates.Longitude, units);
  44. return GetForecast(weather.CityId, units);
  45. }
  46. public async Task<WeatherForecastRoot> GetForecast(int id, Units units = Units.Imperial)
  47. {
  48. using (var client = new HttpClient())
  49. {
  50. var url = string.Format(ForecaseUri, id, units.ToString().ToLower());
  51. var json = await client.GetStringAsync(url);
  52. if (string.IsNullOrWhiteSpace(json))
  53. return null;
  54. return DeserializeObject<WeatherForecastRoot>(json);
  55. }
  56. }
  57. public async Task<WeatherForecastRoot> GetForecast(double lat, double lon, Units units = Units.Imperial)
  58. {
  59. using (var client = new HttpClient())
  60. {
  61. var url = string.Format(ForecaseCoordinatesUri, lat, lon, units.ToString().ToLower());
  62. var json = await client.GetStringAsync(url);
  63. if (string.IsNullOrWhiteSpace(json))
  64. return null;
  65. return DeserializeObject<WeatherForecastRoot>(json);
  66. }
  67. }
  68. }
  69. }