1717import java .net .URLEncoder ;
1818import java .util .HashMap ;
1919import java .util .Map ;
20+ import java .util .Properties ;
2021import java .util .Scanner ;
2122import java .lang .reflect .Type ;
2223
2324public 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}
0 commit comments