Esempio n. 1
0
        private string getRecipeInformation(RecipeObject recipe)
        {
            string ingredients = "Ingredients: <br><br>";

            for (int i = 0; i < recipe.recipes[0].extendedIngredients.Length; i++)
            {
                var currentIngredient = recipe.recipes[0].extendedIngredients[i];
                ingredients = ingredients + currentIngredient.measures.us.amount + " " + currentIngredient.measures.us.unitShort + " " + currentIngredient.name + "<br>";
            }

            string instructions = "<br> Instructions: <br><br>";

            for (int j = 0; j < recipe.recipes[0].analyzedInstructions.Length; j++)
            {
                var    currentInstruction = recipe.recipes[0].analyzedInstructions[j];
                string steps = "";
                for (int k = 0; k < recipe.recipes[0].analyzedInstructions[j].steps.Length; k++)
                {
                    var currentStep = currentInstruction.steps[k];
                    steps = steps + currentStep.number.ToString() + ":   " + currentStep.step + "<br>";
                }
                instructions = instructions + currentInstruction.name + steps;
            }
            recipeName = recipe.recipes[0].title;
            return(ingredients + instructions);
        }
Esempio n. 2
0
        public string getRandomRecipe()
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://api.spoonacular.com/recipes/");
                string recipe_key = ConfigurationManager.AppSettings["RecipeAPI"];

                for (int i = 0; i < 4; i++)
                {
                    HttpResponseMessage response = client.GetAsync("random?number=1&apiKey=" + recipe_key).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        string       result = response.Content.ReadAsStringAsync().Result;
                        RecipeObject recipe = JsonConvert.DeserializeObject <RecipeObject>(result);

                        return(getRecipeInformation(recipe));
                    }
                    else
                    {
                        Debug.WriteLine("Unsuccsessful request. Status code: " + response.StatusCode);
                        Thread.Sleep(2000 * i);
                    }
                }
            }
            return(null);
        }
Esempio n. 3
0
        public string getRecipeBytype(string typeOfFood)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://api.spoonacular.com/recipes/");
                string recipe_key            = ConfigurationManager.AppSettings["RecipeAPI"];
                HttpResponseMessage response = client.GetAsync("search?query=" + typeOfFood + "&number=1&apiKey=" + recipe_key).Result;
                if (response.IsSuccessStatusCode)
                {
                    Debug.WriteLine("typeOfFood " + typeOfFood);
                    string     result = response.Content.ReadAsStringAsync().Result;
                    Rootobject recipe = JsonConvert.DeserializeObject <Rootobject>(result);

                    string id = recipe.results[0].id.ToString();
                    HttpResponseMessage infoResponse = client.GetAsync(id + "/information?&apiKey=" + recipe_key).Result;

                    if (infoResponse.IsSuccessStatusCode)
                    {
                        string infoResult = infoResponse.Content.ReadAsStringAsync().Result;
                        Recipe recipeInfo = JsonConvert.DeserializeObject <Recipe>(infoResult);

                        RecipeObject newRecipe = new RecipeObject();
                        newRecipe.recipes    = new Recipe[1];
                        newRecipe.recipes[0] = recipeInfo;
                        return(getRecipeInformation(newRecipe));
                    }
                    else
                    {
                        Debug.WriteLine("Unsuccsessful request. ");
                        return(null);
                    }
                }
                else
                {
                    Debug.WriteLine("Unsuccsessful request. ");
                    return(null);
                }
            }
        }