本文整理汇总了Java中com.hubspot.horizon.HttpRequest类的典型用法代码示例。如果您正苦于以下问题:Java HttpRequest类的具体用法?Java HttpRequest怎么用?Java HttpRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpRequest类属于com.hubspot.horizon包,在下文中一共展示了HttpRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addQueryParams
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
private void addQueryParams(HttpRequest.Builder requestBuilder, Map<String, Object> queryParams) {
for (Entry<String, Object> queryParamEntry : queryParams.entrySet()) {
if (queryParamEntry.getValue() instanceof String) {
requestBuilder.setQueryParam(queryParamEntry.getKey()).to((String) queryParamEntry.getValue());
} else if (queryParamEntry.getValue() instanceof Integer) {
requestBuilder.setQueryParam(queryParamEntry.getKey()).to((Integer) queryParamEntry.getValue());
} else if (queryParamEntry.getValue() instanceof Long) {
requestBuilder.setQueryParam(queryParamEntry.getKey()).to((Long) queryParamEntry.getValue());
} else if (queryParamEntry.getValue() instanceof Boolean) {
requestBuilder.setQueryParam(queryParamEntry.getKey()).to((Boolean) queryParamEntry.getValue());
} else {
throw new RuntimeException(String.format("The type '%s' of query param %s is not supported. Only String, long, int and boolean values are supported",
queryParamEntry.getValue().getClass().getName(), queryParamEntry.getKey()));
}
}
}
示例2: getFromMesos
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
private HttpResponse getFromMesos(String uri) {
HttpResponse response = null;
final long start = System.currentTimeMillis();
LOG.debug("Fetching {} from mesos", uri);
try {
response = httpClient.execute(HttpRequest.newBuilder().setUrl(uri).build());
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;
}
示例3: addQueryParams
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
private void addQueryParams(HttpRequest.Builder requestBuilder, Map<String, ?> queryParams) {
for (Entry<String, ?> queryParamEntry : queryParams.entrySet()) {
if (queryParamEntry.getValue() instanceof String) {
requestBuilder.setQueryParam(queryParamEntry.getKey()).to((String) queryParamEntry.getValue());
} else if (queryParamEntry.getValue() instanceof Integer) {
requestBuilder.setQueryParam(queryParamEntry.getKey()).to((Integer) queryParamEntry.getValue());
} else if (queryParamEntry.getValue() instanceof Long) {
requestBuilder.setQueryParam(queryParamEntry.getKey()).to((Long) queryParamEntry.getValue());
} else if (queryParamEntry.getValue() instanceof Boolean) {
requestBuilder.setQueryParam(queryParamEntry.getKey()).to((Boolean) queryParamEntry.getValue());
} else {
throw new RuntimeException(String.format("The type '%s' of query param %s is not supported. Only String, long, int and boolean values are supported",
queryParamEntry.getValue().getClass().getName(), queryParamEntry.getKey()));
}
}
}
示例4: itRetriesRequestsThatErrorDueToDeadHost
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
@Test
public void itRetriesRequestsThatErrorDueToDeadHost() {
when(httpClient.execute(any()))
.thenReturn(response);
when(response.getStatusCode())
.thenReturn(503)
.thenReturn(200);
when(response.isServerError())
.thenReturn(true)
.thenReturn(false);
singularityClient.pauseSingularityRequest("requestId", Optional.absent());
verify(httpClient, times(2))
.execute(requestCaptor.capture());
HttpRequest sentRequest = requestCaptor.getValue();
assertThat(sentRequest.getUrl().toString())
.matches("http://host(1|2)/singularity/v2/api/requests/request/requestId/pause");
}
示例5: buildNotifyServiceRequest
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
private HttpRequest buildNotifyServiceRequest(String action, boolean addStatusParam) throws AgentServiceNotifyException {
Collection<String> baseUris = workerDatastore.getBaseUris();
if (!baseUris.isEmpty()) {
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.setUrl(String.format(SERVICE_CHECKIN_URL_FORMAT, baseUris.iterator().next(), configuration.getLoadBalancerConfiguration().getName(), action))
.setMethod(HttpRequest.Method.POST)
.setBody(baragonAgentMetadata);
if (addStatusParam) {
requestBuilder.setQueryParam("status").to(true);
}
Map<String, BaragonAuthKey> authKeys = authDatastore.getAuthKeyMap();
if (!authKeys.isEmpty()) {
requestBuilder.setQueryParam("authkey").to(authKeys.entrySet().iterator().next().getValue().getValue());
}
return requestBuilder.build();
} else {
throw new AgentServiceNotifyException("No services available to notify");
}
}
示例6: getGlobalState
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
private Collection<BaragonServiceState> getGlobalState() throws AgentServiceNotifyException {
Collection<String> baseUris = workerDatastore.getBaseUris();
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.setUrl(String.format(GLOBAL_STATE_FORMAT, baseUris.iterator().next()))
.setMethod(Method.GET);
Map<String, BaragonAuthKey> authKeys = authDatastore.getAuthKeyMap();
if (!authKeys.isEmpty()) {
requestBuilder.setQueryParam("authkey").to(authKeys.entrySet().iterator().next().getValue().getValue());
}
HttpRequest request = requestBuilder.build();
HttpResponse response = httpClient.execute(request);
LOG.info(String.format("Got %s response from BaragonService", response.getStatusCode()));
if (response.isError()) {
throw new AgentServiceNotifyException(String.format("Bad response received from BaragonService %s", response.getAsString()));
}
return response.getAs(new TypeReference<Collection<BaragonServiceState>>() {});
}
示例7: buildRequest
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
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;
}
示例8: delete
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
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();
}
示例9: executeRequest
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
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);
}
}
示例10: post
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
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;
}
示例11: getCollectionWithParams
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
private <T> Collection<T> getCollectionWithParams(String uri, String type, Optional<Map<String, Object>> queryParams, TypeReference<Collection<T>> typeReference) {
LOG.info("Getting all {} from {}", type, uri);
final long start = System.currentTimeMillis();
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.setUrl(uri);
if (queryParams.isPresent()) {
addQueryParams(requestBuilder, queryParams.get());
}
addCredentials(requestBuilder);
HttpResponse response = httpClient.execute(requestBuilder.build());
if (response.getStatusCode() == 404) {
return ImmutableList.of();
}
checkResponse(type, response);
LOG.info("Got {} in {}ms", type, System.currentTimeMillis() - start);
return response.getAs(typeReference);
}
示例12: addCredentials
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
private void addCredentials(HttpRequest.Builder requestBuilder) {
if (credentials.isPresent()) {
requestBuilder.addHeader(credentials.get().getHeaderName(), credentials.get().getToken());
}
}
示例13: executeRequest
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
private HttpResponse executeRequest(String uri, String type, Optional<?> body, Method method) {
final long start = System.currentTimeMillis();
HttpRequest.Builder request = HttpRequest.newBuilder().setUrl(uri).setMethod(method);
if (body.isPresent()) {
request.setBody(body.get());
}
addCredentials(request);
HttpResponse response = httpClient.execute(request.build());
checkResponse(type, response);
LOG.info("Successfully {}ed {} in {}ms", method, type, System.currentTimeMillis() - start);
return response;
}
示例14: getState
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
public SingularityState getState(Optional<Boolean> skipCache, Optional<Boolean> includeRequestIds) {
final String uri = String.format(STATE_FORMAT, getHost(), contextPath);
LOG.info("Fetching state from {}", uri);
final long start = System.currentTimeMillis();
HttpRequest.Builder request = HttpRequest.newBuilder().setUrl(uri);
if (skipCache.isPresent()) {
request.setQueryParam("skipCache").to(skipCache.get().booleanValue());
}
if (includeRequestIds.isPresent()) {
request.setQueryParam("includeRequestIds").to(includeRequestIds.get().booleanValue());
}
addCredentials(request);
HttpResponse response = httpClient.execute(request.build());
checkResponse("state", response);
LOG.info("Got state in {}ms", System.currentTimeMillis() - start);
return response.getAs(SingularityState.class);
}
示例15: getSingleWithParams
import com.hubspot.horizon.HttpRequest; //导入依赖的package包/类
private <T> Optional<T> getSingleWithParams(String uri, String type, String id, Optional<Map<String, Object>> queryParams, Class<T> clazz) {
checkNotNull(id, String.format("Provide a %s id", type));
LOG.info("Getting {} {} from {}", type, id, uri);
final long start = System.currentTimeMillis();
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.setUrl(uri);
if (queryParams.isPresent()) {
addQueryParams(requestBuilder, queryParams.get());
}
addCredentials(requestBuilder);
HttpResponse response = httpClient.execute(requestBuilder.build());
if (response.getStatusCode() == 404) {
return Optional.absent();
}
checkResponse(type, response);
LOG.info("Got {} {} in {}ms", type, id, System.currentTimeMillis() - start);
return Optional.fromNullable(response.getAs(clazz));
}