From cb5bfa94f96da3a3921fb95a546bbaa1a075e7c6 Mon Sep 17 00:00:00 2001 From: Andrea Fiorencis Date: Thu, 26 Feb 2026 09:08:08 +0100 Subject: [PATCH] Aggiunta classe SpoonacularApi --- PizzaExpress/API/SpoonacularApi.cs | 50 +++++++++++++++++++++++ PizzaExpress/appsettings.Development.json | 3 ++ PizzaExpress/appsettings.json | 3 ++ 3 files changed, 56 insertions(+) create mode 100644 PizzaExpress/API/SpoonacularApi.cs diff --git a/PizzaExpress/API/SpoonacularApi.cs b/PizzaExpress/API/SpoonacularApi.cs new file mode 100644 index 0000000..466717e --- /dev/null +++ b/PizzaExpress/API/SpoonacularApi.cs @@ -0,0 +1,50 @@ +namespace PizzaExpress.API +{ + public class SpoonacularApi + { + private readonly HttpClient _httpClient; + private readonly string _apiKey; + + public SpoonacularApi(HttpClient httpClient, IConfiguration configuration) + { + _httpClient = httpClient; + + _apiKey = configuration["Spoonacular:ApiKey"]; + } + + public async Task> GetIngredientiDaPizzaAsync(string nomePizza) + { + var searchUrl = $"https://api.spoonacular.com/recipes/complexSearch?query={Uri.EscapeDataString(nomePizza)}&language=it&number=1&apiKey={_apiKey}"; + // Chiamata GET Ricerca + var searchResponse = await _httpClient.GetFromJsonAsync(searchUrl); + if (searchResponse == null || searchResponse.Results.Count == 0) + return new List(); + // Prendiamo l'ID della prima ricetta trovata + int recipeId = searchResponse.Results[0].Id; + + // Query per ottenere gli ingredienti della ricetta + var ingredientiUrl = $"https://api.spoonacular.com/recipes/{recipeId}/ingredientWidget.json?language=it&apiKey={_apiKey}"; + // Chiamata GET Ingredienti + var ingredientiResponse = await _httpClient.GetFromJsonAsync(ingredientiUrl); + + return ingredientiResponse.Ingredients.Select(i => i.Name).ToList(); + } + + private class SearchResponse + { + public List Results { get; set; } = new(); + } + private class SearchResult + { + public int Id { get; set; } + } + private class IngredientiResponse + { + public List Ingredients { get; set; } = new(); + } + private class Ingrediente + { + public string Name { get; set; } = ""; + } + } +} diff --git a/PizzaExpress/appsettings.Development.json b/PizzaExpress/appsettings.Development.json index d071b2d..ec3f22c 100644 --- a/PizzaExpress/appsettings.Development.json +++ b/PizzaExpress/appsettings.Development.json @@ -17,5 +17,8 @@ "Url": "http://localhost:5000" } } + }, + "Spoonacular": { + "ApiKey": "YOUR_SPOONACULAR_API_KEY" } } diff --git a/PizzaExpress/appsettings.json b/PizzaExpress/appsettings.json index 23f45d5..1f26b03 100644 --- a/PizzaExpress/appsettings.json +++ b/PizzaExpress/appsettings.json @@ -18,5 +18,8 @@ "Url": "http://0.0.0.0:5000" } } + }, + "Spoonacular": { + "ApiKey": "YOUR_SPOONACULAR_API_KEY" } }