Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 668b6b5

Browse files
committed
Update to v3 api
1 parent eeea066 commit 668b6b5

File tree

9 files changed

+57
-129
lines changed

9 files changed

+57
-129
lines changed

‎src/main/java/com/detectlanguage/Client.java‎

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,45 @@
1818
import java.util.HashMap;
1919
import java.util.Map;
2020
import java.util.Scanner;
21+
import java.lang.reflect.Type;
2122

2223
public class Client {
23-
24-
public static final String CHARSET = "UTF-8";
25-
2624
private static final String AGENT = "detectlanguage-java";
25+
private static final String CHARSET = "UTF-8";
2726

2827
public Client() {
2928
}
3029

31-
public <T> T execute(String method, Map<String, Object> params,
32-
Class<T> responseClass) throws APIError {
33-
URL url = buildUrl(method);
34-
String query = buildQuery(params);
30+
public <T> T get(String path, Type responseType) throws APIError {
31+
return execute("GET", path, null, null, responseType);
32+
}
33+
34+
public <T> T post(String path, String payload, Type responseType) throws APIError {
35+
return execute("POST", path, null, payload, responseType);
36+
}
37+
38+
private <T> T execute(String method, String path, Map<String, Object> params,
39+
String payload, Type responseType) throws APIError {
40+
URL url = buildUrl(path, params);
3541

3642
try {
37-
HttpURLConnection conn = createPostConnection(url, query);
43+
HttpURLConnection conn = createConnection(url);
44+
45+
conn.setDoOutput(true);
46+
conn.setRequestMethod(method);
47+
conn.setRequestProperty("Content-Type", "application/json");
48+
49+
if (payload != null) {
50+
OutputStream output = null;
51+
try {
52+
output = conn.getOutputStream();
53+
output.write(payload.getBytes(CHARSET));
54+
} finally {
55+
if (output != null) {
56+
output.close();
57+
}
58+
}
59+
}
3860

3961
try {
4062
// trigger the request
@@ -47,7 +69,7 @@ public <T> T execute(String method, Map<String, Object> params,
4769
body = getResponseBody(conn.getErrorStream());
4870
}
4971

50-
return processResponse(responseClass, body);
72+
return processResponse(responseType, body);
5173
} finally {
5274
conn.disconnect();
5375
}
@@ -56,7 +78,7 @@ public <T> T execute(String method, Map<String, Object> params,
5678
}
5779
}
5880

59-
private <T> T processResponse(Class<T> responseClass, String body)
81+
private <T> T processResponse(TyperesponseType, String body)
6082
throws APIError {
6183

6284
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
@@ -69,20 +91,15 @@ private <T> T processResponse(Class<T> responseClass, String body)
6991
}
7092

7193
try {
72-
return gson.fromJson(body, responseClass);
94+
return gson.fromJson(body, responseType);
7395
} catch (JsonSyntaxException e) {
7496
throw new APIError("Server error. Invalid response format.", 9999);
7597
}
7698
}
7799

78-
private String getProtocol() {
79-
return DetectLanguage.ssl ? "https" : "http";
80-
}
81-
82100
private URL buildUrl(String path, Map<String, Object> params) {
83101
String url = String.format(
84-
"%s://%s/%s/%s",
85-
getProtocol(),
102+
"https://%s/%s/%s",
86103
DetectLanguage.apiHost,
87104
DetectLanguage.apiVersion,
88105
path);
@@ -98,31 +115,6 @@ private URL buildUrl(String path, Map<String, Object> params) {
98115
}
99116
}
100117

101-
private URL buildUrl(String path) {
102-
return buildUrl(path, null);
103-
}
104-
105-
private HttpURLConnection createPostConnection(
106-
URL url, String query) throws IOException {
107-
HttpURLConnection conn = createConnection(url);
108-
109-
conn.setDoOutput(true);
110-
conn.setRequestMethod("POST");
111-
conn.setRequestProperty("Content-Type", String.format(
112-
"application/x-www-form-urlencoded;charset=%s", CHARSET));
113-
114-
OutputStream output = null;
115-
try {
116-
output = conn.getOutputStream();
117-
output.write(query.getBytes(CHARSET));
118-
} finally {
119-
if (output != null) {
120-
output.close();
121-
}
122-
}
123-
return conn;
124-
}
125-
126118
private HttpURLConnection createConnection(URL url) throws IOException {
127119
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
128120
conn.setConnectTimeout(DetectLanguage.timeout);
@@ -133,7 +125,6 @@ private HttpURLConnection createConnection(URL url) throws IOException {
133125

134126
conn.setRequestProperty("User-Agent", AGENT + '/' + version);
135127
conn.setRequestProperty("Accept", "application/json");
136-
conn.setRequestProperty("Accept-Charset", CHARSET);
137128
conn.setRequestProperty("Authorization", "Bearer " + DetectLanguage.apiKey);
138129

139130
return conn;
Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.detectlanguage;
22

33
import com.detectlanguage.errors.APIError;
4-
import com.detectlanguage.responses.BatchDetectResponse;
5-
import com.detectlanguage.responses.DetectResponse;
64
import com.detectlanguage.responses.StatusResponse;
75

86
import java.util.HashMap;
97
import java.util.List;
8+
import java.lang.reflect.Type;
9+
import com.google.gson.reflect.TypeToken;
10+
import com.google.gson.Gson;
1011

1112
public abstract class DetectLanguage {
1213
public static String apiHost = "ws.detectlanguage.com";
13-
public static String apiVersion = "0.2";
14+
public static String apiVersion = "v3";
1415
public static String apiKey;
1516
public static int timeout = 3 * 1000;
16-
public static boolean ssl = false;
1717

1818
public static String simpleDetect(final String text) throws APIError {
1919
List<Result> results = detect(text);
@@ -25,39 +25,35 @@ public static String simpleDetect(final String text) throws APIError {
2525
}
2626

2727
public static List<Result> detect(final String text) throws APIError {
28-
HashMap<String, Object> params = new HashMap<String, Object>();
29-
params.put("q", text);
28+
HashMap<String, Object> jsonMap = new HashMap<String, Object>();
29+
jsonMap.put("q", text);
3030

31-
DetectResponseresponse = getClient().execute("detect", params,
32-
DetectResponse.class);
31+
Gsongson = newGson();
32+
Stringpayload = gson.toJson(jsonMap);
3333

34-
return response.data.detections;
34+
Type resultType = new TypeToken<List<Result>>(){}.getType();
35+
36+
return getClient().post("detect", payload, resultType);
3537
}
3638

3739
public static List<List<Result>> detect(final String[] texts)
3840
throws APIError {
39-
HashMap<String, Object> params = new HashMap<String, Object>();
41+
HashMap<String, Object> jsonMap = new HashMap<String, Object>();
42+
jsonMap.put("q", texts);
4043

41-
for (int i = 0; i < texts.length; i++) {
42-
params.put("q[" + i + "]", texts[i]);
43-
}
44+
Gson gson = new Gson();
45+
String payload = gson.toJson(jsonMap);
4446

45-
BatchDetectResponse response = getClient().execute("detect", params,
46-
BatchDetectResponse.class);
47+
Type resultType = new TypeToken<List<List<Result>>>(){}.getType();
4748

48-
return response.data.detections;
49+
return getClient().post("detect-batch", payload, resultType);
4950
}
5051

5152
public static StatusResponse getStatus() throws APIError {
52-
HashMap<String, Object> params = new HashMap<String, Object>();
53-
54-
StatusResponse response = getClient().execute("user/status", params,
55-
StatusResponse.class);
56-
57-
return response;
53+
return getClient().get("account/status", StatusResponse.class);
5854
}
5955

6056
private static Client getClient() {
6157
return new Client();
6258
}
63-
}
59+
}

‎src/main/java/com/detectlanguage/Result.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22

33
public class Result {
44
public String language;
5-
public boolean isReliable;
6-
public double confidence;
5+
public double score;
76
}

‎src/main/java/com/detectlanguage/responses/BatchDetectResponse.java‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎src/main/java/com/detectlanguage/responses/BatchDetectionsData.java‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎src/main/java/com/detectlanguage/responses/DetectResponse.java‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎src/main/java/com/detectlanguage/responses/DetectionsData.java‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎src/test/java/com/detectlanguage/GenericTest.java‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public void testDetect() throws APIError {
2626
Result result = results.get(0);
2727

2828
assertEquals(result.language, "en");
29-
assertTrue(result.isReliable);
30-
assertTrue(result.confidence > 0);
29+
assertTrue(result.score > 0);
3130
}
3231

3332
@Test(expected = APIError.class)
@@ -46,14 +45,12 @@ public void testBatchDetect() throws APIError {
4645
result = results.get(0).get(0);
4746

4847
assertEquals(result.language, "en");
49-
assertTrue(result.isReliable);
50-
assertTrue(result.confidence > 0);
48+
assertTrue(result.score > 0);
5149

5250
result = results.get(1).get(0);
5351

5452
assertEquals(result.language, "lt");
55-
assertTrue(result.isReliable);
56-
assertTrue(result.confidence > 0);
53+
assertTrue(result.score > 0);
5754
}
5855

5956
@Test(expected = APIError.class)

‎src/test/java/com/detectlanguage/SslTest.java‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /