Reformatted code and made interfacing with api easier
This commit is contained in:
parent
7b2eccf61f
commit
f0e35cd96e
@ -5,7 +5,7 @@ plugins {
|
||||
}
|
||||
|
||||
group 'tech.nevets.deepj'
|
||||
version '1.0'
|
||||
version '1.1.0'
|
||||
|
||||
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_11
|
||||
|
||||
|
@ -8,11 +8,11 @@ import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class DeepJ {
|
||||
public DeepJ() {
|
||||
public class Request {
|
||||
public Request() {
|
||||
}
|
||||
|
||||
public String translate(String authKey, Enum<Languages> 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 lang;
|
||||
@ -43,6 +43,4 @@ public class DeepJ {
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
}
|
36
src/main/java/tech/nevets/deepj/Translator.java
Normal file
36
src/main/java/tech/nevets/deepj/Translator.java
Normal file
@ -0,0 +1,36 @@
|
||||
package tech.nevets.deepj;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Translator {
|
||||
private final String authKey;
|
||||
Request r = new Request();
|
||||
|
||||
public Translator(String authKey) {
|
||||
this.authKey = authKey;
|
||||
}
|
||||
public String translate(Enum<Languages> lang, String sourceMessage) {
|
||||
String response = "Error processing request";
|
||||
try {
|
||||
response = r.get(authKey, lang, sourceMessage);
|
||||
} catch (IOException | InterruptedException e) {
|
||||
System.out.println("Error processing request");
|
||||
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");
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package tech.nevets.deepj.json;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class JsonExtractor {
|
||||
public JsonExtractor() {
|
||||
}
|
||||
|
||||
public String extractMessage(String jsonResponse) {
|
||||
String message = "Error Processing Request";
|
||||
|
||||
JSONObject jo = new JSONObject(jsonResponse);
|
||||
JSONArray ja = jo.getJSONArray("translations");
|
||||
|
||||
for (int i = 0; i < ja.length(); i++) {
|
||||
JSONObject joo = ja.getJSONObject(i);
|
||||
message = joo.getString("text");
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public String extractDetectedLang(String jsonResponse) {
|
||||
String lang = "Error Processing Request";
|
||||
|
||||
JSONObject jo = new JSONObject(jsonResponse);
|
||||
JSONArray ja = jo.getJSONArray("translations");
|
||||
|
||||
for (int i = 0; i < ja.length(); i++) {
|
||||
JSONObject joo = ja.getJSONObject(i);
|
||||
lang = joo.getString("detected_source_language");
|
||||
}
|
||||
|
||||
return lang;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user