Update 'src/main/java/com/the5gi/DeepJ/Translator.java'

This commit is contained in:
5gi 2022-12-08 12:40:56 -05:00
parent 6f754483a5
commit dbf2af3c64

View File

@ -15,11 +15,13 @@ public class Translator {
//REQUEST INSIDE OF TRANSLATOR
protected Request request;
//GLOBAL AUTH KEY
//GLOBAL AUTH KEY (GAK)
public static String globalAPIKey = "null";
//CONSTRUCTORS
public Translator() {
//GAK
if (globalAPIKey == null) {
System.out.println("\n[DeepJ] You are currently using the Global API Key Translator Constructor." +"\n[DeepJ] If you are trying to use global API keys, Call the method \"Translator.setGlobalAuthKey(String key)\" and then re-use this contructor." + "\n[DeepJ] If you want to define an different authkey every time do: \"new Translator(String authKey)\"\n");
} else {
@ -27,24 +29,29 @@ public class Translator {
}
}
public Translator(String authKey) {
//NORMAL REQUEST (GAK Notice) (NO SILENCE)
if (!Objects.equals(globalAPIKey, "null")) {
System.out.println("[DeepJ] Looks like you have defined a global API key already! You can use if by just typing \"new Traslator()\"" + "\n instead of \"new Translator(String authKey)\". NOTE: This will work but just some advice :)");
}
request = new Request(authKey);
}
public Translator(String apiKey, boolean silence) {
//NORMAL REQUEST (GAK SILENCEABLE)
if (!Objects.equals(globalAPIKey, "null") && !silence) {
System.out.println("[DeepJ] Looks like you have defined a global API key already! You can use if by just typing \"new Traslator()\"" + "\n instead of \"new Translator(String authKey)\". NOTE: This will work but this is just some advice :)");
}
request = new Request(apiKey);
}
public static void setGlobalAPIKey(String key) {
//SET GLOBAL API KEY (STATIC)
globalAPIKey = key;
}
public static Translator newTranslator(String apiKey) {
//NORMAL REQUEST (NO GAK) (STATIC)
return new Translator(apiKey);
}
public static Translator newTranslator() {
//GAK (STATIC)
if (globalAPIKey == null) {
System.out.println("\n[DeepJ] You are currently using the Global API Key \".of()\" Translator." +
"\n[DeepJ] If you are trying to use global API keys, Call the method \"Translator.setGlobalAPIKey(String key)\" and then re-use this method." +
@ -105,7 +112,13 @@ public class Translator {
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().GET().header("Accept","*/*").header("Content-Type", "application/x-www-form-urlencoded").uri(URI.create("https://api-free.deepl.com/v2/translate?auth_key="+encodedAuthKey+"&target_lang="+langEnum.getApiString()+"&text="+encodedMessage)).build();
HttpRequest request = HttpRequest.newBuilder()
URI deepLURI = URI.create("https://api-free.deepl.com/v2/translate?auth_key="+encodedAuthKey+"&target_lang="+langEnum.getApiString()+"&text="+encodedMessage)
.GET()
.header("Accept","*/*")
.header("Content-Type", "application/x-www-form-urlencoded")
.uri(deepLURI)
.build();
HttpResponse<String> response = client.send(request,HttpResponse.BodyHandlers.ofString());
return response.body();
}