Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt

Use this file to discover all available pages before exploring further.

This page explains how to retrieve data from two different services via API call and combine their outputs into a single output.

Scenario

  • Service1: Returns user information
  • Service2: Returns department information
  • Matching: When Service1.xDEPARTMANI == Service2.ChildDepartmentID, adds data from Service2 to Service1

Service1 Sample Output

[
  {
    "USERID": "111",
    "FIRSTNAME": "ADI",
    "LASTNAME": "SOYADI",
    "EMPLOYEMENTSTART": "2011-04-18 00:00:00.0",
    "DEPARTMENT": "333",
    "DEPARTMENTNAME": "DEPARTMAN ADI",
    "PROFESSION": "44",
    "PROFESSIONNAME": "YETKİLİ",
    "POS_ITION": "55555"
  }
]

Service2 Sample Output

{
  "0": {
    "ParentDepartmentID": "xxx",
    "ParentDepartmentName": "DEP NAME > GİDİLECEK DEP NAME",
    "ChildDepartmentID": "yyy"
  },
  "1": {...}
}

Groovy Script

import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def linearFind(map, elementToSearch) {
    def returnValue = -1
    map.each { k, v ->
        if (v.ChildDepartmentID.toString() == elementToSearch.xDEPARTMANI.toString()){
            returnValue = v
        }
    }
    return returnValue
}

//Gets response from Service1
String requestX = "http://1.1.1.1:30080/apigateway/xyolu"
URL urlX = new URL(requestX)
HttpURLConnection connX = (HttpURLConnection) urlX.openConnection()
connX.setDoOutput(true)
connX.setRequestMethod("GET")
connX.setRequestProperty("Content-Type", "application/json")
connX.setUseCaches(false)
connX.setConnectTimeout(60000)

def orijinalBody = connX.inputStream.withCloseable { inStream ->
    new JsonSlurper().parse(inStream as InputStream)
}

//Gets response from Service2
String request = "http://1.1.1.1:30080/apigateway/test/test"
URL url = new URL(request)
HttpURLConnection conn = (HttpURLConnection) url.openConnection()
conn.setDoOutput(true)
conn.setRequestMethod("POST")
conn.setRequestProperty("Content-Type", "application/json")
conn.setUseCaches(false)
conn.setConnectTimeout(10000)

def childBody = conn.inputStream.withCloseable { inStream ->
    new JsonSlurper().parse(inStream as InputStream)
}

//Compares data from 2 services
//If match found, adds relevant values to record
orijinalBody.eachWithIndex { val, idx ->
    def result = linearFind(childBody, val)
    if(result != -1){
        val.put("ParentDepartmentID", result.ParentDepartmentID)
        val.put("ParentDepartmentName", result.ParentDepartmentName)
    }
}

//Returns result in JSON format
responseBodyTextToClient = JsonOutput.toJson(orijinalBody)

Explanation

This script performs the following operations:
  1. Retrieving Data from Service1: Retrieves user information from the first service with GET request.
  2. Retrieving Data from Service2: Retrieves department information from the second service with POST request.
  3. Matching: Uses linearFind function to find matching department information in Service2 for each record in Service1.
  4. Data Combining: When a match is found, adds ParentDepartmentID and ParentDepartmentName fields from Service2 to Service1 record.
  5. Returning Result: Returns combined data in JSON format.
This script should be run on the response line (Response Policy) because it uses the responseBodyTextToClient variable.