co.cask.common.http.HttpRequest.post()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(13.2k)|赞(0)|评价(0)|浏览(117)

本文整理了Java中co.cask.common.http.HttpRequest.post()方法的一些代码示例,展示了HttpRequest.post()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.post()方法的具体详情如下:
包路径:co.cask.common.http.HttpRequest
类名称:HttpRequest
方法名:post

HttpRequest.post介绍

暂无

代码示例

代码示例来源:origin: caskdata/cdap

private void revoke(RevokeRequest revokeRequest)
 throws IOException, UnauthenticatedException, FeatureDisabledException, UnauthorizedException, NotFoundException {
 URL url = config.resolveURLV3(AUTHORIZATION_BASE + "/privileges/revoke");
 HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(revokeRequest)).build();
 executePrivilegeRequest(request);
}

代码示例来源:origin: caskdata/cdap

@Override
public void grant(Authorizable authorizable, Principal principal, Set<Action> actions) throws IOException,
 UnauthorizedException, UnauthenticatedException, NotFoundException, FeatureDisabledException {
 GrantRequest grantRequest = new GrantRequest(authorizable, principal, actions);
 URL url = config.resolveURLV3(AUTHORIZATION_BASE + "/privileges/grant");
 HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(grantRequest)).build();
 executePrivilegeRequest(request);
}

代码示例来源:origin: caskdata/cdap

/**
 * Gets the status of multiple programs.
 *
 * @param namespace the namespace of the programs
 * @param programs the list of programs to get status for
 * @return the status of each program
 */
public List<BatchProgramStatus> getStatus(NamespaceId namespace, List<BatchProgram> programs)
 throws IOException, UnauthenticatedException, UnauthorizedException {
 URL url = config.resolveNamespacedURLV3(namespace, "status");
 HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(programs)).build();
 HttpResponse response = restClient.execute(request, config.getAccessToken());
 return ObjectResponse.<List<BatchProgramStatus>>fromJsonBody(response, BATCH_STATUS_RESPONSE_TYPE, GSON)
  .getResponseObject();
}

代码示例来源:origin: caskdata/cdap

/**
 * Creates an application with a version using an existing artifact.
 *
 * @param appId the id of the application to add
 * @param createRequest the request body, which contains the artifact to use and any application config
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public void deploy(ApplicationId appId, AppRequest<?> createRequest) throws IOException, UnauthenticatedException {
 URL url = config.resolveNamespacedURLV3(new NamespaceId(appId.getNamespace()),
                     String.format("apps/%s/versions/%s/create",
                            appId.getApplication(), appId.getVersion()));
 HttpRequest request = HttpRequest.post(url)
  .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
  .withBody(GSON.toJson(createRequest))
  .build();
 restClient.upload(request, config.getAccessToken());
}

代码示例来源:origin: cdapio/cdap

private void add(URL serviceUrl, Collection<CubeFact> facts) throws IOException {
 URL url = new URL(serviceUrl, "add");
 HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(facts)).build();
 HttpResponse response = HttpRequests.execute(request);
 Assert.assertEquals(200, response.getResponseCode());
}

代码示例来源:origin: cdapio/cdap

protected static HttpResponse doPost(String resource, @Nullable String body,
                   @Nullable Map<String, String> headers) throws Exception {
 HttpRequest.Builder builder = HttpRequest.post(getEndPoint(resource).toURL());
 if (headers != null) {
  builder.addHeaders(headers);
 }
 builder.addHeader(Constants.Gateway.API_KEY, API_KEY);
 if (body != null) {
  builder.withBody(body);
 }
 return HttpRequests.execute(builder.build(), httpRequestConfig);
}

代码示例来源:origin: caskdata/cdap

private void deployApp(NamespaceId namespace, File jarFile, Map<String, String> headers)
 throws IOException, UnauthenticatedException {
 URL url = config.resolveNamespacedURLV3(namespace, "apps");
 HttpRequest request = HttpRequest.post(url)
  .addHeaders(headers)
  .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM)
  .withBody(jarFile).build();
 restClient.upload(request, config.getAccessToken());
}

代码示例来源:origin: caskdata/cdap

/**
 * Update an existing app to use a different artifact version or config.
 *
 * @param appId the id of the application to update
 * @param updateRequest the request to update the application with
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws NotFoundException if the app or requested artifact could not be found
 * @throws BadRequestException if the request is invalid
 */
public void update(ApplicationId appId, AppRequest<?> updateRequest)
 throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
 URL url = config.resolveNamespacedURLV3(appId.getParent(), String.format("apps/%s/update", appId.getApplication()));
 HttpRequest request = HttpRequest.post(url)
  .withBody(GSON.toJson(updateRequest))
  .build();
 HttpResponse response = restClient.execute(request, config.getAccessToken(),
                       HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
 int responseCode = response.getResponseCode();
 if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
  throw new NotFoundException("app or app artifact");
 } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
  throw new BadRequestException(String.format("Bad Request. Reason: %s",
                        response.getResponseBodyAsString()));
 }
}

代码示例来源:origin: caskdata/cdap

private void doUpdate(ScheduleId scheduleId, String json) throws IOException,
 UnauthenticatedException, NotFoundException, UnauthorizedException, AlreadyExistsException {
 String path = String.format("apps/%s/versions/%s/schedules/%s/update",
               scheduleId.getApplication(), scheduleId.getVersion(), scheduleId.getSchedule());
 URL url = config.resolveNamespacedURLV3(scheduleId.getNamespaceId(), path);
 HttpRequest request = HttpRequest.post(url).withBody(json).build();
 HttpResponse response = restClient.execute(request, config.getAccessToken(),
                       HttpURLConnection.HTTP_NOT_FOUND);
 if (HttpURLConnection.HTTP_NOT_FOUND == response.getResponseCode()) {
  throw new NotFoundException(scheduleId);
 }
}

代码示例来源:origin: cdapio/cdap

private Collection<DimensionValue> searchDimensionValue(URL serviceUrl, CubeExploreQuery query) throws IOException {
 URL url = new URL(serviceUrl, "searchDimensionValue");
 HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(query)).build();
 HttpResponse response = HttpRequests.execute(request);
 Assert.assertEquals(200, response.getResponseCode());
 return GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<Collection<DimensionValue>>() { }.getType());
}

代码示例来源:origin: cdapio/cdap

private Collection<TimeSeries> query(URL serviceUrl, CubeQuery query) throws IOException {
  URL url = new URL(serviceUrl, "query");
  HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(query)).build();
  HttpResponse response = HttpRequests.execute(request);
  Assert.assertEquals(200, response.getResponseCode());
  return GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<Collection<TimeSeries>>() { }.getType());
 }
}

代码示例来源:origin: cdapio/cdap

private Collection<String> searchMeasure(URL serviceUrl, CubeExploreQuery query) throws IOException {
 URL url = new URL(serviceUrl, "searchMeasure");
 HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(query)).build();
 HttpResponse response = HttpRequests.execute(request);
 Assert.assertEquals(200, response.getResponseCode());
 return GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<Collection<String>>() { }.getType());
}

代码示例来源:origin: caskdata/cdap

/**
 * Starts a program using specified runtime arguments.
 *
 * @param program the program to start
 * @param debug true to start in debug mode
 * @param runtimeArgs runtime arguments to pass to the program
 * @throws IOException
 * @throws ProgramNotFoundException
 * @throws UnauthenticatedException
 * @throws UnauthorizedException
 */
public void start(ProgramId program, boolean debug, @Nullable Map<String, String> runtimeArgs) throws IOException,
 ProgramNotFoundException, UnauthenticatedException, UnauthorizedException {
 String action = debug ? "debug" :  "start";
 String path = String.format("apps/%s/versions/%s/%s/%s/%s", program.getApplication(), program.getVersion(),
               program.getType().getCategoryName(), program.getProgram(), action);
 URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
 HttpRequest.Builder request = HttpRequest.post(url);
 if (runtimeArgs != null) {
  request.withBody(GSON.toJson(runtimeArgs));
 }
 HttpResponse response = restClient.execute(request.build(), config.getAccessToken(),
                       HttpURLConnection.HTTP_NOT_FOUND);
 if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
  throw new ProgramNotFoundException(program);
 }
}

代码示例来源:origin: cdapio/cdap

protected HttpResponse deploy(ApplicationId appId, AppRequest<? extends Config> appRequest) throws Exception {
 String deployPath = getVersionedAPIPath(String.format("apps/%s/versions/%s/create", appId.getApplication(),
                            appId.getVersion()),
                     appId.getNamespace());
 return executeDeploy(HttpRequest.post(getEndPoint(deployPath).toURL()), appRequest);
}

代码示例来源:origin: caskdata/cdap

private ObjectResponse<List<DatasetSpecificationSummary>> getInstancesWithProperties(String namespace,
                                           Map<String, String> properties)
 throws IOException {
 HttpRequest request = HttpRequest.post(getUrl(namespace, "/data/datasets"))
  .withBody(GSON.toJson(properties)).build();
 return ObjectResponse.fromJsonBody(HttpRequests.execute(request),
                   new TypeToken<List<DatasetSpecificationSummary>>() { }.getType());
}

代码示例来源:origin: caskdata/cdap

private void validatePost(URL url, String body, Integer ... expected) throws IOException {
 HttpRequest request = HttpRequest.post(url).withBody(body).build();
 assertStatus(HttpRequests.execute(request).getResponseCode(), url, expected);
}

代码示例来源:origin: cdapio/cdap

/**
 * This method accepts GSON serialized form of plugin classes for testing purpose.
 */
private HttpResponse addArtifact(Id.Artifact artifactId, InputSupplier<? extends InputStream> artifactContents,
                 Set<ArtifactRange> parents, @Nullable String pluginClassesJSON)
 throws Exception {
 String path = getVersionedAPIPath("artifacts/" + artifactId.getName(), artifactId.getNamespace().getId());
 HttpRequest.Builder builder = HttpRequest.post(getEndPoint(path).toURL())
  .addHeader(Constants.Gateway.API_KEY, "api-key-example")
  .addHeader("Artifact-Version", artifactId.getVersion().getVersion());
 if (parents != null && !parents.isEmpty()) {
  builder.addHeader("Artifact-Extends", Joiner.on('/').join(parents));
 }
 // Note: we purposefully do not check for empty string and let it pass as the header because we want to test the
 // behavior where plugin classes header is set to empty string. This is what is passed by the UI. For more
 // details see: https://issues.cask.co/browse/CDAP-14578
 if (pluginClassesJSON != null) {
  builder.addHeader("Artifact-Plugins", pluginClassesJSON);
 }
 builder.withBody(artifactContents);
 return HttpRequests.execute(builder.build(), httpRequestConfig);
}

代码示例来源:origin: cdapio/cdap

private co.cask.common.http.HttpResponse callPluginMethod(
 ArtifactId plugins3Id, String pluginType, String pluginName, String pluginMethod,
 String body, ArtifactScope scope, int expectedResponseCode) throws URISyntaxException, IOException {
 URL endpoint = getEndPoint(
  String.format("%s/namespaces/%s/artifacts/%s/versions/%s/plugintypes/%s/plugins/%s/methods/%s?scope=%s",
         Constants.Gateway.API_VERSION_3,
         plugins3Id.getNamespace(),
         plugins3Id.getArtifact(),
         plugins3Id.getVersion(),
         pluginType,
         pluginName,
         pluginMethod,
         scope.name()))
  .toURL();
 HttpRequest request = HttpRequest.post(endpoint).withBody(body).build();
 co.cask.common.http.HttpResponse response = HttpRequests.execute(request);
 Assert.assertEquals(expectedResponseCode, response.getResponseCode());
 return response;
}

代码示例来源:origin: caskdata/cdap

private void testAdminOp(DatasetId datasetInstanceId, String opName, int expectedStatus,
             Object expectedResult)
 throws IOException {
 String path = String.format("/namespaces/%s/data/datasets/%s/admin/%s",
               datasetInstanceId.getNamespace(), datasetInstanceId.getEntityName(), opName);
 URL targetUrl = resolve(path);
 HttpResponse response = HttpRequests.execute(HttpRequest.post(targetUrl).build());
 DatasetAdminOpResponse body = getResponse(response.getResponseBody());
 Assert.assertEquals(expectedStatus, response.getResponseCode());
 Assert.assertEquals(expectedResult, body.getResult());
}

代码示例来源:origin: cdapio/cdap

@Category(XSlowTests.class)
@Test
public void testByteCodeClassLoader() throws Exception {
 // This test verify bytecode generated classes ClassLoading
 ApplicationManager appManager = deployApplication(testSpace, ClassLoaderTestApp.class);
 ServiceManager serviceManager = appManager.getServiceManager("RecordHandler").start();
 URL serviceURL = serviceManager.getServiceURL(15, TimeUnit.SECONDS);
 Assert.assertNotNull(serviceURL);
 // Increment record
 URL url = new URL(serviceURL, "increment/public");
 for (int i = 0; i < 10; i++) {
  HttpResponse response = HttpRequests.execute(HttpRequest.post(url).build());
  Assert.assertEquals(200, response.getResponseCode());
 }
 // Query record
 url = new URL(serviceURL, "query?type=public");
 HttpRequest request = HttpRequest.get(url).build();
 HttpResponse response = HttpRequests.execute(request);
 Assert.assertEquals(200, response.getResponseCode());
 long count = Long.parseLong(response.getResponseBodyAsString());
 serviceManager.stop();
 // Verify the record count with dataset
 DataSetManager<KeyValueTable> recordsManager = getDataset(testSpace.dataset("records"));
 KeyValueTable records = recordsManager.get();
 Assert.assertEquals(count, Bytes.toLong(records.read("PUBLIC")));
}

相关文章