0

I need to call two APIs A1 and A2 but not parallelly. A2 would get called only if A1 returns some flag value in its JSON response.

I'm aware of how to make an http call in java using Httpclient. one way is to write one code to make first call and parse its response and again use the same code to make another call.Is their any other smart way which automate this process for us where I will pass both the request and the condition on which second one need to call like it is possible in Rxjava

Follwing is the Rxjava code snippet (Reference : (RxJava Combine Sequence Of Requests))

api1.items(queryParam)
.flatMap(itemList -> Observable.fromIterable(itemList)))
.flatMap(item -> api2.extendedInfo(item.id()))
.subscribe(...)

How can I accomplish this in Java? Is there any Java feature that already exists and will allow me to make a multiple sequential call?

I tried searching for existing solutions but they were not in Java.

priyadhingra19
  • 333
  • 4
  • 15
  • "*if any java class already exists that make us to make sequential call?*" - You mean... like the [`HTTPClient`](https://openjdk.java.net/groups/net/httpclient/intro.html)? – Turing85 Oct 05 '19 at 14:13
  • What is the meaning ".if any java class already exists that make us to make sequential call?" – Sambit Oct 05 '19 at 14:16
  • Can you not simply have a piece of Java code which 1) issues the call to A1 and receives the response; 2) parses the response and checks the appropriate flag value; 3) depending on the flag value, either calls A2 or doesn't? – Kevin Anderson Oct 05 '19 at 14:22
  • I have edited the question for more clarity. Please have a look.thanks! – priyadhingra19 Oct 05 '19 at 14:33

1 Answers1

0

You can use HttpURLConnection to do an API call.

Check response and accordingly trigger another call.

Something like this

public static void main(String[] args) throws IOException {

    String response1 = sendGET("http://url1");
    if(response1 != null && response1.contains("true")){
        String response2 = sendGET("http://url2");
    }

}

private static String sendGET(String url) throws IOException {
    URL obj = new URL(url);
    StringBuffer response = new StringBuffer();
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    int responseCode = con.getResponseCode();
    System.out.println("GET Response Code :: " + responseCode);
    if (responseCode == HttpURLConnection.HTTP_OK) { // success
        BufferedReader in = new BufferedReader(new InputStreamReader(
                con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // print result
        System.out.println(response.toString());
    } else {
        System.out.println("GET request not worked");
    }
    return response.toString();
}
MyTwoCents
  • 7,284
  • 3
  • 24
  • 52
  • Please check my question again .I have edited to make it more clear. Thanks for this answer!!! – priyadhingra19 Oct 05 '19 at 14:35
  • Btw, when switched to java 11 better to utilize HttpClient: https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html – zkvarz Jun 22 '21 at 07:51