From b9fb0978360a53c132ccf2101dac5ef8260c4618 Mon Sep 17 00:00:00 2001 From: 5gi <5gi@gitea.noreply@nevets.tech> Date: Fri, 4 Nov 2022 11:19:09 -0400 Subject: [PATCH] Update 'src/main/java/tech/nevets/deepj/Translator.java' --- .../java/tech/nevets/deepj/Translator.java | 47 +++++++++++++++---- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/tech/nevets/deepj/Translator.java b/src/main/java/tech/nevets/deepj/Translator.java index b411726..db4b97c 100644 --- a/src/main/java/tech/nevets/deepj/Translator.java +++ b/src/main/java/tech/nevets/deepj/Translator.java @@ -9,24 +9,51 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; public class Translator { - Request request; - public Translator(String authKey) { +//TODO TEST CLASS + public static String globalAuthKey = "null"; + + public static void setGlobalAuthKey(String key) { + this.globalAuthKey = key; + } + private Request request; + public Translator() { + if (globalAuthKey == null) { + System.out.println("\n[DeepJ] You are currently using the Global Auth Key Translator Constructor." + + "\n[DeepJ] If you are trying to use global auth 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 { + request = new request(globalAuthKey); + } + } + public Translator(@NotNull String authKey) { + if (globalAuthKey != "null") { + System.out.println("[DeepJ] Looks like you have defined a global auth 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(@NotNull String authKey, @NotNull boolean silence) { + if (globalAuthKey != "null" && silence == false) { + System.out.println("[DeepJ] Looks like you have defined a global auth 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 String translate(Language langToTranslateTo, String sourceMessage) { String response = "Error processing request"; try { - response = request.get(langToTranslateTo, sourceMessage); + response = request.queryAPI(langToTranslateTo, sourceMessage); } catch (IOException | InterruptedException e) { - System.out.println("Error processing request"); + System.out.println(response); e.printStackTrace(); } String message = "Error Processing Request"; - JSONObject jo = new JSONObject(response); - JSONArray ja = jo.getJSONArray("translations"); - for (int i = 0; i < ja.length(); i++) { - JSONObject joo = ja.getJSONObject(i); - message = joo.getString("text"); + JSONObject jsonObject = new JSONObject(response); + JSONArray jsonArray = jsonObject.getJSONArray("translations"); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonObject1 = jsonArray.getJSONObject(i); + message = jsonObject.getString("text"); } return message; } @@ -35,7 +62,7 @@ public class Translator { public Request(String authKey) { this.authKey = authKey; } - public String get(Language langEnum, String message) throws IOException, InterruptedException { + public String queryAPI(Language langEnum, String message) throws IOException, InterruptedException { String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8); String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8); HttpClient client = HttpClient.newHttpClient();