Converted api classes to be only statically accessible | Renamed SpoonacularController to RecipeSearchController

This commit is contained in:
2022-07-13 16:22:36 +02:00
parent 5af845f195
commit ac223093fe
3 changed files with 24 additions and 35 deletions

View File

@@ -19,27 +19,25 @@ import java.util.stream.Collectors;
@Component @Component
public class RecipeInformation { public class RecipeInformation {
private final Dotenv dotenv; private final static Dotenv dotenv = Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load();
private final OkHttpClient client; private final static OkHttpClient client = new OkHttpClient();
public RecipeInformation(){ private RecipeInformation(){
dotenv = Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load();
client = new OkHttpClient();
} }
public List<Recipe> getRecipeFromIds(List<Integer> ids) throws IOException { public static List<Recipe> getRecipeFromIds(List<Integer> ids) throws IOException {
String idsString = ids.stream().map(String::valueOf) String idsString = ids.stream().map(String::valueOf)
.collect(Collectors.joining(",")); .collect(Collectors.joining(","));
return new ObjectMapper().readValue(queryInformationBulk(idsString).body().string(), new TypeReference<>(){}); return new ObjectMapper().readValue(queryInformationBulk(idsString).body().string(), new TypeReference<>(){});
} }
public List<ExtendedRecipeByIngredient> getExtendedRecipeFromIds(List<Integer> ids) throws IOException { public static List<ExtendedRecipeByIngredient> getExtendedRecipeFromIds(List<Integer> ids) throws IOException {
String idsString = ids.stream().map(String::valueOf) String idsString = ids.stream().map(String::valueOf)
.collect(Collectors.joining(",")); .collect(Collectors.joining(","));
return new ObjectMapper().readValue(queryInformationBulk(idsString).body().string(), new TypeReference<>() {}); return new ObjectMapper().readValue(queryInformationBulk(idsString).body().string(), new TypeReference<>() {});
} }
private Response queryInformationBulk(String idsString) throws IOException { private static Response queryInformationBulk(String idsString) throws IOException {
Request request = APIAuthentication.addAuthHeaders(new Request.Builder() Request request = APIAuthentication.addAuthHeaders(new Request.Builder()
.url("https://" + dotenv.get("X-RapidAPI-Host") + .url("https://" + dotenv.get("X-RapidAPI-Host") +
"/recipes/informationBulk?ids=" + idsString)) "/recipes/informationBulk?ids=" + idsString))
@@ -47,7 +45,7 @@ public class RecipeInformation {
return client.newCall(request).execute(); return client.newCall(request).execute();
} }
public List<ExtendedRecipeByIngredient> getRecepieByIngredientsExtended(List<RecipeByIngredient> recipeByIngredients) throws IOException { public static List<ExtendedRecipeByIngredient> getRecepieByIngredientsExtended(List<RecipeByIngredient> recipeByIngredients) throws IOException {
List<Integer> ids = recipeByIngredients.stream().map(RecipeByIngredient::getId).toList(); List<Integer> ids = recipeByIngredients.stream().map(RecipeByIngredient::getId).toList();
List<ExtendedRecipeByIngredient> extendedRecipeByIngredients = getExtendedRecipeFromIds(ids); List<ExtendedRecipeByIngredient> extendedRecipeByIngredients = getExtendedRecipeFromIds(ids);
Iterator<RecipeByIngredient> recipeByIngredientIterator = recipeByIngredients.iterator(); Iterator<RecipeByIngredient> recipeByIngredientIterator = recipeByIngredients.iterator();

View File

@@ -23,20 +23,15 @@ import java.util.Random;
public class RecipeSearch { public class RecipeSearch {
private static final boolean IGNOREPANTRY = true; private static final boolean IGNOREPANTRY = true;
private final Random rnd; private static final Random rnd = new Random();
private final Dotenv dotenv; private static final Dotenv dotenv = Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load();
private final RecipeInformation recipeInformation;
private final OkHttpClient client; private static final OkHttpClient client = new OkHttpClient();
public RecipeSearch(){ private RecipeSearch(){
rnd = new Random();
dotenv = Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load();
recipeInformation = new RecipeInformation();
client = new OkHttpClient();
} }
public List<ExtendedRecipeByIngredient> getForIngridients(Iterable<Item> items, int number) throws java.io.IOException { public static List<ExtendedRecipeByIngredient> getForIngridients(Iterable<Item> items, int number) throws java.io.IOException {
List<String> itemNames = new LinkedList<>(); List<String> itemNames = new LinkedList<>();
items.forEach(item -> itemNames.add(item.getName())); items.forEach(item -> itemNames.add(item.getName()));
String ingridients = String.join(",", itemNames); String ingridients = String.join(",", itemNames);
@@ -53,14 +48,14 @@ public class RecipeSearch {
String responseString = response.body().string(); String responseString = response.body().string();
List<RecipeByIngredient> recipeByIngredients = new ObjectMapper().readValue(responseString, new TypeReference<>(){}); List<RecipeByIngredient> recipeByIngredients = new ObjectMapper().readValue(responseString, new TypeReference<>(){});
return recipeInformation.getRecepieByIngredientsExtended(recipeByIngredients); return RecipeInformation.getRecepieByIngredientsExtended(recipeByIngredients);
} }
public ExtendedRecipeByIngredient getOneForIngridients(Iterable<Item> items, int number) throws IOException { public static ExtendedRecipeByIngredient getOneForIngridients(Iterable<Item> items, int number) throws IOException {
return this.getForIngridients(items, number).get(rnd.nextInt(number)); return getForIngridients(items, number).get(rnd.nextInt(number));
} }
public List<Recipe> getRandom(List<String> tags, int number) throws java.io.IOException, JSONException { public static List<Recipe> getRandom(List<String> tags, int number) throws java.io.IOException, JSONException {
String tagString = String.join(",", tags); String tagString = String.join(",", tags);
Request request = APIAuthentication.addAuthHeaders(new Request.Builder() Request request = APIAuthentication.addAuthHeaders(new Request.Builder()
.url("https://" + dotenv.get("X-RapidAPI-Host") + .url("https://" + dotenv.get("X-RapidAPI-Host") +

View File

@@ -5,7 +5,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.BasePathAwareController; import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import spoonaccular.RecipeInformation;
import spoonaccular.RecipeSearch; import spoonaccular.RecipeSearch;
import spoonaccular.models.recipe_by_ingredient.ExtendedRecipeByIngredient; import spoonaccular.models.recipe_by_ingredient.ExtendedRecipeByIngredient;
import spoonaccular.models.recipe_information.Recipe; import spoonaccular.models.recipe_information.Recipe;
@@ -17,29 +16,26 @@ import java.util.List;
@RestController() @RestController()
@BasePathAwareController() @BasePathAwareController()
public class SpoonacularController { public class RecipeSearchController {
private int nextRecepies=10; private final int nextRecipes=10;
private int nextRecepiesForOneRandom=20; private final int nextRecipesForOneRandom = 20;
@Autowired @Autowired
private ItemRepository itemRepository; private ItemRepository itemRepository;
@Autowired
private RecipeInformation recipeInformation;
@Autowired
private RecipeSearch recipeSearch;
@GetMapping("/recipe/forFridge") @GetMapping("/recipe/forFridge")
public List<ExtendedRecipeByIngredient> getForFridge() throws IOException { public List<ExtendedRecipeByIngredient> getForFridge() throws IOException {
return recipeSearch.getForIngridients(itemRepository.findAll(), nextRecepies); return RecipeSearch.getForIngridients(itemRepository.findAll(), nextRecipes);
} }
@GetMapping("/recipe/random") @GetMapping("/recipe/random")
public List<Recipe> getRandom() throws IOException, InterruptedException, JSONException { public List<Recipe> getRandom() throws IOException, JSONException {
return recipeSearch.getRandom(new LinkedList<>(), nextRecepies); return RecipeSearch.getRandom(new LinkedList<>(), nextRecipes);
//when user has food preferences apply instead of linked list. //when user has food preferences apply instead of linked list.
} }
@GetMapping("/recipe/oneFridge") @GetMapping("/recipe/oneFridge")
public ExtendedRecipeByIngredient getOneFridge() throws IOException { public ExtendedRecipeByIngredient getOneFridge() throws IOException {
return recipeSearch.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom); return RecipeSearch.getOneForIngridients(itemRepository.findAll(), nextRecipesForOneRandom);
} }
} }