use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class AbstractNamespaceQueryClient method list.
@Override
public List<NamespaceMeta> list() throws Exception {
HttpRequest request = HttpRequest.get(resolve("namespaces")).build();
HttpResponse response = execute(request);
if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
return ObjectResponse.fromJsonBody(response, new TypeToken<List<NamespaceMeta>>() {
}).getResponseObject();
}
throw new IOException(String.format("Cannot list namespaces. Reason: %s", response.getResponseBodyAsString()));
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class RemotePreviewRequestFetcher method fetch.
@Override
public Optional<PreviewRequest> fetch() throws IOException, UnauthorizedException {
HttpRequest request = remoteClientInternal.requestBuilder(HttpMethod.POST, "requests/pull").withBody(ByteBuffer.wrap(pollerInfoProvider.get())).build();
HttpResponse httpResponse = remoteClientInternal.execute(request);
if (httpResponse.getResponseCode() == 200) {
PreviewRequest previewRequest = GSON.fromJson(httpResponse.getResponseBodyAsString(), PreviewRequest.class);
if (previewRequest != null && previewRequest.getPrincipal() != null) {
SecurityRequestContext.setUserId(previewRequest.getPrincipal().getName());
SecurityRequestContext.setUserCredential(previewRequest.getPrincipal().getFullCredential());
}
return Optional.ofNullable(previewRequest);
}
throw new IOException(String.format("Received status code:%s and body: %s", httpResponse.getResponseCode(), httpResponse.getResponseBodyAsString(Charsets.UTF_8)));
}
use of io.cdap.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();
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class ProgramClient method stop.
/**
* Stops a batch of programs in the same call.
*
* @param namespace the namespace of the programs
* @param programs the programs to stop
* @return the result of stopping 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> stop(NamespaceId namespace, List<BatchProgram> programs) throws IOException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(namespace, "stop");
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();
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class ProgramClient method setRuntimeArgs.
/**
* Sets the runtime args of a program.
*
* @param program the program
* @param runtimeArgs args of the program
* @throws IOException if a network error occurred
* @throws ProgramNotFoundException if the application or program could not be found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public void setRuntimeArgs(ProgramId program, Map<String, String> runtimeArgs) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
String path = String.format("apps/%s/versions/%s/%s/%s/runtimeargs", program.getApplication(), program.getVersion(), program.getType().getCategoryName(), program.getProgram());
URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(runtimeArgs)).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ProgramNotFoundException(program);
}
}
Aggregations