/// <summary> /// If a searchterms yields no results in the databae then this methos is used to check for results in the salling api(currently disabled because of technical issus on sallings side) /// </summary> /// <param name="Searchterm"></param> /// <returns>returns bool based on wheter or not any products are found to match the serchterm</returns> private bool CheckIngredientsInApi(string Searchterm) { System.Threading.Thread.Sleep(4000); BearerAccessToken bearerAccessToken = new BearerAccessToken("fc5aefca-c70f-4e59-aaaa-1c4603607df8"); SallingAPILink linkMaker = new SallingAPILink(); SallingAPIProductSuggestions productSuggestions = new SallingAPIProductSuggestions(); string apiLink = linkMaker.GetProductAPILink(Searchterm); OpenHttp <SallingAPIProductSuggestions> openHttp; try { openHttp = new OpenHttp <SallingAPIProductSuggestions>(apiLink, bearerAccessToken.GetBearerToken()); productSuggestions = openHttp.ReadAndParseAPISingle(); if (productSuggestions.Suggestions != null) { foreach (var p in productSuggestions.Suggestions) { dc.Product.Add(new Product(p.title, p.id, p.prod_id, p.price, p.description, p.link, p.img)); } } } catch (System.Net.WebException) { Console.WriteLine("Exception found is being handled"); System.Threading.Thread.Sleep(30000); Console.WriteLine("Exception resolved"); return(false); } if (productSuggestions.Suggestions != null) { return(productSuggestions.Suggestions.Count != 0 ? true : false); } else { return(false); } }
public List <Product> SearchForProducts(String searchterm) { BearerAccessToken bearerAccessToken = new BearerAccessToken("fc5aefca-c70f-4e59-aaaa-1c4603607df8"); SallingAPILink linkMaker = new SallingAPILink(); SallingAPIProductSuggestions productSuggestions = new SallingAPIProductSuggestions(); // Creates a link to the Salling api with the searchterm as input string apiLink = linkMaker.GetProductAPILink(searchterm); OpenHttp <SallingAPIProductSuggestions> openHttp; List <Product> returnList = new List <Product>(); try { // Opens the Salling API openHttp = new OpenHttp <SallingAPIProductSuggestions>(apiLink, bearerAccessToken.GetBearerToken()); // Parsese the returned Json string into a list of SallingAPIProduct productSuggestions = openHttp.ReadAndParseAPISingle(); if (productSuggestions.Suggestions.Count != 0) { foreach (SallingAPIProduct products in productSuggestions.Suggestions) { // Adds all SallingAPIProduct to the returnList and converts them all to the class Product returnList.Add(new Product(products.id, products.title, products.description, products.price, products.img, "Bilka")); } } } catch (WebException e) { // If there is an error while loading the Salling api. Console.WriteLine("This program is expected to throw WebException on successful run." + "\n\nException Message :" + e.Message); } return(returnList); }