Aggiunta classe SpoonacularApi
This commit is contained in:
50
PizzaExpress/API/SpoonacularApi.cs
Normal file
50
PizzaExpress/API/SpoonacularApi.cs
Normal 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; } = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,5 +17,8 @@
|
||||
"Url": "http://localhost:5000"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Spoonacular": {
|
||||
"ApiKey": "YOUR_SPOONACULAR_API_KEY"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,5 +18,8 @@
|
||||
"Url": "http://0.0.0.0:5000"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Spoonacular": {
|
||||
"ApiKey": "YOUR_SPOONACULAR_API_KEY"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user