Finished restructuring and changed way of querying the API

This commit is contained in:
2022-07-13 04:25:40 +02:00
parent f7455019c3
commit c5014c4a11
6 changed files with 106 additions and 46 deletions

View File

@@ -1,40 +1,61 @@
package spoonaccular;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.cdimascio.dotenv.Dotenv;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Component;
import spoonaccular.models.recipe_by_ingredient.ExtendedRecipeByIngredient;
import spoonaccular.models.recipe_by_ingredient.RecipeByIngredient;
import spoonaccular.models.recipe_information.Recipe;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class RecipeInformation {
private final Dotenv dotenv;
private final OkHttpClient client;
public RecipeInformation(){
dotenv = Dotenv.load();
client = new OkHttpClient();
}
public Recipe getRecipeFromId(int id) throws IOException {
String urlString = dotenv.get("SPOONACCULAR_API_URL") + "/recipes/" + id + "/information?apiKey="+dotenv.get("SPOONACCULAR_API_KEY")+"&includeNutrition=false";
URL url = new URL(urlString);
return new ObjectMapper().readValue(url, Recipe.class);
public List<Recipe> getRecipeFromIds(List<Integer> ids) throws IOException {
String idsString = ids.stream().map(String::valueOf)
.collect(Collectors.joining(","));
return new ObjectMapper().readValue(queryInformationBulk(idsString).body().string(), new TypeReference<>(){});
}
public ExtendedRecipeByIngredient getExtendedRecipeFromId(int id) throws IOException {
String urlString = dotenv.get("SPOONACCULAR_API_URL") + "/recipes/" + id + "/information?apiKey="+dotenv.get("SPOONACCULAR_API_KEY")+"&includeNutrition=false";
URL url = new URL(urlString);
return new ObjectMapper().readValue(url, ExtendedRecipeByIngredient.class);
public List<ExtendedRecipeByIngredient> getExtendedRecipeFromIds(List<Integer> ids) throws IOException {
String idsString = ids.stream().map(String::valueOf)
.collect(Collectors.joining(","));
return new ObjectMapper().readValue(queryInformationBulk(idsString).body().string(), new TypeReference<>() {});
}
public ExtendedRecipeByIngredient getRecepieByIngredientsExtended(RecipeByIngredient recipeByIngredient) throws IOException {
ExtendedRecipeByIngredient extendedRecipeByIngredient = getExtendedRecipeFromId(recipeByIngredient.getId());
extendedRecipeByIngredient.setMissedIngredients(recipeByIngredient.getMissedIngredients());
return extendedRecipeByIngredient;
private Response queryInformationBulk(String idsString) throws IOException {
Request request = APIAuthentication.addAuthHeaders(new Request.Builder()
.url("https://" + dotenv.get("X-RapidAPI-Host") +
"/recipes/informationBulk?ids=" + idsString))
.build();
return client.newCall(request).execute();
}
public List<ExtendedRecipeByIngredient> getRecepieByIngredientsExtended(List<RecipeByIngredient> recipeByIngredients) throws IOException {
List<Integer> ids = recipeByIngredients.stream().map(RecipeByIngredient::getId).toList();
List<ExtendedRecipeByIngredient> extendedRecipeByIngredients = getExtendedRecipeFromIds(ids);
Iterator<RecipeByIngredient> recipeByIngredientIterator = recipeByIngredients.iterator();
Iterator<ExtendedRecipeByIngredient> extendedRecipeByIngredientIterator = extendedRecipeByIngredients.iterator();
while(recipeByIngredientIterator.hasNext() && extendedRecipeByIngredientIterator.hasNext()){
extendedRecipeByIngredientIterator.next().addMissingInfo(recipeByIngredientIterator.next());
}
return extendedRecipeByIngredients;
}
}