51 lines
1.9 KiB
C#
51 lines
1.9 KiB
C#
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; } = "";
|
|
}
|
|
}
|
|
}
|