Search in sources :

Example 1 with HttpMethod

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());
}
Also used : CommandInputError(io.cdap.cdap.cli.exception.CommandInputError) HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) ServiceId(io.cdap.cdap.proto.id.ServiceId) TypeToken(com.google.common.reflect.TypeToken) Map(java.util.Map) HttpMethod(io.cdap.common.http.HttpMethod)

Example 2 with HttpMethod

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());
        }
    }
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) HttpResponse(io.cdap.common.http.HttpResponse) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) HttpMethod(io.cdap.common.http.HttpMethod) Test(org.junit.Test)

Example 3 with HttpMethod

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));
            }
        }
    }
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) HttpResponse(io.cdap.common.http.HttpResponse) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) HttpMethod(io.cdap.common.http.HttpMethod) Test(org.junit.Test)

Aggregations

HttpMethod (io.cdap.common.http.HttpMethod)3 HttpResponse (io.cdap.common.http.HttpResponse)3 RemoteClient (io.cdap.cdap.common.internal.remote.RemoteClient)2 RemoteClientFactory (io.cdap.cdap.common.internal.remote.RemoteClientFactory)2 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)2 Test (org.junit.Test)2 TypeToken (com.google.common.reflect.TypeToken)1 CommandInputError (io.cdap.cdap.cli.exception.CommandInputError)1 ServiceId (io.cdap.cdap.proto.id.ServiceId)1 HttpRequest (io.cdap.common.http.HttpRequest)1 URL (java.net.URL)1 Map (java.util.Map)1