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()) { // We've tried everything we started with. Look again. 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); } }
private HttpResponse getFromMesos(String uri, boolean useShortTimeout) { HttpClient currentHttpClient = useShortTimeout ? shortTimeoutHttpClient : httpClient; HttpResponse response = null; final long start = System.currentTimeMillis(); LOG.debug("Fetching {} from mesos", uri); try { response = currentHttpClient.execute(HttpRequest.newBuilder().setUrl(uri).build(), new Options()); LOG.debug("Response {} - {} after {}", response.getStatusCode(), uri, JavaUtils.duration(start)); } catch (Exception e) { throw new MesosClientException(String.format("Exception fetching %s after %s", uri, JavaUtils.duration(start)), e); } if (!response.isSuccess()) { throw new MesosClientException(String.format("Invalid response code from %s : %s", uri, response.getStatusCode())); } return response; }
private HttpRequest.Builder buildRequest(String uri, Map<String, String> queryParams) { final HttpRequest.Builder builder = HttpRequest.newBuilder().setUrl(uri); final Optional<String> maybeAuthkey = authkeyProvider.get(); if (maybeAuthkey.isPresent()) { builder.setQueryParam("authkey").to(maybeAuthkey.get()); } if ((queryParams != null) && (!queryParams.isEmpty())) { for (Map.Entry<String, String> entry : queryParams.entrySet()) { builder.setQueryParam(entry.getKey()).to(entry.getValue()); } } return builder; }
private HttpRequest.Builder buildBaseSlackPost(SlackMethod method) { return HttpRequest.newBuilder() .setMethod(Method.POST) .setUrl(config.getSlackApiBasePath().get() + "/" + method.getMethod()); }