Skip to content

feat: do not close or manage lifecycle of http-client passed in #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set default value to false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard this comment. The default value will be false. That is sufficient.


/**
* 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