Files
EIST-Teamprojekt/backend/src/main/spoonaccular/AmountConversion.java
Simon Bußmann 711a8e5c21 first draft implemented
Still struggling at some ingredients which deviating names
2022-07-16 22:20:32 +02:00

40 lines
1.4 KiB
Java

package spoonaccular;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.cdimascio.dotenv.Dotenv;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import spoonaccular.models.amount_conversion.ConvertedAmount;
import java.io.IOException;
public class AmountConversion {
private static final OkHttpClient client = new OkHttpClient();
private static final Dotenv dotenv = Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load();
private AmountConversion(){
}
public static double convertAmount(String ingrdientName, Double sourceAmount, String sourceUnit, String targetUnit) throws IOException {
Request request = APIAuthentication.addAuthHeaders(
new Request.Builder()
.url("https://" + dotenv.get("X-RapidAPI-Host") +
"/recipes/convert?ingredientName=" + ingrdientName +
"&targetUnit=" + targetUnit +
"&sourceUnit=" + sourceUnit +
"&sourceAmount=" + sourceAmount)
).build();
Response response = client.newCall(request).execute();
String responseString = response.body().string();
try {
return new ObjectMapper().readValue(responseString, ConvertedAmount.class).getTargetAmount();
} catch (Exception e){
return 0;
}
}
}