Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<filtering>false</filtering>
</resource>
</resources>
</build>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
Expand Down
29 changes: 21 additions & 8 deletions src/main/java/com/detectlanguage/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;
import java.lang.reflect.Type;

public class Client {
private static final String AGENT = "detectlanguage-java";
private static final String AGENT = "detectlanguage-java/" + getVersionFromProperties();
private static final String CHARSET = "UTF-8";

public Client() {
Expand All @@ -36,7 +37,7 @@ public <T> T post(String path, String payload, Type responseType) throws APIErro
}

private <T> T execute(String method, String path, Map<String, Object> params,
String payload, Type responseType) throws APIError {
String payload, Type responseType) throws APIError {
URL url = buildUrl(path, params);

try {
Expand Down Expand Up @@ -104,9 +105,8 @@ private URL buildUrl(String path, Map<String, Object> params) {
DetectLanguage.apiVersion,
path);


if (params != null && params.size() > 0)
url+= '?' + buildQuery(params);
url += '?' + buildQuery(params);

try {
return new URL(url);
Expand All @@ -121,9 +121,7 @@ private HttpURLConnection createConnection(URL url) throws IOException {
conn.setReadTimeout(DetectLanguage.timeout);
conn.setUseCaches(false);

String version = getClass().getPackage().getImplementationVersion();

conn.setRequestProperty("User-Agent", AGENT + '/' + version);
conn.setRequestProperty("User-Agent", AGENT);
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer " + DetectLanguage.apiKey);

Expand Down Expand Up @@ -183,7 +181,7 @@ private static Map<String, String> flattenParams(Map<String, Object> params) {

private static String getResponseBody(InputStream responseStream)
throws IOException {
//\A is the beginning of
// \A is the beginning of
// the stream boundary
String rBody = new Scanner(responseStream, CHARSET)
.useDelimiter("\\A")
Expand All @@ -192,4 +190,19 @@ private static String getResponseBody(InputStream responseStream)
responseStream.close();
return rBody;
}

private static String getVersionFromProperties() {
try {
Properties props = new Properties();
InputStream input = Client.class.getClassLoader().getResourceAsStream("version.properties");
if (input != null) {
props.load(input);
input.close();
return props.getProperty("version", "unknown");
}
} catch (IOException e) {
// Fallback to unknown version
}
return "unknown";
}
}
1 change: 1 addition & 0 deletions src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]@
4 changes: 2 additions & 2 deletions src/test/java/com/detectlanguage/DetectLanguageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testDetectError() throws APIError {

@Test
public void testBatchDetect() throws APIError {
String[] texts = {"Hello world", "Kabo kabikas, žiūri žiūrikas"};
String[] texts = { "Hello world", "Kabo kabikas, žiūri žiūrikas" };

List<List<Result>> results = DetectLanguage.detect(texts);
Result result;
Expand All @@ -57,7 +57,7 @@ public void testBatchDetect() throws APIError {
public void testBatchDetectError() throws APIError {
DetectLanguage.apiKey = "INVALID";

String[] texts = {"Hello world", "Kabo kabikas, žiūri žiūrikas"};
String[] texts = { "Hello world", "Kabo kabikas, žiūri žiūrikas" };

DetectLanguage.detect(texts);
}
Expand Down