use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class DynamicPluginServiceTestRun method testDynamicPluginSimple.
@Test
public void testDynamicPluginSimple() throws Exception {
// test a single plugin
Map<String, String> properties = new HashMap<>();
properties.put("value", "x");
URL url = baseURI.resolve(String.format("plugins/%s/apply", ConstantFunction.NAME)).toURL();
HttpRequest request = HttpRequest.builder(HttpMethod.POST, url).withBody(GSON.toJson(properties)).build();
HttpResponse response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals("x", response.getResponseBodyAsString());
// test plugin that uses a plugin
Map<String, String> delegateProperties = new HashMap<>();
delegateProperties.put("value", "y");
properties = new HashMap<>();
properties.put("delegateName", ConstantFunction.NAME);
properties.put("properties", GSON.toJson(delegateProperties));
url = baseURI.resolve(String.format("plugins/%s/apply", DelegatingFunction.NAME)).toURL();
request = HttpRequest.builder(HttpMethod.POST, url).withBody(GSON.toJson(properties)).build();
response = executeHttp(request);
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals("y", response.getResponseBodyAsString());
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class ApplicationClient method update.
/**
* 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()));
}
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class ApplicationClient method deploy.
/**
* 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());
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class AuthorizationClient method addRoleToPrincipal.
@Override
public void addRoleToPrincipal(Role role, Principal principal) throws AccessException {
URL url = resolveURL(String.format(AUTHORIZATION_BASE + "%s/%s/roles/%s", principal.getType(), principal.getName(), role.getName()));
HttpRequest request = HttpRequest.put(url).build();
executeExistingRolesRequest(role, request);
}
use of io.cdap.common.http.HttpRequest in project cdap by caskdata.
the class AuthorizationClient method grant.
@Override
public void grant(Authorizable authorizable, Principal principal, Set<? extends Permission> permissions) throws AccessException {
GrantRequest grantRequest = new GrantRequest(authorizable, principal, permissions);
URL url = resolveURL(AUTHORIZATION_BASE + "/privileges/grant");
HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(grantRequest)).build();
executePrivilegeRequest(request);
}
Aggregations