Skip to content
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

Allow customization and closing of HttpClient #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 14 additions & 3 deletions src/main/java/com/justinsb/etcd/EtcdClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.justinsb.etcd;

import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
Expand Down Expand Up @@ -36,9 +37,9 @@
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;

public class EtcdClient {
static final CloseableHttpAsyncClient httpClient = buildDefaultHttpClient();
static final Gson gson = new GsonBuilder().create();
public class EtcdClient implements Closeable {
private final CloseableHttpAsyncClient httpClient;
private static final Gson gson = new GsonBuilder().create();

static CloseableHttpAsyncClient buildDefaultHttpClient() {
// TODO: Increase timeout??
Expand All @@ -51,6 +52,11 @@ static CloseableHttpAsyncClient buildDefaultHttpClient() {
final URI baseUri;

public EtcdClient(URI baseUri) {
this(buildDefaultHttpClient(), baseUri);
}

public EtcdClient(CloseableHttpAsyncClient httpClient, URI baseUri) {
this.httpClient = httpClient;
String uri = baseUri.toString();
if (!uri.endsWith("/")) {
uri += "/";
Expand Down Expand Up @@ -443,4 +449,9 @@ public static String format(Object o) {
}
}

@Override
public void close() throws IOException
{
httpClient.close();
}
}