private HttpResponse postWithParams(Function<String, String> hostToUri, String type, Optional<?> body, Optional<Map<String, Object>> queryParams) { return executeRequest(hostToUri, type, body, Method.POST, queryParams); }
private HttpResponse post(Function<String, String> hostToUri, String type, Optional<?> body) { return executeRequest(hostToUri, type, body, Method.POST, Optional.absent()); }
private HttpResponse post(Function<String, String> hostToUri, String type, Optional<?> body, Map<String, Object> queryParams) { return executeRequest(hostToUri, type, body, Method.POST, Optional.of(queryParams)); }
private HttpResponse put(Function<String, String> hostToUri, String type, Optional<?> body) { return executeRequest(hostToUri, type, body, Method.PUT, Optional.absent()); }
private HttpResponse executeGetSingleWithParams(Function<String, String> hostToUrl, String type, String id, Optional<Map<String, Object>> queryParams) { checkNotNull(id, String.format("Provide a %s id", type)); LOG.info("Getting {} {} from Singularity host", type, id); return executeRequest(hostToUrl, Method.GET, Optional.absent(), queryParams.or(Collections.emptyMap())); }
private <T> Optional<T> post(Function<String, String> hostToUri, String type, Optional<?> body, Optional<Class<T>> clazz) { try { HttpResponse response = executeRequest(hostToUri, type, body, Method.POST, Optional.absent()); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } } catch (Exception e) { LOG.warn("Http post failed", e); } return Optional.absent(); }
private HttpResponse executeRequest(Function<String, String> hostToUri, String type, Optional<?> body, Method method, Optional<Map<String, Object>> queryParams) { final long start = System.currentTimeMillis(); HttpResponse response = executeRequest(hostToUri, method, body, queryParams.or(Collections.emptyMap())); checkResponse(type, response); LOG.info("Successfully {}ed {} in {}ms", method, type, System.currentTimeMillis() - start); return response; }
private <T> Collection<T> getCollectionWithParams(Function<String, String> hostToUrl, String type, Optional<Map<String, Object>> queryParams, TypeReference<Collection<T>> typeReference) { final long start = System.currentTimeMillis(); HttpResponse response = executeRequest(hostToUrl, Method.GET, Optional.absent(), queryParams.or(Collections.emptyMap())); if (response.getStatusCode() == 404) { return ImmutableList.of(); } checkResponse(type, response); LOG.info("Got {} in {}ms", type, System.currentTimeMillis() - start); return response.getAs(typeReference); }
private <T> Optional<T> deleteWithParams(Function<String, String> hostToUrl, String type, String id, Optional<?> body, Optional<Map<String, Object>> queryParams, Optional<Class<T>> clazz) { LOG.info("Deleting {} {} from Singularity", type, id); final long start = System.currentTimeMillis(); HttpResponse response = executeRequest(hostToUrl, Method.DELETE, body, queryParams.or(Collections.emptyMap())); if (response.getStatusCode() == 404) { LOG.info("{} ({}) was not found", type, id); return Optional.absent(); } checkResponse(type, response); LOG.info("Deleted {} ({}) from Singularity in %sms", type, id, System.currentTimeMillis() - start); if (clazz.isPresent()) { return Optional.of(response.getAs(clazz.get())); } return Optional.absent(); }
public Optional<SingularityTaskReconciliationStatistics> getTaskReconciliationStatistics() { final Function<String, String> uri = (host) -> String.format(TASK_RECONCILIATION_FORMAT, getApiBase(host)); LOG.info("Fetch task reconciliation statistics from {}", uri); final long start = System.currentTimeMillis(); HttpResponse response = executeRequest(uri, Method.GET, Optional.absent(), Collections.emptyMap()); if (response.getStatusCode() == 404) { return Optional.absent(); } checkResponse("task reconciliation statistics", response); LOG.info("Got task reconciliation statistics in {}ms", System.currentTimeMillis() - start); return Optional.of(response.getAs(SingularityTaskReconciliationStatistics.class)); }
public SingularityState getState(Optional<Boolean> skipCache, Optional<Boolean> includeRequestIds) { final Function<String, String> uri = (host) -> String.format(STATE_FORMAT, getApiBase(host)); LOG.info("Fetching state from {}", uri); final long start = System.currentTimeMillis(); Map<String, Boolean> queryParams = new HashMap<>(); if (skipCache.isPresent()) { queryParams.put("skipCache", skipCache.get()); } if (includeRequestIds.isPresent()) { queryParams.put("includeRequestIds", includeRequestIds.get()); } HttpResponse response = executeRequest(uri, Method.GET, Optional.absent(), queryParams); checkResponse("state", response); LOG.info("Got state in {}ms", System.currentTimeMillis() - start); return response.getAs(SingularityState.class); }