Search in sources :

Example 36 with HttpRequest

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

the class DatasetInstanceHandlerTest method createInstance.

private HttpResponse createInstance(DatasetId instance, String typeName, @Nullable String description, @Nullable DatasetProperties props, @Nullable String ownerPrincipal) throws IOException {
    DatasetInstanceConfiguration creationProperties;
    if (props != null) {
        creationProperties = new DatasetInstanceConfiguration(typeName, props.getProperties(), description, ownerPrincipal);
    } else {
        creationProperties = new DatasetInstanceConfiguration(typeName, null, description, ownerPrincipal);
    }
    HttpRequest request = HttpRequest.put(getUrl(instance.getNamespace(), "/data/datasets/" + instance.getEntityName())).withBody(GSON.toJson(creationProperties)).build();
    return HttpRequests.execute(request);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) DatasetInstanceConfiguration(co.cask.cdap.proto.DatasetInstanceConfiguration)

Example 37 with HttpRequest

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

the class DatasetInstanceHandlerTest method validateGet.

private void validateGet(URL url, Integer... expected) throws IOException {
    HttpRequest request = HttpRequest.get(url).build();
    assertStatus(HttpRequests.execute(request).getResponseCode(), url, expected);
}
Also used : HttpRequest(co.cask.common.http.HttpRequest)

Example 38 with HttpRequest

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

the class SparkPageRankAppTest method test.

@Test
public void test() throws Exception {
    // Deploy the SparkPageRankApp
    ApplicationManager appManager = deployApplication(SparkPageRankApp.class);
    // Send a stream events to the Stream
    StreamManager streamManager = getStreamManager(SparkPageRankApp.BACKLINK_URL_STREAM);
    streamManager.send(Joiner.on(" ").join(URL_1, URL_2));
    streamManager.send(Joiner.on(" ").join(URL_1, URL_3));
    streamManager.send(Joiner.on(" ").join(URL_2, URL_1));
    streamManager.send(Joiner.on(" ").join(URL_3, URL_1));
    // Start service
    ServiceManager serviceManager = appManager.getServiceManager(SparkPageRankApp.SERVICE_HANDLERS).start();
    // Wait for service to start since the Spark program needs it
    serviceManager.waitForStatus(true);
    // Start the SparkPageRankProgram
    SparkManager sparkManager = appManager.getSparkManager(SparkPageRankApp.PageRankSpark.class.getSimpleName()).start();
    sparkManager.waitForRun(ProgramRunStatus.COMPLETED, 60, TimeUnit.SECONDS);
    // Run RanksCounter which will count the number of pages for a pr
    MapReduceManager mapReduceManager = appManager.getMapReduceManager(SparkPageRankApp.RanksCounter.class.getSimpleName()).start();
    mapReduceManager.waitForRun(ProgramRunStatus.COMPLETED, 3, TimeUnit.MINUTES);
    //Query for rank
    URL url = new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), SparkPageRankApp.SparkPageRankServiceHandler.RANKS_PATH);
    HttpRequest request = HttpRequest.post(url).withBody(("{\"" + SparkPageRankApp.SparkPageRankServiceHandler.URL_KEY + "\":\"" + URL_1 + "\"}")).build();
    HttpResponse response = HttpRequests.execute(request);
    Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    Assert.assertEquals(RANK, response.getResponseBodyAsString());
    // Request total pages for a page rank and verify it
    url = new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), SparkPageRankApp.SparkPageRankServiceHandler.TOTAL_PAGES_PATH + "/" + RANK);
    response = HttpRequests.execute(HttpRequest.get(url).build());
    Assert.assertEquals(TOTAL_PAGES, response.getResponseBodyAsString());
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) ApplicationManager(co.cask.cdap.test.ApplicationManager) SparkManager(co.cask.cdap.test.SparkManager) MapReduceManager(co.cask.cdap.test.MapReduceManager) StreamManager(co.cask.cdap.test.StreamManager) ServiceManager(co.cask.cdap.test.ServiceManager) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Example 39 with HttpRequest

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

the class ServiceClient method storeRouteConfig.

/**
   * Stores RouteConfig of a service with different application versions.
   *
   * @param serviceId {@link ServiceId} of the service with the application version part ignored
   * @param routeConfig a Map of {@link String} application version and {@link Integer} percentage of
   *                    traffic routed to the version.
   */
public void storeRouteConfig(ServiceId serviceId, Map<String, Integer> routeConfig) throws IOException, UnauthorizedException, UnauthenticatedException {
    URL url = buildRouteConfigUrl(serviceId);
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(routeConfig, MAP_STRING_INTEGER_TYPE)).build();
    restClient.upload(request, config.getAccessToken());
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) URL(java.net.URL)

Example 40 with HttpRequest

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

the class StreamClient method setTTL.

/**
   * Sets the Time-to-Live (TTL) of a stream. TTL governs how long stream events are readable.
   *
   * @param stream ID of the stream
   * @param ttlInSeconds desired TTL, in seconds
   * @throws IOException if a network error occurred
   * @throws StreamNotFoundException if the stream with the specified name was not found
   */
public void setTTL(StreamId stream, long ttlInSeconds) 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("ttl", ttlInSeconds))).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)

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