basic api functuinality added, still needs debugging
This commit is contained in:
@@ -10,10 +10,11 @@ import whattocook.repositories.ItemRepository;
|
|||||||
import whattocook.implementation.ApiServiceImpl;
|
import whattocook.implementation.ApiServiceImpl;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class SpoonacularController {
|
public class SpoonacularController {
|
||||||
private final int nextPages = 10;
|
private int nextRecepies;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ItemRepository itemRepository;
|
private ItemRepository itemRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -21,6 +22,16 @@ public class SpoonacularController {
|
|||||||
|
|
||||||
@GetMapping("api/forFridge")
|
@GetMapping("api/forFridge")
|
||||||
public HttpEntity<String> getForFridge() throws IOException, InterruptedException {
|
public HttpEntity<String> getForFridge() throws IOException, InterruptedException {
|
||||||
return new HttpEntity<>(service.getForIngridients(itemRepository.findAll(), 10));
|
return new HttpEntity<>(service.getForIngridients(itemRepository.findAll(), nextRecepies));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("api/random")
|
||||||
|
public HttpEntity<String> getRandom() throws IOException, InterruptedException {
|
||||||
|
return new HttpEntity<>(service.getRandom(new LinkedList<>(), nextRecepies));
|
||||||
|
//when user has food preferences apply instead of linked list.
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNextRecepies(int nextRecepies) {
|
||||||
|
this.nextRecepies = nextRecepies;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package whattocook.implementation;
|
|||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import whattocook.models.Item;
|
import whattocook.models.Item;
|
||||||
|
import whattocook.models.Unit;
|
||||||
import whattocook.repositories.ApiService;
|
import whattocook.repositories.ApiService;
|
||||||
|
|
||||||
|
|
||||||
@@ -16,14 +17,14 @@ import java.util.LinkedList;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ApiServiceImpl implements ApiService {
|
public class ApiServiceImpl implements ApiService {
|
||||||
private final String KEY = "";
|
private final String KEY = "85cc006d508b447a88e659cd748899db";
|
||||||
private final String RANKING = "2";
|
private final String RANKING = "2";
|
||||||
private final boolean IGNOREPANTRY = true;
|
private final boolean IGNOREPANTRY = true;
|
||||||
|
|
||||||
public String getForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException {
|
public String getForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException {
|
||||||
Iterator<Item> itemIterator = items.iterator();
|
Iterator<Item> itemIterator = items.iterator();
|
||||||
if (!itemIterator.hasNext()) {
|
if (!itemIterator.hasNext()) {
|
||||||
return getRandom(number, new java.util.LinkedList<String>());
|
return getRandom( new java.util.LinkedList<String>(), number);
|
||||||
} else {
|
} else {
|
||||||
String ingridients = itemIterator.next().getName();
|
String ingridients = itemIterator.next().getName();
|
||||||
for (Iterator<Item> it = itemIterator; it.hasNext(); ) {
|
for (Iterator<Item> it = itemIterator; it.hasNext(); ) {
|
||||||
@@ -33,9 +34,7 @@ public class ApiServiceImpl implements ApiService {
|
|||||||
ingridients += "," + curryItem.getName();
|
ingridients += "," + curryItem.getName();
|
||||||
}
|
}
|
||||||
java.net.http.HttpRequest request = java.net.http.HttpRequest.newBuilder()
|
java.net.http.HttpRequest request = java.net.http.HttpRequest.newBuilder()
|
||||||
.uri(java.net.URI.create("https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/findByIngredients?ingredients=" + ingridients + "&ranking=" + RANKING + "&ignorePantry=" + IGNOREPANTRY + "&number=" + number))
|
.uri(java.net.URI.create("https://api.spoonacular.com/recipes/findByIngredients?apiKey="+KEY+"&ingredients=" + ingridients + "&ranking=" + RANKING + "&ignorePantry=" + IGNOREPANTRY + "&number=" + number))
|
||||||
.header("X-RapidAPI-Key", KEY)
|
|
||||||
.header("X-RapidAPI-Host", "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com")
|
|
||||||
.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());
|
||||||
@@ -43,7 +42,7 @@ public class ApiServiceImpl implements ApiService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRandom(int number, java.util.List<String> tags) throws java.io.IOException, InterruptedException {
|
public String getRandom( java.util.List<String> tags, int number) throws java.io.IOException, InterruptedException {
|
||||||
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))
|
||||||
@@ -67,7 +66,13 @@ public class ApiServiceImpl implements ApiService {
|
|||||||
|
|
||||||
public static void main(String[] args) throws IOException, InterruptedException {
|
public static void main(String[] args) throws IOException, InterruptedException {
|
||||||
ApiServiceImpl impl = new ApiServiceImpl();
|
ApiServiceImpl impl = new ApiServiceImpl();
|
||||||
System.out.println(impl.getRandom(10, new LinkedList<>()));
|
LinkedList<Item > tags= new LinkedList<>();
|
||||||
|
tags.add(new Item("tortelini", Unit.GRAMMS,75));
|
||||||
|
tags.add(new Item("garlic", Unit.GRAMMS,75));
|
||||||
|
tags.add(new Item("eggplant", Unit.GRAMMS,75));
|
||||||
|
tags.add(new Item("zuccini", Unit.GRAMMS,75));
|
||||||
|
|
||||||
|
System.out.println(impl.getForIngridients(tags,10 ));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,5 +5,5 @@ import whattocook.models.Item;
|
|||||||
public interface ApiService {
|
public interface ApiService {
|
||||||
String getForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException;
|
String getForIngridients(Iterable<Item> items, int number) throws java.io.IOException, InterruptedException;
|
||||||
|
|
||||||
String getRandom(int number, java.util.List<String> tags) throws java.io.IOException, InterruptedException;
|
String getRandom(java.util.List<String> tags, int number) throws java.io.IOException, InterruptedException;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user