Merged Classes

Fixed repetitive auth key usage
This commit is contained in:
5gi 2022-11-03 17:31:18 -04:00
parent f5b9bc787b
commit 50d01058aa
2 changed files with 64 additions and 79 deletions

7
.idea/discord.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="ASK" />
<option name="description" value="" />
</component>
</project>

View File

@ -10,88 +10,65 @@ import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.io.IOException;
public class Translator { public class Translator {
Request request;
//TODO Once I get home I gotta see if this works
//NOTE: I'm stupid and did this during ELA
private final String authKey;
Request r = new Request();
public Translator(String authKey) { public Translator(String authKey) {
this.authKey = authKey; request = new Request(authKey);
} }
public String translate(Enum<Languages> lang, String sourceMessage) { public String translate(Enum<Language> langToTranslateTo, String sourceMessage) {
String response = "Error processing request"; String response = "Error processing request";
try { try {
response = r.get(authKey, lang, sourceMessage); response = request.get(langToTranslateTo, sourceMessage);
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
System.out.println("Error processing request"); System.out.println("Error processing request");
e.printStackTrace(); e.printStackTrace();
} }
String message = "Error Processing Request"; String message = "Error Processing Request";
JSONObject jo = new JSONObject(response); JSONObject jo = new JSONObject(response);
JSONArray ja = jo.getJSONArray("translations"); JSONArray ja = jo.getJSONArray("translations");
for (int i = 0; i < ja.length(); i++) { for (int i = 0; i < ja.length(); i++) {
JSONObject joo = ja.getJSONObject(i); JSONObject joo = ja.getJSONObject(i);
message = joo.getString("text"); message = joo.getString("text");
} }
return message; return message;
} }
} private static class Request {
private class Request { private final String authKey;
public Request() { public Request(String authKey) {
this.authKey = authKey;
} }
public String get(Enum<Language> langEnum, String message) throws IOException, InterruptedException {
public String get(String authKey, Enum<Languages> langEnum, String message) throws IOException, InterruptedException {
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8); String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
String lang = langEnum.toString();
String lang; if (langEnum == Language.EN_US) {
lang = langEnum.toString();
if (langEnum == Languages.ENUS) {
lang = "EN-US"; lang = "EN-US";
} else if (langEnum == Languages.ENGB) { } else if (langEnum == Language.EN_GB) {
lang = "EN-GB"; lang = "EN-GB";
} else if (langEnum == Languages.PTBR) { } else if (langEnum == Language.PT_BR) {
lang = "PT-BR"; lang = "PT-BR";
} else if (langEnum == Languages.PTPT) { } else if (langEnum == Language.PT_PT) {
lang = "PT-PT"; lang = "PT-PT";
} }
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8); String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder().GET()
.GET()
.header("Accept","*/*") .header("Accept","*/*")
.header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
.uri(URI.create("https://api-free.deepl.com/v2/translate?auth_key=" + encodedAuthKey + "&target_lang=" + lang + "&text=" + encodedMessage)) .uri(URI.create("https://api-free.deepl.com/v2/translate?auth_key=" + encodedAuthKey + "&target_lang=" + lang + "&text=" + encodedMessage)).build();
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
String data = response.body(); }
return data;
//wait for imports
} }
}
public static enum Languages { public enum Language {
BG, BG,
CS, CS,
DA, DA,
DE, DE,
EL, EL,
ENGB, EN_GB,
ENUS, EN_US,
ES, ES,
ET, ET,
FI, FI,
@ -103,13 +80,14 @@ public static enum Languages {
LV, LV,
NL, NL,
PL, PL,
PTBR, PT_BR,
PTPT, PT_PT,
RO, RO,
RU, RU,
SK, SK,
SL, SL,
SV, SV,
ZH ZH
}
} }