8
8
import com .google .gson .annotations .SerializedName ;
9
9
import com .route4me .sdk .exception .APIException ;
10
10
import com .route4me .sdk .queryconverter .QueryConverter ;
11
+ import java .io .*;
12
+ import java .lang .reflect .Type ;
13
+ import java .net .URISyntaxException ;
14
+ import java .util .List ;
11
15
import org .apache .http .HttpEntity ;
16
+ import org .apache .http .HttpHost ;
12
17
import org .apache .http .NameValuePair ;
18
+ import org .apache .http .client .config .RequestConfig ;
13
19
import org .apache .http .client .entity .UrlEncodedFormEntity ;
14
20
import org .apache .http .client .methods .CloseableHttpResponse ;
15
21
import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
16
22
import org .apache .http .client .methods .HttpRequestBase ;
17
23
import org .apache .http .client .utils .URIBuilder ;
18
24
import org .apache .http .entity .StringEntity ;
19
- import org .apache .http .impl .client .HttpClients ;
20
-
21
- import java .io .*;
22
- import java .lang .reflect .Type ;
23
- import java .net .URISyntaxException ;
24
- import java .util .List ;
25
- import org .apache .http .HttpHost ;
26
- import org .apache .http .client .config .RequestConfig ;
27
25
import org .apache .http .impl .client .CloseableHttpClient ;
26
+ import org .apache .http .impl .client .HttpClients ;
28
27
import org .apache .logging .log4j .LogManager ;
29
28
30
29
public abstract class Manager {
31
-
32
- protected static final org .apache .logging .log4j .Logger logger = LogManager .getLogger (Manager .class );
33
30
31
+ protected static final org .apache .logging .log4j .Logger logger = LogManager .getLogger (Manager .class );
34
32
35
33
private final String apiKey ;
36
34
protected Gson gson ;
@@ -184,7 +182,9 @@ private <T> T makeRequest(RequestMethod method, URIBuilder builder, HttpEntity b
184
182
builder .addParameter ("redirect" , "0" );
185
183
client = HttpClients .custom ().disableRedirectHandling ().build ();
186
184
}
187
- builder .addParameter ("api_key" , this .apiKey );
185
+ if (this .apiKey != null ) {
186
+ builder .addParameter ("api_key" , this .apiKey );
187
+ }
188
188
HttpRequestBase hrb = method .create (builder .build ());
189
189
if (requestContentType != null ) {
190
190
hrb .setHeader ("Content-type" , requestContentType );
@@ -201,12 +201,12 @@ private <T> T makeRequest(RequestMethod method, URIBuilder builder, HttpEntity b
201
201
}
202
202
//try with resources to close streams
203
203
204
- try ( CloseableHttpResponse resp = client .execute (hrb ); InputStream is = resp .getEntity ().getContent ()) {
204
+ try (CloseableHttpResponse resp = client .execute (hrb ); InputStream is = resp .getEntity ().getContent ()) {
205
205
//response should always be present
206
206
if (is == null ) {
207
207
throw new APIException ("Response body is null." );
208
208
}
209
- try ( InputStreamReader isr = new InputStreamReader (is ); BufferedReader br = new BufferedReader (isr )) {
209
+ try (InputStreamReader isr = new InputStreamReader (is ); BufferedReader br = new BufferedReader (isr )) {
210
210
if (resp .getStatusLine ().getStatusCode () < 200 || resp .getStatusLine ().getStatusCode () > 303 ) {
211
211
try {
212
212
StringBuilder respString = responseContentParser (br );
@@ -218,11 +218,10 @@ private <T> T makeRequest(RequestMethod method, URIBuilder builder, HttpEntity b
218
218
//deserialize json into an object
219
219
if (clazz != null && clazz != String .class ) {
220
220
StringBuilder respString = responseContentParser (br );
221
-
222
221
try {
223
222
return this .gson .fromJson (respString .toString (), clazz );
224
223
} catch (JsonSyntaxException ex ) {
225
- throw new APIException (respString .toString ());
224
+ throw new APIException (ex .toString ());
226
225
}
227
226
} else if (type != null ) {
228
227
StringBuilder respString = responseContentParser (br );
0 commit comments