aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/HttpHandlers/WeatherHandler.cs
blob: 93d4c8877eb94abab8bd7e2f838b17dd873e0db1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller;
using MediaBrowser.Model.Weather;
using System;
using System.ComponentModel.Composition;
using System.Net;
using System.Threading.Tasks;

namespace MediaBrowser.Api.HttpHandlers
{
    [Export(typeof(BaseHandler))]
    class WeatherHandler : BaseSerializationHandler<WeatherInfo>
    {
        public override bool HandlesRequest(HttpListenerRequest request)
        {
            return ApiService.IsApiUrlMatch("weather", request);
        }
        
        protected override Task<WeatherInfo> GetObjectToSerialize()
        {
            // If a specific zip code was requested on the query string, use that. Otherwise use the value from configuration

            string zipCode = QueryString["zipcode"];

            if (string.IsNullOrWhiteSpace(zipCode))
            {
                zipCode = Kernel.Instance.Configuration.WeatherZipCode;
            }

            return Kernel.Instance.WeatherClient.GetWeatherInfoAsync(zipCode);
        }

        /// <summary>
        /// Tell the client to cache the weather info for 15 minutes
        /// </summary>
        public override TimeSpan CacheDuration
        {
            get
            {
                return TimeSpan.FromMinutes(15);
            }
        }
    }
}