Skip to content

Commit ff91a3f

Browse files
authored
Fix user agent version resolution (#15)
1 parent 73928e7 commit ff91a3f

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3636
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
3737
</properties>
38+
<build>
39+
<resources>
40+
<resource>
41+
<directory>src/main/resources</directory>
42+
<filtering>true</filtering>
43+
</resource>
44+
<resource>
45+
<directory>src/main/java</directory>
46+
<filtering>false</filtering>
47+
</resource>
48+
</resources>
49+
</build>
3850
<parent>
3951
<groupId>org.sonatype.oss</groupId>
4052
<artifactId>oss-parent</artifactId>

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
import java.net.URLEncoder;
1818
import java.util.HashMap;
1919
import java.util.Map;
20+
import java.util.Properties;
2021
import java.util.Scanner;
2122
import java.lang.reflect.Type;
2223

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

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

3839
private <T> T execute(String method, String path, Map<String, Object> params,
39-
String payload, Type responseType) throws APIError {
40+
String payload, Type responseType) throws APIError {
4041
URL url = buildUrl(path, params);
4142

4243
try {
@@ -104,9 +105,8 @@ private URL buildUrl(String path, Map<String, Object> params) {
104105
DetectLanguage.apiVersion,
105106
path);
106107

107-
108108
if (params != null && params.size() > 0)
109-
url+= '?' + buildQuery(params);
109+
url += '?' + buildQuery(params);
110110

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

124-
String version = getClass().getPackage().getImplementationVersion();
125-
126-
conn.setRequestProperty("User-Agent", AGENT + '/' + version);
124+
conn.setRequestProperty("User-Agent", AGENT);
127125
conn.setRequestProperty("Accept", "application/json");
128126
conn.setRequestProperty("Authorization", "Bearer " + DetectLanguage.apiKey);
129127

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

184182
private static String getResponseBody(InputStream responseStream)
185183
throws IOException {
186-
//\A is the beginning of
184+
// \A is the beginning of
187185
// the stream boundary
188186
String rBody = new Scanner(responseStream, CHARSET)
189187
.useDelimiter("\\A")
@@ -192,4 +190,19 @@ private static String getResponseBody(InputStream responseStream)
192190
responseStream.close();
193191
return rBody;
194192
}
193+
194+
private static String getVersionFromProperties() {
195+
try {
196+
Properties props = new Properties();
197+
InputStream input = Client.class.getClassLoader().getResourceAsStream("version.properties");
198+
if (input != null) {
199+
props.load(input);
200+
input.close();
201+
return props.getProperty("version", "unknown");
202+
}
203+
} catch (IOException e) {
204+
// Fallback to unknown version
205+
}
206+
return "unknown";
207+
}
195208
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version=@project.version@

src/test/java/com/detectlanguage/DetectLanguageTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void testDetectError() throws APIError {
3737

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

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

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

6262
DetectLanguage.detect(texts);
6363
}

0 commit comments

Comments
 (0)