1212import org .apache .http .Header ;
1313import org .apache .http .StatusLine ;
1414import org .apache .http .annotation .NotThreadSafe ;
15+ import org .apache .http .client .ClientProtocolException ;
1516import org .apache .http .client .ResponseHandler ;
1617import org .apache .http .client .methods .CloseableHttpResponse ;
1718import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
@@ -277,23 +278,21 @@ public Response delete(Request request) throws URISyntaxException, IOException {
277278 }
278279
279280 private Response executeApiCall (HttpRequestBase httpPost ) throws IOException {
280- CloseableHttpResponse serverResponse = null ;
281- Response response = new Response ();
282281 try {
283- serverResponse = httpClient .execute (httpPost );
284- response = getResponse (serverResponse );
285- final StatusLine statusLine = serverResponse .getStatusLine ();
286- if (statusLine .getStatusCode ()>=300 ){
287- //throwing IOException here to not break API behavior.
288- throw new IOException ("Request returned status Code " +statusLine .getStatusCode ()+"Body:" +(response !=null ?response .getBody ():null ));
289- }
290-
291- } finally {
292- if (serverResponse != null ) {
282+ CloseableHttpResponse serverResponse = httpClient .execute (httpPost );
283+ try {
284+ Response response = getResponse (serverResponse );
285+ if (response .getStatusCode () >= 300 ) {
286+ //throwing IOException here to not break API behavior.
287+ throw new IOException ("Request returned status Code " +response .getStatusCode ()+"Body:" +response .getBody ());
288+ }
289+ return response ;
290+ } finally {
293291 serverResponse .close ();
294292 }
293+ } catch (ClientProtocolException e ) {
294+ throw new IOException (e .getMessage ());
295295 }
296- return response ;
297296 }
298297
299298 /**
@@ -326,4 +325,15 @@ public Response api(Request request) throws IOException {
326325 throw new IOException (errors .toString ());
327326 }
328327 }
329- }
328+
329+ @ Override
330+ public void finalize () throws Throwable {
331+ try {
332+ this .httpClient .close ();
333+ } catch (IOException e ) {
334+ throw new Throwable (e .getMessage ());
335+ } finally {
336+ super .finalize ();
337+ }
338+ }
339+ }
0 commit comments