DataPower

DataPower

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
Expand all | Collapse all

Calling JS functions in Gatewayscript

  • 1.  Calling JS functions in Gatewayscript

    Posted Fri November 27, 2020 01:11 PM

    Hi Team,

    I have below payload request, from the below payload for-each id I have to make different API calls.

    For Suppose In request If I received 3 Id's from the list will make below GET call.

    a) https://10.10.9.10:8000/test1/123

    b) https://10.10.9.10:8000/test1/456

    c) https://10.10.9.10:8000/test1/789

    After getting the below success response from each API call, I need to fetch one of the element from the response.

    {

    data :{

    "firstName" :"xxxx",

    "Lastname" :"yyyyy"

    }

    }

    Also If above mentioned 3 calls are success and responsecode="200", then agian I have to make PATCH/POST call with new url by sending the payload. If anyone of the "id" response fail,will not make any call to other PATCHApicall.

    Here my query, Since I have so many calls are running under urlopen,I want to add this urlopen in code in one function and trying to call that function in same js. But while trying to call the function , how to fetch the response of that particualr, Since I have to

    pass one of the element to the next PATCH call. ?

    Could you please suggest what is the best approach and how to return the json response from one function and use it in another function with in the same js file.

    can we achieve this requirement thru js functions or else please suggest me is there any otherway to approach on this in gatewayscript. If yes, please share some sample code.

    Sample code.

    Input:

    {

    "FinalList": {

    "IdLst": {

    "id": [

    "123",

    "456",

    "789"

    ]

    }

    }

    }

    var id = input.FinalList.IdLst.id;

    id.forEach(function(id){

    var uri = url + 'test/' +id(123,456,789);

    var getapicall = {

    target: uri,

    sslClientProfile: 'xxx',

    method: 'GET',

    contentType: 'application/json',

    headers: {

    'Token': Token

    },

    };

    urlopen.open(getapicall, function(error, response) {

    if (error) {

    logger.error("urlopen connect error with getapicall" + JSON.stringify(error));

    session.reject("urlopen connect error with getapicall");

    } else {

    var responseStatusCode = response.statusCode;

    if (responseStatusCode == 200 ) {

    response.readAsBuffer(function(error, responseData) {

    f (error) {

    //dp:reject stop the transaction and go error rule with error code and description

    logger.error("failure in putting message to getapicall" + JSON.stringify(error));

    //session.reject("failure in putting message to getapicall");

    } else {

    hm.response.statusCode = responseStatusCode;

    var getapiesponse = JSON.parse(responseData);

    var firstName = JSON.parse(responseData).data.firstName;

    if(firstName != undefined) {

    var patchcallurl = url+'test2/'+id(123,456,789);

    var TokenValue = apiToken;

    var payload = {

    "Names": firstName

    }

    var PATCHApicall = {

    target: patchcallurl,

    sslClientProfile: 'xxxx',

    method: 'PATCH',

    contentType: 'application/json',

    headers: {

    'Token': Token

    },

    data: payload

    };

    // then again from this 200:success response, I have to fetch one of the element for the corresponding Id and and again make call to the other url as like above.

    }

    }

    });

    }

    });

    });



    #DataPower
    #Support
    #SupportMigration


  • 2.  RE: Calling JS functions in Gatewayscript

    Posted Mon November 30, 2020 06:50 PM

    Hi,

    GatewayScript is (like JavaScript) async and if you want to do multiple url-opens you should wrap the calls using promise.

    It might be easier to divide the functionality into multiple actions using DataPower call rule and for-each rather than writing everything in one large GS file. Small simple code is easier to maintain.

    --HP



    #DataPower
    #Support
    #SupportMigration