Search in sources :

Example 96 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class DatasetTypeClient method exists.

/**
 * Checks if a dataset type exists.
 *
 * @param type the dataset type to check
 * @return true if the dataset type exists
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public boolean exists(DatasetTypeId type) throws DatasetTypeNotFoundException, IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(type.getParent(), String.format("data/types/%s", type.getType()));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    return response.getResponseCode() != HttpURLConnection.HTTP_NOT_FOUND;
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 97 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class LineageClient method getLineage.

private <T> T getLineage(NamespacedEntityId namespacedId, String path, Class<T> type) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
    URL lineageURL = config.resolveNamespacedURLV3(new NamespaceId(namespacedId.getNamespace()), path);
    HttpResponse response = restClient.execute(HttpRequest.get(lineageURL).build(), config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(response.getResponseBodyAsString());
    }
    return GSON.fromJson(response.getResponseBodyAsString(), type);
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException) NotFoundException(io.cdap.cdap.common.NotFoundException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) URL(java.net.URL)

Example 98 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class MetricsClient method query.

/**
 * Gets the value of the given metrics.
 *
 * @param tags tags for the request
 * @param metrics names of the metrics
 * @param groupBys groupBys for the request
 * @param timeRangeParams parameters specifying the time range
 * @return values of the metrics
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
// TODO: take in query object shared by MetricsHandler
public MetricQueryResult query(Map<String, String> tags, List<String> metrics, List<String> groupBys, Map<String, String> timeRangeParams) throws IOException, UnauthenticatedException, UnauthorizedException {
    List<String> queryParts = Lists.newArrayList();
    queryParts.add("target=tag");
    add("metric", metrics, queryParts);
    add("groupBy", groupBys, queryParts);
    addTags(tags, queryParts);
    addTimeRangeParametersToQuery(timeRangeParams, queryParts);
    URL url = config.resolveURLV3(String.format("metrics/query?%s", Joiner.on("&").join(queryParts)));
    // Required to add body even if runtimeArgs is null to avoid 411 error for Http POST
    HttpRequest.Builder request = HttpRequest.post(url).withBody("");
    HttpResponse response = restClient.execute(request.build(), config.getAccessToken());
    return ObjectResponse.fromJsonBody(response, MetricQueryResult.class).getResponseObject();
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult) URL(java.net.URL)

Example 99 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class MonitorClient method getSystemServiceLiveInfo.

/**
 * Gets the live info of a system service.
 *
 * @param serviceName Name of the system service
 * @return live info of the system service
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public SystemServiceLiveInfo getSystemServiceLiveInfo(String serviceName) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    URL url = config.resolveURLV3(String.format("system/services/%s/live-info", serviceName));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    String responseBody = new String(response.getResponseBody());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(new SystemServiceId(serviceName));
    }
    return GSON.fromJson(responseBody, SystemServiceLiveInfo.class);
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) HttpResponse(io.cdap.common.http.HttpResponse) NotFoundException(io.cdap.cdap.common.NotFoundException) URL(java.net.URL)

Example 100 with HttpResponse

use of io.cdap.common.http.HttpResponse in project cdap by caskdata.

the class MonitorClient method getAllSystemServiceStatus.

/**
 * Gets the status of all system services.
 *
 * @return map from service name to service status
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public Map<String, String> getAllSystemServiceStatus() throws IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveURL("system/services/status");
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken());
    return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() {
    }).getResponseObject();
}
Also used : TypeToken(com.google.common.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Aggregations

HttpResponse (io.cdap.common.http.HttpResponse)908 URL (java.net.URL)344 HttpRequest (io.cdap.common.http.HttpRequest)276 Test (org.junit.Test)268 NotFoundException (io.cdap.cdap.common.NotFoundException)98 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)74 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)74 IOException (java.io.IOException)72 ProgramId (io.cdap.cdap.proto.id.ProgramId)70 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)54 TypeToken (com.google.common.reflect.TypeToken)52 ProgramNotFoundException (io.cdap.cdap.common.ProgramNotFoundException)48 JsonObject (com.google.gson.JsonObject)44 RunRecord (io.cdap.cdap.proto.RunRecord)44 TypeToken (com.google.gson.reflect.TypeToken)40 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)40 HashMap (java.util.HashMap)39 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)38 BadRequestException (io.cdap.cdap.common.BadRequestException)36 Id (io.cdap.cdap.common.id.Id)36