Anushka Ekanayake
Anushka Ekanayake

Reputation: 987

Aggregate multiple json responses with aggregate mediator in wso2 esb

I have defined a api in wso2 esb and it calls two internal APIs through recepient list and which are passing json responses as follows.(sample responses)

{
  "name": "api1",
  "response": "success",
  "status": "1"
}

and

{
  "name": "api2",
  "response": "unsuccess",
  "status": "2"
}

I need to pass the response by aggregating both of these responses as a single response. I red about payloadfactory and able to construct aggregated response. But i need to aggregate whatever the responses coming from these 2 apis and generate response as one single json object and pass by including both of these responses as follows

 {
    "response1": {
        "name": "api1",
        "response": "success",
        "status": "1"
    },
    "response2": {
        "name": "api2",
        "response": "unsuccess",
        "status": "2"
    }
}

so how can a accomplish with WSO2ESB. I'm using latest version of ESB.

Upvotes: 2

Views: 2054

Answers (2)

Afzal khan
Afzal khan

Reputation: 41

I have created three APIs and aggregated the API responses using Clone ,Below is my API which is used for aggregating responses of two API endpoints

<api xmlns="http://ws.apache.org/ns/synapse" name="aggregateResponse" context="/aggregate">
   <resource methods="POST">
      <inSequence>
         <clone id="aggr">
            <target>
               <sequence>
                  <call>
                     <endpoint>
                        <http method="GET" uri-template="http://localhost:8280/getresponse1"/>
                     </endpoint>
                  </call>
                  <log>
                     <property name="Logger1" expression="json-eval($.)"/>
                  </log>
               </sequence>
            </target>
            <target>
               <sequence>
                  <call>
                     <endpoint>
                        <http method="GET" uri-template="http://localhost:8280/getResponse2"/>
                     </endpoint>
                  </call>
                  <log>
                     <property name="Logger1" expression="json-eval($.)"/>
                  </log>
               </sequence>
            </target>
         </clone>
         <payloadFactory media-type="json">
            <format>{"responses":{ "name":"$1","response":"$2","status":"$3"}}</format>
            <args>
               <arg evaluator="json" expression="$.name"/>
               <arg evaluator="json" expression="$.response"/>
               <arg evaluator="json" expression="$.status"/>
            </args>
         </payloadFactory>
         <loopback/>
      </inSequence>
      <outSequence>
         <property name="res" scope="default">
            <ResponseDetail xmlns=""/>
         </property>
         <aggregate id="aggr">
            <completeCondition>
               <messageCount min="-1" max="-1"/>
            </completeCondition>
            <onComplete expression="$body//responses" enclosingElementProperty="res">
               <payloadFactory media-type="json">
                  <format>{"response1":$1 ,"response2":$2}</format>
                  <args>
                     <arg evaluator="json" expression="$.ResponseDetail.responses[0]"/>
                     <arg evaluator="json" expression="$.ResponseDetail.responses[1]"/>
                  </args>
               </payloadFactory>
               <send/>
            </onComplete>
         </aggregate>
      </outSequence>
   </resource>
</api>

API 1:

<api xmlns="http://ws.apache.org/ns/synapse" name="response1" context="/getresponse1">
   <resource methods="GET">
      <inSequence>
         <payloadFactory media-type="json">
            <format>{  "name": "api1",  "response": "success",  "status": "1"}</format>
            <args/>
         </payloadFactory>
         <respond/>
      </inSequence>
   </resource>
</api>

API 2:

<api xmlns="http://ws.apache.org/ns/synapse" name="response2" context="/getResponse2">
   <resource methods="GET">
      <inSequence>
         <payloadFactory media-type="json">
            <format>{  "name": "api2",  "response": "unsuccess",  "status": "2"}</format>
            <args/>
         </payloadFactory>
         <respond/>
      </inSequence>
   </resource>
</api>

Upvotes: 4

Ravindra Ranwala
Ravindra Ranwala

Reputation: 21124

Well, this is where enrich mediator becomes handy. Please try this out. I have not tested this since I am not doing WSO2 related stuffs now. But your feedback is warmly welcome. The pseudo code is something like this.

<call>
    <endpoint>
       <http method="GET" uri-template="http://www.mocky.io/v2/some-ep"/>
    </endpoint>
 </call>
 <enrich>
    <source type="body" clone="true"/>
    <target type="property" property="first-json"/>
 </enrich>
    <call>
    <endpoint>
       <http method="GET" uri-template="http://www.mocky.io/v2/another-ep"/>
    </endpoint>
 </call>
 <enrich>
    <source type="property" property="first-json" clone="true"/>
    <target action="sibling" xpath="//"/>
 </enrich>
 </respond>

Upvotes: 0

Related Questions