added ".of()" to translator

added ".of()" to Translator.java so you don't have to type "new" so many times
added null protection to request
added ability to set authKey to requests
This commit is contained in:
5gi 2022-11-16 09:45:20 -05:00
parent 29442d1f9a
commit a31d12bbb5

View File

@ -15,7 +15,7 @@ public class Translator {
public static void setGlobalAuthKey(String key) {
globalAuthKey = key;
}
private Request request;
protected Request request;
public Translator() {
if (globalAuthKey == null) {
/*
@ -52,13 +52,38 @@ instead of "new Translator(String authKey)". NOTE: This will work but just some
}
request = new Request(authKey);
}
public void setAuthKey(String key) {
request.setAuthKey(key);
}
public void setAPIKey(String authKey) {
request = new Request(authKey);
}
public String getAPIKey() {
return request.authKey;
}
public static Translator of(String authKey) {
return new Translator(authKey);
}
public static Translator of() {
if (globalAuthKey == null) {
/*
[DeepJ] You are currently using the Global Auth Key Translator Constructor.
[DeepJ] If you are trying to use global auth keys, Call the method "Translator.setGlobalAuthKey(String key)" and then re-use this contructor.
[DeepJ] If you want to define a different authkey every time do: "new Translator(String authKey)"
*/
System.out.println("\n[DeepJ] You are currently using the Global Auth Key \".of()\" Translator." +
"\n[DeepJ] If you are trying to use global auth keys, Call the method \"Translator.setGlobalAuthKey(String key)\" and then re-use this method." +
"\n[DeepJ] If you want to define an different authkey every time do: \"Translator.of(String authKey)\"\n");
return new Translator("null");
} else {
return new Translator();
}
}
public String translate(Language langToTranslateTo, String sourceMessage) {
if (request.authKey == "null") {
System.out.println("[DeepJ] This translator is null! Please recreate!");
return;
}
String response = "Error processing request";
try {
response = request.queryAPI(langToTranslateTo, sourceMessage);
@ -80,6 +105,9 @@ instead of "new Translator(String authKey)". NOTE: This will work but just some
public Request(String authKey) {
this.authKey = authKey;
}
public void setAuthKey(String key) {
authKey = key;
}
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);