Play WS API Making nested requests - WSClient

I want to make two HTTP requests(POST) using play2 WSRequest where some information from the first response is send to the second request. I tried to do this in following manner to make sure second request is only triggers after the first one is completed. But I get Type mismatch: cannot convert from F.Promise to F.Promise error

public Promise<Result>  executeAPI(String apiName,JsonNode requestBody){



    WSRequest pcLoginRequest = ws.url("http://xxxxx.qa.local:8080/mytest/rest/login");
    pcLoginRequest.setContentType("application/json");      
    pcLoginRequest.setHeader("X-x-Password", "xxxxx")
            .setHeader("X-x-Username", "xxxxx")
            .setHeader("X-x-Content-Type", "application/json");

    Promise<Result> myPromise = pcLoginRequest.post("").map(response -> {           


        ProApiSession.getInstanceOf().setProToeken(response.asJson().get("token").asText());


        WSRequest pcrequest = ws.url("http://xxxxx.qa.local:8080/mytest/rest/api/" + apiName);
        pcrequest.setContentType("application/json");       
        pcrequest.setHeader("X-x-Token",ProApiSession.getInstanceOf().getProToeken() )
                .setBody(requestBody)
                .setHeader("X-x-Content-Type", "application/json");


        Promise<Result>  myPromise2 = pcLoginRequest.post(requestBody).map(response2 -> {
                      return Results.ok(response2.asJson());
                });

        return myPromise;

    }); 

Can someone please suggest how to do nested request using WSRequest in play. (import play.libs.ws.* )

Answers


Java 8 type inference errors are bad at the best of times. Since the result of the lambda you're passing to the first map is Promise<Result>, what you're trying to assign to myPromise is Promise<Promise<Result>>. What you actually want to do is replace the map call with flatMap, which is so named because is "flattens" the nested promise to just be a single promise.


Need Your Help

LWJGL Vertex and Fragment Shaders Wont Compile (Error CO206)

java opengl glsl shader lwjgl

I have been interested in learning OpenGl for a while. However every time I start working with it, I get the same error when compiling my shaders. This is the error code that is spits out.

Is it possible to disable both debugger visualization and ToString() invocation in all the debugger views of VS2008?

visual-studio-2008 tostring debuggervisualizer

My project depends on a 3rd party assembly which defines debugger visualizers for some of its types. Usually this is totally fine, but some types have lazy logic, which becomes eager once the data is