use of io.cdap.common.http.HttpMethod in project cdap by caskdata.
the class CallServiceCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String method = arguments.get(ArgumentName.HTTP_METHOD.toString());
String path = arguments.get(ArgumentName.ENDPOINT.toString());
path = path.startsWith("/") ? path.substring(1) : path;
String headers = arguments.getOptional(ArgumentName.HEADERS.toString(), "");
String bodyString = arguments.getOptional(ArgumentName.HTTP_BODY.toString(), "");
String bodyFile = arguments.getOptional(ArgumentName.LOCAL_FILE_PATH.toString(), "");
Preconditions.checkNotNull(bodyString);
Preconditions.checkNotNull(bodyFile);
if (!bodyString.isEmpty() && !bodyFile.isEmpty()) {
String message = String.format("Please provide either [body <%s>] or [body:file <%s>], " + "but not both", ArgumentName.HTTP_BODY.toString(), ArgumentName.LOCAL_FILE_PATH.toString());
throw new CommandInputError(this, message);
}
Map<String, String> headerMap = GSON.fromJson(headers, new TypeToken<Map<String, String>>() {
}.getType());
ServiceId service = new ServiceId(parseProgramId(arguments, ElementType.SERVICE));
URL url = arguments.hasArgument(ArgumentName.APP_VERSION.getName()) ? new URL(serviceClient.getVersionedServiceURL(service), path) : new URL(serviceClient.getServiceURL(service), path);
HttpMethod httpMethod = HttpMethod.valueOf(method);
HttpRequest.Builder builder = HttpRequest.builder(httpMethod, url).addHeaders(headerMap);
if (httpMethod == HttpMethod.GET && (!bodyFile.isEmpty() || !bodyString.isEmpty())) {
throw new UnsupportedOperationException("Sending body in a GET request is not supported");
}
if (!bodyFile.isEmpty()) {
builder.withBody(filePathResolver.resolvePathToFile(bodyFile));
} else if (!bodyString.isEmpty()) {
builder.withBody(bodyString);
}
HttpResponse response = restClient.execute(builder.build(), clientConfig.getAccessToken());
output.printf("< %s %s\n", response.getResponseCode(), response.getResponseMessage());
for (Map.Entry<String, String> header : response.getHeaders().entries()) {
output.printf("< %s: %s\n", header.getKey(), header.getValue());
}
output.print(response.getResponseBodyAsString());
}
use of io.cdap.common.http.HttpMethod in project cdap by caskdata.
the class RuntimeServiceRoutingTest method testGetAndDelete.
@Test
public void testGetAndDelete() throws IOException, UnauthorizedException {
ProgramRunId programRunId = NamespaceId.DEFAULT.app("app", "1.0").workflow("workflow").run(RunIds.generate());
mockRemoteAuthenticatorProvider.setAuthenticator(new MockRemoteAuthenticator(programRunId));
RemoteClient remoteClient = injector.getInstance(RemoteClientFactory.class).createRemoteClient(Constants.Service.RUNTIME, DefaultHttpRequestConfig.DEFAULT, Constants.Gateway.INTERNAL_API_VERSION_3 + "/runtime/namespaces");
for (HttpMethod method : EnumSet.of(HttpMethod.GET, HttpMethod.DELETE)) {
for (int status : Arrays.asList(200, 400, 404, 500)) {
io.cdap.common.http.HttpRequest request = remoteClient.requestBuilder(method, String.format("%s/apps/%s/versions/%s/%s/%s/runs/%s/services/%s/mock/%s/%d", programRunId.getNamespace(), programRunId.getApplication(), programRunId.getVersion(), programRunId.getType().getCategoryName(), programRunId.getProgram(), programRunId.getRun(), MOCK_SERVICE, method.name().toLowerCase(), status)).build();
HttpResponse response = remoteClient.execute(request);
Assert.assertEquals(status, response.getResponseCode());
}
}
}
use of io.cdap.common.http.HttpMethod in project cdap by caskdata.
the class RuntimeServiceRoutingTest method testPutAndPost.
@Test
public void testPutAndPost() throws IOException, UnauthorizedException {
ProgramRunId programRunId = NamespaceId.DEFAULT.app("app", "1.0").workflow("workflow").run(RunIds.generate());
mockRemoteAuthenticatorProvider.setAuthenticator(new MockRemoteAuthenticator(programRunId));
RemoteClient remoteClient = injector.getInstance(RemoteClientFactory.class).createRemoteClient(Constants.Service.RUNTIME, DefaultHttpRequestConfig.DEFAULT, Constants.Gateway.INTERNAL_API_VERSION_3 + "/runtime/namespaces");
String largeContent = Strings.repeat("Testing", 32768);
for (String content : Arrays.asList("", "Small content", largeContent)) {
for (HttpMethod method : EnumSet.of(HttpMethod.PUT, HttpMethod.POST)) {
for (int status : Arrays.asList(200, 400, 404, 500)) {
io.cdap.common.http.HttpRequest request = remoteClient.requestBuilder(method, String.format("%s/apps/%s/versions/%s/%s/%s/runs/%s/services/%s/mock/%s/%d", programRunId.getNamespace(), programRunId.getApplication(), programRunId.getVersion(), programRunId.getType().getCategoryName(), programRunId.getProgram(), programRunId.getRun(), MOCK_SERVICE, method.name().toLowerCase(), status)).withBody(content).build();
HttpResponse response = remoteClient.execute(request);
Assert.assertEquals(status, response.getResponseCode());
Assert.assertEquals(content, response.getResponseBodyAsString(StandardCharsets.UTF_8));
}
}
}
}
Aggregations