changed string to JSON objects/arrrays
This commit is contained in:
@@ -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<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();
|
||||
if (!itemIterator.hasNext()) {
|
||||
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())
|
||||
.build();
|
||||
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
|
||||
public String getOneForIngridients(Iterable<Item> items, int number) throws IOException, InterruptedException {
|
||||
String recepies = getForIngridients(items, number);
|
||||
List<String> recepieList = splitToList(recepies);
|
||||
public JSONObject getOneForIngridients(Iterable<Item> 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<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()) {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + KEY + "&number=" + number))
|
||||
.method("GET", HttpRequest.BodyPublishers.noBody())
|
||||
.build();
|
||||
HttpResponse<String> 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<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 ));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user