Beispiel #1
0
        private async static Task <ApiCacheEntry> FetchAPI <T>(string url)
        {
            ApiCacheEntry ret = new ApiCacheEntry();

            using (HttpRequestMessage hreq = new HttpRequestMessage(HttpMethod.Get, new Uri(url)))
                using (HttpClient hc = new HttpClient())
                    using (HttpResponseMessage hres = await hc.SendRequestAsync(hreq))
                    {
                        ret.ExpiryTime   = DateTime.Now.AddSeconds(SecondsBeforeStale);
                        ret.LastModified = hres.Headers.ContainsKey("Last-Modified") ?
                                           DateTime.Parse(hres.Headers["Last-Modified"]) :
                                           DateTime.Now;

                        using (IInputStream iis = await hres.Content.ReadAsInputStreamAsync())
                            using (Stream s = iis.AsStreamForRead())
                                using (StreamReader sr = new StreamReader(s))
                                    using (JsonReader jr = new JsonTextReader(sr))
                                    {
                                        JsonSerializer jsz = new JsonSerializer();

                                        ret.Value = jsz.Deserialize <T>(jr);
                                    }
                    }

            return(ret);
        }
Beispiel #2
0
        private async static Task <ApiCacheEntry> FetchAPI <T>(string url)
        {
            HttpWebRequest wreq = HttpWebRequest.CreateHttp(url);
            ApiCacheEntry  ret  = new ApiCacheEntry();

            using (HttpWebResponse wres = await wreq.GetResponseAsync() as HttpWebResponse)
            {
                ret.ExpiryTime   = DateTime.Now.AddSeconds(SecondsBeforeStale);
                ret.LastModified = wres.LastModified;

                using (Stream s = wres.GetResponseStream())
                    using (StreamReader sr = new StreamReader(s))
                        using (JsonReader jr = new JsonTextReader(sr))
                        {
                            JsonSerializer jsz = new JsonSerializer();

                            ret.Value = jsz.Deserialize <T>(jr);
                        }
            }

            return(ret);
        }