public string GetReceipeIngredients(int id) { IngredientsRoot ingredients = new IngredientsRoot(); using (var client = new HttpClient()) { client.BaseAddress = new Uri(baseUrl); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage responseIngredients = client.GetAsync($"recipes/{id}//ingredientWidget.json?apiKey=f0bc1cb5a0da48e1b8282cba70ca05ff").Result; if (responseIngredients.IsSuccessStatusCode) { var response = responseIngredients.Content.ReadAsStringAsync().Result; ingredients = JsonConvert.DeserializeObject <IngredientsRoot>(response); } client.Dispose(); string result = ""; if (ingredients != null && ingredients.ingredients != null) { foreach (Ingredient ingredient in ingredients.ingredients) { result += ingredient.amount.metric.value + " " + ingredient.amount.metric.unit + " of " + ingredient.name + "<br />"; } } return(result); } }
public IngredientsAndInstructions GetReceipeById() { int id = Convert.ToInt32(Request.QueryString["recipeID"]); InstructionsRoot instructions = new InstructionsRoot(); IngredientsRoot ingredients = new IngredientsRoot(); using (var client = new HttpClient()) { client.BaseAddress = new Uri(baseUrl); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage responseInstructions = client.GetAsync($"recipes/{id}/analyzedInstructions?apiKey=f0bc1cb5a0da48e1b8282cba70ca05ff").Result; HttpResponseMessage responseIngredients = client.GetAsync($"recipes/{id}//ingredientWidget.json?apiKey=f0bc1cb5a0da48e1b8282cba70ca05ff").Result; if (responseInstructions.IsSuccessStatusCode) { var response = responseInstructions.Content.ReadAsStringAsync().Result; instructions = JsonConvert.DeserializeObject <InstructionsRoot>(response); } if (responseIngredients.IsSuccessStatusCode) { var response = responseIngredients.Content.ReadAsStringAsync().Result; ingredients = JsonConvert.DeserializeObject <IngredientsRoot>(response); } client.Dispose(); IngredientsAndInstructions result = new IngredientsAndInstructions(); if (instructions != null && instructions.steps != null) { foreach (Step step in instructions.steps) { result.instructions.Add(step.step); } } if (ingredients != null && ingredients.ingredients != null) { foreach (Ingredient ingredient in ingredients.ingredients) { result.ingredients.Add(ingredient.amount.metric.value + ingredient.amount.metric.unit + " of " + ingredient.name); } } return(result); } }