Skip to content

Commit

Permalink
Merge pull request #25 from maxxedev/master
Browse files Browse the repository at this point in the history
do not close or manage lifecycle of http-client passed in
  • Loading branch information
thinkingserious authored Oct 31, 2017
2 parents 4c8607d + c43dede commit 49a12eb
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/main/java/com/sendgrid/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.StatusLine;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
Expand All @@ -31,6 +30,7 @@
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";

@Override
public String getMethod() {
return METHOD_NAME;
}
Expand All @@ -48,16 +48,20 @@ public class Client {

private CloseableHttpClient httpClient;
private Boolean test;
private boolean createdHttpClient;

/**
* Constructor for using the default CloseableHttpClient.
*/
public Client() {
this(false);
this.httpClient = HttpClients.createDefault();
this.test = false;
this.createdHttpClient = true;
}

/**
* Constructor for passing in an httpClient.
* Constructor for passing in an httpClient, typically for mocking. Passed-in httpClient will not be closed
* by this Client.
*
* @param httpClient
* an Apache CloseableHttpClient
Expand Down Expand Up @@ -87,6 +91,7 @@ public Client(Boolean test) {
public Client(CloseableHttpClient httpClient, Boolean test) {
this.httpClient = httpClient;
this.test = test;
this.createdHttpClient = true;
}


Expand Down Expand Up @@ -340,7 +345,9 @@ public Response api(Request request) throws IOException {
@Override
public void finalize() throws Throwable {
try {
this.httpClient.close();
if(this.createdHttpClient) {
this.httpClient.close();
}
} catch(IOException e) {
throw new Throwable(e.getMessage());
} finally {
Expand Down

0 comments on commit 49a12eb

Please sign in to comment.