public static List <Store> GetSampledUSStores(List <Store> allUSStores) { List <Store> sampledUSStores = new List <Store>(); var rand = new Random(); for (int sCount = 1; sCount <= 26; sCount++) { var index = rand.Next(allUSStores.Count); StorePreview preview = HomeController.GetPreview(allUSStores[index]); allUSStores[index].preview = preview; sampledUSStores.Add(allUSStores[index]); } return(sampledUSStores); }
public static StorePreview GetPreview(Store store) { StorePreview preview = null; if (null == store) { return(preview); } HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add( new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); string PREVIEW_API_PATH = "https://api.linkpreview.net?key=563b2c1eaf79c62ccabc5c1decfc62ad&q=" + store.URL; string previewData = ""; httpClient.BaseAddress = new Uri(PREVIEW_API_PATH); try { HttpResponseMessage response = httpClient.GetAsync(PREVIEW_API_PATH).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { previewData = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); } if (!previewData.Equals("")) { preview = Newtonsoft.Json.JsonConvert.DeserializeObject <StorePreview>(previewData); } } catch (Exception e) { Console.WriteLine(e.Message); } return(preview); }