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 <T> Optional<T> delete(String uri, String type, String id, Map<String, String> queryParams, Optional<Class<T>> clazz) { LOG.debug("Deleting {} {} from {}", type, id, uri); final long start = System.currentTimeMillis(); HttpRequest.Builder request = buildRequest(uri, queryParams).setMethod(Method.DELETE); HttpResponse response = httpClient.execute(request.build()); if (response.getStatusCode() == 404) { LOG.debug("{} ({}) was not found", type, id); return Optional.absent(); } checkResponse(type, response); LOG.debug("Deleted {} ({}) from Baragon in %sms", type, id, System.currentTimeMillis() - start); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } return Optional.absent(); }
private HttpResponse post(String uri, String type, Optional<?> body, Map<String, String> params) { LOG.debug("Posting {} to {}", type, uri); final long start = System.currentTimeMillis(); HttpRequest.Builder request = buildRequest(uri, params).setMethod(Method.POST); if (body.isPresent()) { request.setBody(body.get()); } HttpResponse response = httpClient.execute(request.build()); checkResponse(type, response); LOG.debug("Successfully posted {} in {}ms", type, System.currentTimeMillis() - start); return response; }
private HttpRequest.Builder buildBaseSlackPost(SlackMethod method) { return HttpRequest.newBuilder() .setMethod(Method.POST) .setUrl(config.getSlackApiBasePath().get() + "/" + method.getMethod()); }
private HttpResponse put(String uri, String type, Map<String, String> params) { LOG.debug("Posting {} to {}", type, uri); final long start = System.currentTimeMillis(); HttpRequest.Builder request = buildRequest(uri, params).setMethod(Method.PUT); HttpResponse response = httpClient.execute(request.build()); checkResponse(type, response); LOG.debug("Successfully posted {} in {}ms", type, System.currentTimeMillis() - start); return response; }