From 8b472a6d612aa65513ff70c1eebbba8acad3f222 Mon Sep 17 00:00:00 2001 From: Bruno Date: Fri, 17 Jun 2022 17:50:21 +0200 Subject: [PATCH] changed string to JSON objects/arrrays --- .../Controller/SpoonacularController.java | 18 +++--- .../implementation/ApiServiceImpl.java | 59 ++++++++----------- .../whattocook/repositories/ApiService.java | 10 ---- .../main/whattocook/services/ApiService.java | 13 ++++ .../services/SpoonacularService.java | 5 -- 5 files changed, 48 insertions(+), 57 deletions(-) delete mode 100644 backend/src/main/whattocook/repositories/ApiService.java create mode 100644 backend/src/main/whattocook/services/ApiService.java delete mode 100644 backend/src/main/whattocook/services/SpoonacularService.java diff --git a/backend/src/main/whattocook/Controller/SpoonacularController.java b/backend/src/main/whattocook/Controller/SpoonacularController.java index 90fd959..3a22222 100644 --- a/backend/src/main/whattocook/Controller/SpoonacularController.java +++ b/backend/src/main/whattocook/Controller/SpoonacularController.java @@ -1,14 +1,16 @@ package whattocook.Controller; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; -import whattocook.repositories.ApiService; +import whattocook.services.ApiService; import whattocook.repositories.ItemRepository; -import whattocook.implementation.ApiServiceImpl; import java.io.IOException; import java.util.LinkedList; @@ -23,19 +25,19 @@ public class SpoonacularController { private ApiService service; @GetMapping("api/forFridge") - public HttpEntity getForFridge() throws IOException, InterruptedException { - return new HttpEntity<>(service.getForIngridients(itemRepository.findAll(), nextRecepies)); + public HttpEntity getForFridge() throws IOException, InterruptedException, JSONException { + return new HttpEntity(service.getForIngridients(itemRepository.findAll(), nextRecepies)); } @GetMapping("api/random") - public HttpEntity getRandom() throws IOException, InterruptedException { - return new HttpEntity<>(service.getRandom(new LinkedList<>(), nextRecepies)); + public HttpEntity getRandom() throws IOException, InterruptedException, JSONException { + return new HttpEntity(service.getRandom(new LinkedList<>(), nextRecepies)); //when user has food preferences apply instead of linked list. } @GetMapping("api/oneFridge" ) - public HttpEntity getOneFridge() throws IOException, InterruptedException { - return new HttpEntity<>(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom)); + public HttpEntity getOneFridge() throws IOException, InterruptedException, JSONException { + return new HttpEntity(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom)); } public void setNextRecepies(int nextRecepies) { diff --git a/backend/src/main/whattocook/implementation/ApiServiceImpl.java b/backend/src/main/whattocook/implementation/ApiServiceImpl.java index 39a28a2..3a3e24e 100644 --- a/backend/src/main/whattocook/implementation/ApiServiceImpl.java +++ b/backend/src/main/whattocook/implementation/ApiServiceImpl.java @@ -3,8 +3,8 @@ package whattocook.implementation; import org.springframework.stereotype.Service; import whattocook.models.Item; -import whattocook.repositories.ApiService; - +import whattocook.services.ApiService; +import org.json.*; import java.io.IOException; import java.net.http.HttpResponse; @@ -12,8 +12,6 @@ import java.net.http.HttpRequest; import java.net.http.HttpClient; import java.net.URI; import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; import java.util.Random; @Service @@ -23,7 +21,7 @@ public class ApiServiceImpl implements ApiService { private final boolean IGNOREPANTRY = true; Random rnd=new Random(); - public String getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException { + public JSONArray getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException, JSONException { Iterator itemIterator = items.iterator(); if (!itemIterator.hasNext()) { return getRandom(new java.util.LinkedList(), number); @@ -40,26 +38,33 @@ public class ApiServiceImpl implements ApiService { .method("GET", java.net.http.HttpRequest.BodyPublishers.noBody()) .build(); java.net.http.HttpResponse response = java.net.http.HttpClient.newHttpClient().send(request, java.net.http.HttpResponse.BodyHandlers.ofString()); - return response.body(); + + JSONArray array=new JSONArray(response.body()); + return array; + + } } @Override - public String getOneForIngridients(Iterable items, int number) throws IOException, InterruptedException { - String recepies = getForIngridients(items, number); - List recepieList = splitToList(recepies); + public JSONObject getOneForIngridients(Iterable items, int number) throws IOException, InterruptedException, JSONException { + JSONArray array= getForIngridients(items, number); - return recepieList.get(rnd.nextInt(20)); + + return array.getJSONObject(rnd.nextInt(20)); } - public String getRandom(java.util.List tags, int number) throws java.io.IOException, InterruptedException { + public JSONArray getRandom(java.util.List tags, int number) throws java.io.IOException, InterruptedException, JSONException { if (tags.isEmpty()) { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + KEY + "&number=" + number)) .method("GET", HttpRequest.BodyPublishers.noBody()) .build(); HttpResponse response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); - return response.body(); + + JSONArray array=new JSONArray(response.body()); + return array; + } else { String tagString = tags.get(0); for (int i = 1; i < tags.size(); i++) { @@ -70,32 +75,18 @@ public class ApiServiceImpl implements ApiService { .method("GET", HttpRequest.BodyPublishers.noBody()) .build(); HttpResponse response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); - return response.body(); + JSONArray array=new JSONArray(response.body()); + return array; } } - private List splitToList(String recipies) { - int startOfRecipie = 1; - int bracketCounter = 0; - List recipieList = new LinkedList<>(); - for (int i = 0; i < recipies.length(); i++) { - if (recipies.charAt(i) == '{') { - bracketCounter++; - if (bracketCounter == 1) { - startOfRecipie = i; - } - } else if (recipies.charAt(i) == '}') { - bracketCounter--; - if (bracketCounter == 0) { - recipieList.add(recipies.substring(startOfRecipie, i+1 )); - - } - } - - } - return recipieList; + public static void main(String[] args) throws IOException, InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + "85cc006d508b447a88e659cd748899db" + "&number=" + 10)) + .method("GET", HttpRequest.BodyPublishers.noBody()) + .build(); + HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); } - } diff --git a/backend/src/main/whattocook/repositories/ApiService.java b/backend/src/main/whattocook/repositories/ApiService.java deleted file mode 100644 index 8188b67..0000000 --- a/backend/src/main/whattocook/repositories/ApiService.java +++ /dev/null @@ -1,10 +0,0 @@ -package whattocook.repositories; - -import whattocook.models.Item; - -public interface ApiService { - String getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException; - String getOneForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException; - - String getRandom(java.util.List tags, int number) throws java.io.IOException, InterruptedException; -} diff --git a/backend/src/main/whattocook/services/ApiService.java b/backend/src/main/whattocook/services/ApiService.java new file mode 100644 index 0000000..b8af462 --- /dev/null +++ b/backend/src/main/whattocook/services/ApiService.java @@ -0,0 +1,13 @@ +package whattocook.services; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import whattocook.models.Item; + +public interface ApiService { + JSONArray getForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException, JSONException; + JSONObject getOneForIngridients(Iterable items, int number) throws java.io.IOException, InterruptedException, JSONException; + + JSONArray getRandom(java.util.List tags, int number) throws java.io.IOException, InterruptedException, JSONException; +} diff --git a/backend/src/main/whattocook/services/SpoonacularService.java b/backend/src/main/whattocook/services/SpoonacularService.java deleted file mode 100644 index df989f2..0000000 --- a/backend/src/main/whattocook/services/SpoonacularService.java +++ /dev/null @@ -1,5 +0,0 @@ -package whattocook.services; - -public interface SpoonacularService { - String getByFridge(); -}