コード例 #1
0
        protected Driver GetDriver(string url, int id)
        {
            Driver result;
            string key = id.ToString();

            if (!WebRequestCache <Driver> .ContainsKey(key) || WebRequestCache <Driver> .Expired(key) || !WebRequestCache <Driver> .UseCache)
            {
                HttpClient httpClient = CreateHttpClient();

                try
                {
                    var response = httpClient.GetStringAsync(url).Result;
                    result = JsonConvert.DeserializeObject <Driver>(response);
                    WebRequestCache <Driver> .Add(key, result);
                }
                catch (Exception)
                {
                    result = new Driver();
                }
            }
            else
            {
                result = WebRequestCache <Driver> .GetItem(key);

                ////Uncomment to fix pre-cache bug
                //if (result.Picture == null)
                //{
                //  result = WebRequestCache<Driver>.GetFullItemFromDisk(key);
                //}
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Do GetByVisitor
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        protected async Task <T> GetAsync <T>(string url)
            where T : new()
        {
            T result;

            if (!WebRequestCache <T> .ContainsKey(url) || WebRequestCache <T> .Expired(url))
            {
                HttpClient httpClient = CreateHttpClient();

                try
                {
                    var response = await httpClient.GetStringAsync(url);

                    result = JsonConvert.DeserializeObject <T>(response);
                    WebRequestCache <T> .Add(url, result);
                }
                catch (Exception)
                {
                    result = new T();
                }
            }
            else
            {
                string jsonResponse = WebRequestCache <T> .GetItem(url) as string;

                result = JsonConvert.DeserializeObject <T>(jsonResponse);
            }

            return(result);
        }
コード例 #3
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Driver d = EditDriverViewModel.CurrentDriver;

            WebRequestCache <Driver> .Add(d.DriverId.ToString(), d);

            if (BackingJsonFile == null && chkUpdateJson.IsChecked == true)
            {
                MessageBox.Show("No backing json file selected");
                return;
            }
            else if (chkUpdateJson.IsChecked == true)
            {
                WebRequestCache <Driver> .WriteCacheToFile(BackingJsonFile);
            }

            NavigationHelper.NavigateBack();
        }