private HttpResponse executeRequest(Function<String, String> hostToUri, Method method, Optional<?> body, Map<String, ?> queryParams) {
HttpRequest.Builder request = HttpRequest.newBuilder().setMethod(method);
if (body.isPresent()) {
request.setBody(body.get());
}
addQueryParams(request, queryParams);
addCredentials(request);
List<String> hosts = new ArrayList<>(hostsProvider.get());
request
.setRetryStrategy(RetryStrategy.NEVER_RETRY)
.setMaxRetries(1);
try {
return httpResponseRetryer.call(() -> {
if (hosts.isEmpty()) {
hosts.addAll(hostsProvider.get());
}
int selection = random.nextInt(hosts.size());
String host = hosts.get(selection);
String url = hostToUri.apply(host);
hosts.remove(selection);
LOG.info("Making {} request to {}", method, url);
request.setUrl(url);
return httpClient.execute(request.build());
});
} catch (ExecutionException | RetryException exn) {
throw new SingularityClientException("Failed request to Singularity", exn);
}
}