
pl.allegro.tech.embeddedelasticsearch.HttpClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of embedded-elasticsearch Show documentation
Show all versions of embedded-elasticsearch Show documentation
Tool that ease up creation of integration tests with Elasticsearch
package pl.allegro.tech.embeddedelasticsearch;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.util.function.Consumer;
import java.util.function.Function;
class HttpClient {
private static final Consumer noop = x -> {};
private final CloseableHttpClient internalHttpClient = HttpClients.createDefault();
void execute(HttpRequestBase request) {
execute(request, noop);
}
void execute(HttpRequestBase request, Consumer block) {
execute(request, (Function) response -> {
block.accept(response);
return null;
});
}
T execute(HttpRequestBase request, Function block) {
try (CloseableHttpResponse response = internalHttpClient.execute(request)) {
return block.apply(response);
} catch (IOException e) {
throw new HttpRequestException(e);
} finally {
request.releaseConnection();
}
}
static class HttpRequestException extends RuntimeException {
HttpRequestException(IOException cause) {
super(cause);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy