当前位置: 首页>>代码示例>>Java>>正文


Java HttpRequest.Builder方法代码示例

本文整理汇总了Java中com.hubspot.horizon.HttpRequest.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequest.Builder方法的具体用法?Java HttpRequest.Builder怎么用?Java HttpRequest.Builder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.hubspot.horizon.HttpRequest的用法示例。


在下文中一共展示了HttpRequest.Builder方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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()));
    }
  }
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:17,代码来源:SingularityClient.java

示例2: 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()));
    }
  }
}
 
开发者ID:HubSpot,项目名称:Singularity,代码行数:17,代码来源:SingularityClient.java

示例3: 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");
  }
}
 
开发者ID:HubSpot,项目名称:Baragon,代码行数:23,代码来源:LifecycleHelper.java

示例4: 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>>() {});
}
 
开发者ID:HubSpot,项目名称:Baragon,代码行数:20,代码来源:LifecycleHelper.java

示例5: 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;
}
 
开发者ID:HubSpot,项目名称:Baragon,代码行数:18,代码来源:BaragonServiceClient.java

示例6: 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();
}
 
开发者ID:HubSpot,项目名称:Baragon,代码行数:21,代码来源:BaragonServiceClient.java

示例7: 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);
  }
}
 
开发者ID:HubSpot,项目名称:Singularity,代码行数:35,代码来源:SingularityClient.java

示例8: 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;
}
 
开发者ID:HubSpot,项目名称:Baragon,代码行数:15,代码来源:BaragonServiceClient.java

示例9: 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);
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:27,代码来源:SingularityClient.java

示例10: addCredentials

import com.hubspot.horizon.HttpRequest; //导入方法依赖的package包/类
private void addCredentials(HttpRequest.Builder requestBuilder) {
  if (credentials.isPresent()) {
    requestBuilder.addHeader(credentials.get().getHeaderName(), credentials.get().getToken());
  }
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:6,代码来源:SingularityClient.java

示例11: 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;
  }
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:21,代码来源:SingularityClient.java

示例12: 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);
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:27,代码来源:SingularityClient.java

示例13: 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));
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:29,代码来源:SingularityClient.java

示例14: delete

import com.hubspot.horizon.HttpRequest; //导入方法依赖的package包/类
private <T> Optional<T> delete(String uri, String type, String id, Optional<?> body, Optional<Class<T>> clazz) {
  LOG.info("Deleting {} {} from {}", type, id, uri);

  final long start = System.currentTimeMillis();

  HttpRequest.Builder request = HttpRequest.newBuilder().setUrl(uri).setMethod(Method.DELETE);

  if (body.isPresent()) {
    request.setBody(body.get());
  }

  addCredentials(request);

  HttpResponse response = httpClient.execute(request.build());

  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();
}
 
开发者ID:PacktPublishing,项目名称:Mastering-Mesos,代码行数:31,代码来源:SingularityClient.java


注:本文中的com.hubspot.horizon.HttpRequest.Builder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。