public CompletionStage<Result> handleRequest() {
ObjectNode body = Json.newObject();
body.put("key", value);
return wsClient.url(serviceUri)
.setContentType("application/json")
.post(body)
.thenApply(this::handleWsResponse)
.exceptionally(e -> {
Logger.error("Exception", e);
return internalServerError();
});
}
private Result handleWsResponse(WSResponse wsResponse) {
if(wsResponse.getStatus() == Http.Status.OK) {
try {
return processData(wsResponse);
} catch (InterruptedException | TimeoutException | ExecutionException e) {
Logger.error("Exception e", e);
return internalServerError();
}
} else {
return new Result(wsResponse.getStatus());
}
}
private Result processData(WSResponse wsResponse) throws InterruptedException, ExecutionException, TimeoutException {
return CompletableFuture.supplyAsync(() -> wsResponse)
.thenApply(WSResponse::asJson)
.thenComposeAsync(this::fetchResponse, httpExecutionContext.current())
.thenApply(Results::ok)
.get(2, TimeUnit.SECONDS); // Error: java.lang.RuntimeException: There is no HTTP Context available from here.
}