From ac223093fe3c35feb564fa7dcb170255bd5f8f3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Bu=C3=9Fmann?= Date: Wed, 13 Jul 2022 16:22:36 +0200 Subject: [PATCH] Converted api classes to be only statically accessible | Renamed SpoonacularController to RecipeSearchController --- .../main/spoonaccular/RecipeInformation.java | 16 ++++++------- .../src/main/spoonaccular/RecipeSearch.java | 23 ++++++++----------- ...oller.java => RecipeSearchController.java} | 20 +++++++--------- 3 files changed, 24 insertions(+), 35 deletions(-) rename backend/src/main/whattocook/controller/{SpoonacularController.java => RecipeSearchController.java} (62%) diff --git a/backend/src/main/spoonaccular/RecipeInformation.java b/backend/src/main/spoonaccular/RecipeInformation.java index 7fc7a4f..186ad77 100644 --- a/backend/src/main/spoonaccular/RecipeInformation.java +++ b/backend/src/main/spoonaccular/RecipeInformation.java @@ -19,27 +19,25 @@ import java.util.stream.Collectors; @Component public class RecipeInformation { - private final Dotenv dotenv; - private final OkHttpClient client; + private final static Dotenv dotenv = Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load(); + private final static OkHttpClient client = new OkHttpClient(); - public RecipeInformation(){ - dotenv = Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load(); - client = new OkHttpClient(); + private RecipeInformation(){ } - public List getRecipeFromIds(List ids) throws IOException { + public static List getRecipeFromIds(List ids) throws IOException { String idsString = ids.stream().map(String::valueOf) .collect(Collectors.joining(",")); return new ObjectMapper().readValue(queryInformationBulk(idsString).body().string(), new TypeReference<>(){}); } - public List getExtendedRecipeFromIds(List ids) throws IOException { + public static List getExtendedRecipeFromIds(List ids) throws IOException { String idsString = ids.stream().map(String::valueOf) .collect(Collectors.joining(",")); 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() .url("https://" + dotenv.get("X-RapidAPI-Host") + "/recipes/informationBulk?ids=" + idsString)) @@ -47,7 +45,7 @@ public class RecipeInformation { return client.newCall(request).execute(); } - public List getRecepieByIngredientsExtended(List recipeByIngredients) throws IOException { + public static List getRecepieByIngredientsExtended(List recipeByIngredients) throws IOException { List ids = recipeByIngredients.stream().map(RecipeByIngredient::getId).toList(); List extendedRecipeByIngredients = getExtendedRecipeFromIds(ids); Iterator recipeByIngredientIterator = recipeByIngredients.iterator(); diff --git a/backend/src/main/spoonaccular/RecipeSearch.java b/backend/src/main/spoonaccular/RecipeSearch.java index a501623..b055dea 100644 --- a/backend/src/main/spoonaccular/RecipeSearch.java +++ b/backend/src/main/spoonaccular/RecipeSearch.java @@ -23,20 +23,15 @@ import java.util.Random; public class RecipeSearch { private static final boolean IGNOREPANTRY = true; - private final Random rnd; - private final Dotenv dotenv; - private final RecipeInformation recipeInformation; + private static final Random rnd = new Random(); + private static final Dotenv dotenv = Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load(); - private final OkHttpClient client; + private static final OkHttpClient client = new OkHttpClient(); - public RecipeSearch(){ - rnd = new Random(); - dotenv = Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load(); - recipeInformation = new RecipeInformation(); - client = new OkHttpClient(); + private RecipeSearch(){ } - public List getForIngridients(Iterable items, int number) throws java.io.IOException { + public static List getForIngridients(Iterable items, int number) throws java.io.IOException { List itemNames = new LinkedList<>(); items.forEach(item -> itemNames.add(item.getName())); String ingridients = String.join(",", itemNames); @@ -53,14 +48,14 @@ public class RecipeSearch { String responseString = response.body().string(); List recipeByIngredients = new ObjectMapper().readValue(responseString, new TypeReference<>(){}); - return recipeInformation.getRecepieByIngredientsExtended(recipeByIngredients); + return RecipeInformation.getRecepieByIngredientsExtended(recipeByIngredients); } - public ExtendedRecipeByIngredient getOneForIngridients(Iterable items, int number) throws IOException { - return this.getForIngridients(items, number).get(rnd.nextInt(number)); + public static ExtendedRecipeByIngredient getOneForIngridients(Iterable items, int number) throws IOException { + return getForIngridients(items, number).get(rnd.nextInt(number)); } - public List getRandom(List tags, int number) throws java.io.IOException, JSONException { + public static List getRandom(List tags, int number) throws java.io.IOException, JSONException { String tagString = String.join(",", tags); Request request = APIAuthentication.addAuthHeaders(new Request.Builder() .url("https://" + dotenv.get("X-RapidAPI-Host") + diff --git a/backend/src/main/whattocook/controller/SpoonacularController.java b/backend/src/main/whattocook/controller/RecipeSearchController.java similarity index 62% rename from backend/src/main/whattocook/controller/SpoonacularController.java rename to backend/src/main/whattocook/controller/RecipeSearchController.java index 8fe7fb6..59a3eec 100644 --- a/backend/src/main/whattocook/controller/SpoonacularController.java +++ b/backend/src/main/whattocook/controller/RecipeSearchController.java @@ -5,7 +5,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.rest.webmvc.BasePathAwareController; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import spoonaccular.RecipeInformation; import spoonaccular.RecipeSearch; import spoonaccular.models.recipe_by_ingredient.ExtendedRecipeByIngredient; import spoonaccular.models.recipe_information.Recipe; @@ -17,29 +16,26 @@ import java.util.List; @RestController() @BasePathAwareController() -public class SpoonacularController { - private int nextRecepies=10; - private int nextRecepiesForOneRandom=20; +public class RecipeSearchController { + private final int nextRecipes=10; + private final int nextRecipesForOneRandom = 20; + @Autowired private ItemRepository itemRepository; - @Autowired - private RecipeInformation recipeInformation; - @Autowired - private RecipeSearch recipeSearch; @GetMapping("/recipe/forFridge") public List getForFridge() throws IOException { - return recipeSearch.getForIngridients(itemRepository.findAll(), nextRecepies); + return RecipeSearch.getForIngridients(itemRepository.findAll(), nextRecipes); } @GetMapping("/recipe/random") - public List getRandom() throws IOException, InterruptedException, JSONException { - return recipeSearch.getRandom(new LinkedList<>(), nextRecepies); + public List getRandom() throws IOException, JSONException { + return RecipeSearch.getRandom(new LinkedList<>(), nextRecipes); //when user has food preferences apply instead of linked list. } @GetMapping("/recipe/oneFridge") public ExtendedRecipeByIngredient getOneFridge() throws IOException { - return recipeSearch.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom); + return RecipeSearch.getOneForIngridients(itemRepository.findAll(), nextRecipesForOneRandom); } }