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 <T> Optional<T> getSingle(String uri, String type, String id, Class<T> clazz, Map<String, String> queryParams) { checkNotNull(id, String.format("Provide a %s id", type)); LOG.debug("Getting {} {} from {}", type, id, uri); final long start = System.currentTimeMillis(); HttpResponse response = httpClient.execute(buildRequest(uri, queryParams).build()); if (response.getStatusCode() == 404) { return Optional.absent(); } checkResponse(type, response); LOG.debug("Got {} {} in {}ms", type, id, System.currentTimeMillis() - start); return Optional.fromNullable(response.getAs(clazz)); }
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 <T extends SlackResponse> CompletableFuture<Result<T, SlackError>> postSlackCommandJsonEncoded( SlackMethod method, Object params, Class<T> responseType ) { HttpRequest request = buildBaseSlackPost(method) .setContentType(ContentType.JSON) .setBody(params) .addHeader("Authorization", "Bearer " + config.getTokenSupplier().get()) .build(); return executeLoggedAs(method, request, responseType); }
private <T extends SlackResponse> CompletableFuture<Result<T, SlackError>> postSlackCommandUrlEncoded( SlackMethod method, Multimap<String, String> params, Class<T> responseType ) { HttpRequest.Builder requestBuilder = buildBaseSlackPost(method) .setContentType(ContentType.FORM); params.entries() .forEach(param -> requestBuilder.setFormParam(param.getKey()).to(param.getValue())); requestBuilder.setQueryParam("token").to(config.getTokenSupplier().get()); return executeLoggedAs(method, requestBuilder.build(), responseType); }
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 <T> Collection<T> getCollection(String uri, String type, TypeReference<Collection<T>> typeReference) { LOG.debug("Getting all {} from {}", type, uri); final long start = System.currentTimeMillis(); HttpResponse response = httpClient.execute(buildRequest(uri).build()); if (response.getStatusCode() == 404) { throw new BaragonClientException(String.format("%s not found", type), 404); } checkResponse(type, response); LOG.debug("Got {} in {}ms", type, System.currentTimeMillis() - start); return response.getAs(typeReference); }
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; }