Aggiunta classe SpoonacularApi

This commit is contained in:
2026-02-26 09:08:08 +01:00
parent 2912745a21
commit cb5bfa94f9
3 changed files with 56 additions and 0 deletions

View File

@@ -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<List<string>> 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<SearchResponse>(searchUrl);
if (searchResponse == null || searchResponse.Results.Count == 0)
return new List<string>();
// 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<IngredientiResponse>(ingredientiUrl);
return ingredientiResponse.Ingredients.Select(i => i.Name).ToList();
}
private class SearchResponse
{
public List<SearchResult> Results { get; set; } = new();
}
private class SearchResult
{
public int Id { get; set; }
}
private class IngredientiResponse
{
public List<Ingrediente> Ingredients { get; set; } = new();
}
private class Ingrediente
{
public string Name { get; set; } = "";
}
}
}

View File

@@ -17,5 +17,8 @@
"Url": "http://localhost:5000" "Url": "http://localhost:5000"
} }
} }
},
"Spoonacular": {
"ApiKey": "YOUR_SPOONACULAR_API_KEY"
} }
} }

View File

@@ -18,5 +18,8 @@
"Url": "http://0.0.0.0:5000" "Url": "http://0.0.0.0:5000"
} }
} }
},
"Spoonacular": {
"ApiKey": "YOUR_SPOONACULAR_API_KEY"
} }
} }