public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string name = req.Query["name"]; var servings = GetServings(req.Query["servings"]); if (name == null) { return(new BadRequestObjectResult("Please pass a name on the query string or in the request body")); } var comparer = new StringContainsComparer(); var cocktails = CocktailList.GetCocktails() .Where(c => CompareNames(c, name, comparer)) .Select(cocktail1 => cocktail1.ScaleIt(servings)); var json = JsonConvert.SerializeObject(cocktails, new StringEnumConverter()); return(new OkObjectResult(json)); }
public async Task <CocktailList> GetRandomBeverageAsync() { string url = Uri(); CocktailList drinks = null; try { // Return cached data only if there's no Internet and Cache is not expired if (Connectivity.NetworkAccess != NetworkAccess.Internet && !Barrel.Current.IsExpired(key: url)) { return(Barrel.Current.Get <CocktailList>(key: url)); //// return Barrel.Current.Get<IEnumerable<CocktailList>>(key: url); } using (var client = new HttpClient()) { var json = await client.GetStringAsync(url); drinks = Utf8Json.JsonSerializer.Deserialize <CocktailList>(json); } Barrel.Current.Add(key: url, data: drinks, expireIn: TimeSpan.FromDays(2)); } catch (Exception ex) { _log.Error($"Failed to get beverages.{Environment.NewLine}{ex.Message}"); } return(drinks); }
public async Task <IActionResult> GetIngredientSearch([FromRoute] string ingredient) { var cocktailList = new CocktailList(); cocktailList.Cocktails = new List <Cocktail>(); string json = await this._client.Get($"https://www.thecocktaildb.com/api/json/v1/1/filter.php?i={ingredient}"); if (!string.IsNullOrWhiteSpace(json)) { JToken jToken = JsonConvert.DeserializeObject <JToken>(json); var list = jToken["drinks"].ToObject <JArray>(); List <Task <Cocktail> > drinkDataList = new List <Task <Cocktail> >(); foreach (var item in list) { drinkDataList.Add(this.GetCocktail(item)); } var result = await Task.WhenAll <Cocktail>(drinkDataList); //filter the list and get the ones which have the ingredient cocktailList.Cocktails = result .OrderBy(item => item.Ingredients.Count()).ToList(); if (cocktailList.Cocktails.Any()) { //calculate the meta int count = cocktailList.Cocktails.Count(); int firstId = cocktailList.Cocktails.Min(item => item.Id); var lastId = cocktailList.Cocktails.Max(item => item.Id); int medianIndex = count / 2; int medianNumber = 0; if (count % 2 == 0) { //take round down for the median number medianNumber = ( cocktailList.Cocktails[medianIndex].Ingredients.Count() + cocktailList.Cocktails[medianIndex - 1].Ingredients.Count() ) / 2; } else { medianNumber = cocktailList.Cocktails[medianIndex].Ingredients.Count(); } cocktailList.meta = new ListMeta { count = count, firstId = firstId, lastId = lastId, medianIngredientCount = medianNumber }; } } return(Ok(cocktailList)); }
public async Task <IActionResult> GetIngredientSearch([FromRoute] string ingredient) { var cocktailList = new CocktailList(); // TODO - Search the CocktailDB for cocktails with the ingredient given, and return the cocktails // https://www.thecocktaildb.com/api/json/v1/1/filter.php?i=Gin // You will need to populate the cocktail details from // https://www.thecocktaildb.com/api/json/v1/1/lookup.php?i=11007 // The calculate and fill in the meta object return(Ok(cocktailList)); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); var qsIngredients = (string)req.Query["ingredients"]; var ingredients = qsIngredients.Split(','); var cocktails = CocktailList .GetCocktails() .Where(c => CheckForIngredient(c, ingredients)) .Select(c => c.Name); //string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); //dynamic data = JsonConvert.DeserializeObject(requestBody); //name = name ?? data?.name; var json = JsonConvert.SerializeObject(cocktails, new StringEnumConverter()); return(new OkObjectResult(json)); }