I have a controller action where I need to call 3rd party web services.
My problem is that I am not calling one web service. I need to chain between 4 to 5 web services. Each web service I call returns a JSON object which I need to process and based on some logic I decide to either call another web service (from the 4 web services) or return a response to the caller. Here's what I am trying to do:
public static Promise<Result> accounts(){
return WS.url("url1").get().map(response1 -> {
JsonNode mynode = response1.asJson();
if (mynode.get("status").asInt()==200){
Promise<JsonNode> jsonPromise = WS.url("url2").get().map(response2->{
return response2.asJson();
});
}
return ok(mynode);
});
}
Now, from the documentation, I think that what I need is to chain promises where each web service call is a promise. But I am not sure how to do that?
Thanks
They're calling it Reactive Composition and it's done like this ->
public static Promise<Result> reactiveCombo() {
Promise<WSResponse> promise1 = WS.url("url1").get();
Promise<WSResponse> promise2 = WS.url("url2").get();
Promise<WSResponse> promise3 = WS.url("url3").get();
return promise1.flatMap(response1 -> {
final JsonNode json1 = response1.asJson();
if (!json1.has("someField")) {
return Promise.promise(() -> badRequest());
}
return promise2.flatMap(response2 -> {
final JsonNode json2 = response2.asJson();
if (json1.get("someField").asText().equals(json2.get("someField").asText())) {
return Promise.promise(() -> badRequest());
}
return promise3.map(response3 -> {
final JsonNode json3 = response3.asJson();
if (json3.get("boolField").asBoolean()) {
return badRequest();
}
return ok();
});
});
});
}
For large # of calls you can use Promise.sequence()
and get 'creative':
private static Promise<JsonNode> getPromise(String url, Predicate<JsonNode> predicate) {
return WS.url(url).get().map(response -> {
JsonNode json = response.asJson();
if (predicate.negate().test(json)) {
throw new Exception("BUMMER!");
}
return json;
});
}
public static Promise<Result> reactiveCombo(List<String> urls) {
List<Promise<JsonNode>> promises = new ArrayList<Promise<JsonNode>>(urls.size());
Predicate<String> predURL = p -> p.contains("goodApi");
Predicate<JsonNode> pred1 = p -> p.has("boolField") && p.get("boolField").asBoolean();
Predicate<JsonNode> pred2 = p -> p.has("someField");
urls.forEach(url -> {
Promise<JsonNode> promise = predURL.test(url) ? getPromise(url, pred1) : getPromise(url, pred2);
promises.add(promise);
});
return Promise.sequence(promises).map(results -> ok()).recover(t -> badRequest());
}
Additional details + docudrama:
Go Reactive with Java 8 & Play Framework (old but still educative)
JavaWS
You can use recursion to flatten this structure for any number of Promises. First create a class:
public static class ChainedWebService {
public final Optional<ChainedWebService> next;
public final WSRequestHolder wsResponsePromise;
private final F.Predicate<JsonNode> predicate;
public ChainedWebService(String url, Optional<ChainedWebService> next, F.Predicate<JsonNode> predicate) {
this.next = next;
this.wsResponsePromise = WS.url(url);
this.predicate = predicate;
}
public F.Promise<Result> processChain() {
return wsResponsePromise.get().flatMap(new F.Function<WSResponse, F.Promise<Result>>() {
@Override
public F.Promise<Result> apply(WSResponse wsResponse) throws Throwable {
if (!predicate.test(wsResponse.asJson())) {
return F.Promise.pure(badRequest());
}
if (!next.isPresent()) {
return F.Promise.pure(ok());
}
return next.get().processChain();
}
});
}
}
And then use it:
public static F.Promise<Result> reactiveCombo() {
ChainedWebService chainedWebService3 = new ChainedWebService(
"url3",
Optional.<ChainedWebService>empty(),
jsonNode -> jsonNode.get("boolField").asBoolean()
);
ChainedWebService chainedWebService2 = new ChainedWebService(
"url2",
Optional.of(chainedWebService3),
jsonNode -> jsonNode.get("someField").asText().equals(jsonNode.get("someField").asText()));
ChainedWebService chainedWebService1 = new ChainedWebService(
"url1",
Optional.of(chainedWebService2),
jsonNode -> jsonNode.has("someField")
);
return chainedWebService1.processChain();
}
You can implement some basic builder to make the building process more reasonable.
Hope thats help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With