Merge pull request #29 from cato447/API-querry

Added Spoonacular Api querry
This commit is contained in:
cato
2022-06-27 10:45:34 +02:00
committed by GitHub
3 changed files with 148 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import whattocook.services.SpoonacularApiService;
import whattocook.repositories.ItemRepository;
import java.io.IOException;
import java.util.LinkedList;
@RestController
@RequestMapping("/recipes")
public class SpoonacularController {
private int nextRecepies=10;
private int nextRecepiesForOneRandom=20;
@Autowired
private ItemRepository itemRepository;
@Autowired
private SpoonacularApiService service;
@GetMapping("/forFridge")
public HttpEntity<JSONArray> getForFridge() throws IOException, InterruptedException, JSONException {
return new HttpEntity<JSONArray>(service.getForIngridients(itemRepository.findAll(), nextRecepies));
}
@GetMapping("/random")
public HttpEntity<JSONArray> getRandom() throws IOException, InterruptedException, JSONException {
return new HttpEntity<JSONArray>(service.getRandom(new LinkedList<>(), nextRecepies));
//when user has food preferences apply instead of linked list.
}
@GetMapping("/oneFridge")
public HttpEntity<JSONObject> getOneFridge() throws IOException, InterruptedException, JSONException {
return new HttpEntity<JSONObject>(service.getOneForIngridients(itemRepository.findAll(), nextRecepiesForOneRandom));
}
public void setNextRecepies(int nextRecepies) {
this.nextRecepies = nextRecepies;
}
}

View File

@@ -0,0 +1,86 @@
package whattocook.implementation;
import org.springframework.stereotype.Service;
import whattocook.models.Item;
import whattocook.services.SpoonacularApiService;
import org.json.*;
import java.io.IOException;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest;
import java.net.http.HttpClient;
import java.net.URI;
import java.util.Iterator;
import java.util.Random;
@Service
public class SpoonacularApiServiceImpl implements SpoonacularApiService {
private final String KEY = "85cc006d508b447a88e659cd748899db";
private final String RANKING = "2";
private final boolean IGNOREPANTRY = true;
Random rnd=new Random();
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);
} else {
String ingridients = itemIterator.next().getName();
for (Iterator<Item> it = itemIterator; it.hasNext(); ) {
Item curryItem = it.next();
ingridients += "," + curryItem.getName();
}
java.net.http.HttpRequest request = java.net.http.HttpRequest.newBuilder()
.uri(java.net.URI.create("https://api.spoonacular.com/recipes/findByIngredients?apiKey=" + KEY + "&ingredients=" + ingridients + "&ranking=" + RANKING + "&ignorePantry=" + IGNOREPANTRY + "&number=" + number))
.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());
JSONArray array=new JSONArray(response.body());
return array;
}
}
@Override
public JSONObject getOneForIngridients(Iterable<Item> items, int number) throws IOException, InterruptedException, JSONException {
JSONArray array= getForIngridients(items, number);
return array.getJSONObject(rnd.nextInt(20));
}
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());
JSONArray array=new JSONArray(response.body());
return array;
} else {
String tagString = tags.get(0);
for (int i = 1; i < tags.size(); i++) {
tagString += "," + tags.get(i);
}
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.spoonacular.com/recipes/random?apiKey=" + KEY + "&number=" + number + "&tags=" + tagString))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
JSONArray array=new JSONArray(response.body());
return array;
}
}
}

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 SpoonacularApiService {
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;
}