1818import java .util .HashMap ;
1919import java .util .Map ;
2020import java .util .Scanner ;
21+ import java .lang .reflect .Type ;
2122
2223public 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 (Type responseType , 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 ;
0 commit comments