Search in sources :

Example 91 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class StreamClient method setDescription.

/**
   * Sets the description of a stream.
   *
   * @param stream ID of the stream
   * @param description description of the stream
   * @throws IOException if a network error occurred
   * @throws StreamNotFoundException if the stream with the specified ID was not found
   */
public void setDescription(StreamId stream, String description) throws IOException, StreamNotFoundException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(stream.getParent(), String.format("streams/%s/properties", stream.getStream()));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(ImmutableMap.of("description", description))).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new StreamNotFoundException(stream);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) URL(java.net.URL)

Example 92 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class StreamViewClient method createOrUpdate.

/**
   * Creates or updates a view.
   *
   * @param id the view
   * @param viewSpecification the view config
   * @return true if a view was created, false if updated
   */
public boolean createOrUpdate(StreamViewId id, ViewSpecification viewSpecification) throws NotFoundException, IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(id.getParent().getParent(), String.format("streams/%s/views/%s", id.getStream(), id.getView()));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(viewSpecification)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken());
    return response.getResponseCode() == 201;
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Example 93 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class ProgramClient method setWorkerInstances.

/**
   * Sets the number of instances that a worker will run on.
   *
   * @param instances number of instances for the worker to run on
   * @throws IOException if a network error occurred
   * @throws NotFoundException if the application or worker could not be found
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public void setWorkerInstances(ProgramId worker, int instances) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(worker.getNamespaceId(), String.format("apps/%s/workers/%s/instances", worker.getApplication(), worker.getProgram()));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(worker);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) Instances(co.cask.cdap.proto.Instances) HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) URL(java.net.URL)

Example 94 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class ProgramClient method start.

/**
   * Starts a batch of programs in the same call.
   *
   * @param namespace the namespace of the programs
   * @param programs the programs to start, including any runtime arguments to start them with
   * @return the result of starting each program
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public List<BatchProgramResult> start(NamespaceId namespace, List<BatchProgramStart> programs) throws IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespace, "start");
    HttpRequest request = HttpRequest.builder(HttpMethod.POST, url).withBody(GSON.toJson(programs), Charsets.UTF_8).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken());
    return ObjectResponse.<List<BatchProgramResult>>fromJsonBody(response, BATCH_RESULTS_TYPE, GSON).getResponseObject();
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) BatchProgramResult(co.cask.cdap.proto.BatchProgramResult) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Example 95 with HttpRequest

use of co.cask.common.http.HttpRequest in project cdap by caskdata.

the class StreamHandlerTest method listStreams.

private List<StreamDetail> listStreams(NamespaceId namespaceId) throws Exception {
    URL url = createURL(namespaceId.getNamespace(), "streams");
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = HttpRequests.execute(request);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NamespaceNotFoundException(namespaceId);
    }
    Assert.assertEquals(200, response.getResponseCode());
    return GSON.fromJson(response.getResponseBodyAsString(), new TypeToken<List<StreamDetail>>() {
    }.getType());
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) TypeToken(com.google.common.reflect.TypeToken) StreamDetail(co.cask.cdap.proto.StreamDetail) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException)

Aggregations

HttpRequest (co.cask.common.http.HttpRequest)97 URL (java.net.URL)75 HttpResponse (co.cask.common.http.HttpResponse)71 Test (org.junit.Test)22 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)13 BadRequestException (co.cask.cdap.common.BadRequestException)10 NotFoundException (co.cask.cdap.common.NotFoundException)9 ApplicationManager (co.cask.cdap.test.ApplicationManager)9 ServiceManager (co.cask.cdap.test.ServiceManager)9 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)6 IOException (java.io.IOException)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)5 TypeToken (com.google.common.reflect.TypeToken)5 TypeToken (com.google.gson.reflect.TypeToken)5 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)4 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)4 Instances (co.cask.cdap.proto.Instances)4 SparkManager (co.cask.cdap.test.SparkManager)4 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)3