changed string to JSON objects/arrrays

This commit is contained in:
Bruno
2022-06-17 17:50:21 +02:00
parent 105aaf6bad
commit 8b472a6d61
5 changed files with 48 additions and 57 deletions

View File

@@ -1,14 +1,16 @@
package whattocook.Controller; 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.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import whattocook.repositories.ApiService; import whattocook.services.ApiService;
import whattocook.repositories.ItemRepository; import whattocook.repositories.ItemRepository;
import whattocook.implementation.ApiServiceImpl;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
@@ -23,19 +25,19 @@ public class SpoonacularController {
private ApiService service; private ApiService service;
@GetMapping("api/forFridge") @GetMapping("api/forFridge")
public HttpEntity<String> getForFridge() throws IOException, InterruptedException { public HttpEntity<JSONArray> getForFridge() throws IOException, InterruptedException, JSONException {
return new HttpEntity<>(service.getForIngridients(itemRepository.findAll(), nextRecepies)); return new HttpEntity<JSONArray>(service.getForIngridients(itemRepository.findAll(), nextRecepies));
} }
@GetMapping("api/random") @GetMapping("api/random")
public HttpEntity<String> getRandom() throws IOException, InterruptedException { public HttpEntity<JSONArray> getRandom() throws IOException, InterruptedException, JSONException {
return new HttpEntity<>(service.getRandom(new LinkedList<>(), nextRecepies)); return new HttpEntity<JSONArray>(service.getRandom(new LinkedList<>(), nextRecepies));
//when user has food preferences apply instead of linked list. //when user has food preferences apply instead of linked list.
} }
@GetMapping("api/oneFridge" ) @GetMapping("api/oneFridge" )
public HttpEntity<String> getOneFridge() throws IOException, InterruptedException { public HttpEntity<JSONObject> getOneFridge() throws IOException, InterruptedException, JSONException {
return new HttpEntity<>(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom)); return new HttpEntity<JSONObject>(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom));
} }
public void setNextRecepies(int nextRecepies) { public void setNextRecepies(int nextRecepies) {

View File

@@ -3,8 +3,8 @@ package whattocook.implementation;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import whattocook.models.Item; import whattocook.models.Item;
import whattocook.repositories.ApiService; import whattocook.services.ApiService;
import org.json.*;
import java.io.IOException; import java.io.IOException;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
@@ -12,8 +12,6 @@ import java.net.http.HttpRequest;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.URI; import java.net.URI;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random; import java.util.Random;
@Service @Service
@@ -23,7 +21,7 @@ public class ApiServiceImpl implements ApiService {
private final boolean IGNOREPANTRY = true; private final boolean IGNOREPANTRY = true;
Random rnd=new Random(); Random rnd=new Random();
public String getForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException { public JSONArray getForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException, JSONException {
Iterator<Item> itemIterator = items.iterator(); Iterator<Item> itemIterator = items.iterator();
if (!itemIterator.hasNext()) { if (!itemIterator.hasNext()) {
return getRandom(new java.util.LinkedList<String>(), number); return getRandom(new java.util.LinkedList<String>(), number);
@@ -40,26 +38,33 @@ public class ApiServiceImpl implements ApiService {
.method("GET", java.net.http.HttpRequest.BodyPublishers.noBody()) .method("GET", java.net.http.HttpRequest.BodyPublishers.noBody())
.build(); .build();
java.net.http.HttpResponse<String> response = java.net.http.HttpClient.newHttpClient().send(request, java.net.http.HttpResponse.BodyHandlers.ofString()); java.net.http.HttpResponse<String> 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 @Override
public String getOneForIngridients(Iterable<Item> items, int number) throws IOException, InterruptedException { public JSONObject getOneForIngridients(Iterable<Item> items, int number) throws IOException, InterruptedException, JSONException {
String recepies = getForIngridients(items, number); JSONArray array= getForIngridients(items, number);
List<String> recepieList = splitToList(recepies);
return recepieList.get(rnd.nextInt(20));
return array.getJSONObject(rnd.nextInt(20));
} }
public String getRandom(java.util.List<String> tags, int number) throws java.io.IOException, InterruptedException { public JSONArray getRandom(java.util.List<String> tags, int number) throws java.io.IOException, InterruptedException, JSONException {
if (tags.isEmpty()) { if (tags.isEmpty()) {
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + KEY + "&number=" + number)) .uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + KEY + "&number=" + number))
.method("GET", HttpRequest.BodyPublishers.noBody()) .method("GET", HttpRequest.BodyPublishers.noBody())
.build(); .build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
JSONArray array=new JSONArray(response.body());
return array;
} else { } else {
String tagString = tags.get(0); String tagString = tags.get(0);
for (int i = 1; i < tags.size(); i++) { for (int i = 1; i < tags.size(); i++) {
@@ -70,32 +75,18 @@ public class ApiServiceImpl implements ApiService {
.method("GET", HttpRequest.BodyPublishers.noBody()) .method("GET", HttpRequest.BodyPublishers.noBody())
.build(); .build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
return response.body(); JSONArray array=new JSONArray(response.body());
return array;
} }
} }
private List<String> splitToList(String recipies) {
int startOfRecipie = 1;
int bracketCounter = 0;
List<String> 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 ));
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());
} }
} }
}
return recipieList;
}
}

View File

@@ -1,10 +0,0 @@
package whattocook.repositories;
import whattocook.models.Item;
public interface ApiService {
String getForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException;
String getOneForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException;
String getRandom(java.util.List<String> tags, int number) throws java.io.IOException, InterruptedException;
}

View File

@@ -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<Item> items, int number) throws java.io.IOException, InterruptedException, JSONException;
JSONObject getOneForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException, JSONException;
JSONArray getRandom(java.util.List<String> tags, int number) throws java.io.IOException, InterruptedException, JSONException;
}

View File

@@ -1,5 +0,0 @@
package whattocook.services;
public interface SpoonacularService {
String getByFridge();
}