WeatherHandler.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using MediaBrowser.Common.Net.Handlers;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Model.Weather;
  4. using System;
  5. using System.ComponentModel.Composition;
  6. using System.Net;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Api.HttpHandlers
  9. {
  10. [Export(typeof(BaseHandler))]
  11. class WeatherHandler : BaseSerializationHandler<WeatherInfo>
  12. {
  13. public override bool HandlesRequest(HttpListenerRequest request)
  14. {
  15. return ApiService.IsApiUrlMatch("weather", request);
  16. }
  17. protected override Task<WeatherInfo> GetObjectToSerialize()
  18. {
  19. // If a specific zip code was requested on the query string, use that. Otherwise use the value from configuration
  20. string zipCode = QueryString["zipcode"];
  21. if (string.IsNullOrWhiteSpace(zipCode))
  22. {
  23. zipCode = Kernel.Instance.Configuration.WeatherZipCode;
  24. }
  25. return Kernel.Instance.WeatherClient.GetWeatherInfoAsync(zipCode);
  26. }
  27. /// <summary>
  28. /// Tell the client to cache the weather info for 15 minutes
  29. /// </summary>
  30. public override TimeSpan CacheDuration
  31. {
  32. get
  33. {
  34. return TimeSpan.FromMinutes(15);
  35. }
  36. }
  37. }
  38. }