0

I have 3 APIs.

API#1 - GET {{endPoint}}/event/{{customerId}}/orders

API#2 - POST {{endPoint}}/pullOrders/{{orderId}}/claims

API#3 - DELETE {{endPoint}}/pullOrders/{{orderId}}/events

Response of API#1 is a json with the orderIDs.

{
    "List": [
        {
            "OrderId": "c6882585",
            "Type": "pull"
        },
        {
            "OrderId": "a2f818e7",
            "Type": "pull"
        },
        {
            "OrderId": "1f45e306",
            "Type": "pull"
        }
    ]
}

The input to API#2 is the orderId from the response of API#1. The response of API#2 is a json with partNumber for a particular customerId. The response could be empty/null with varying number of partNumber with other customerId.

[
    {
        "EventId": "46323beaf738",
        "PartNumber": "22b188f7",
    },
    {
        "EventId": "5024519db933",
        "PartNumber": "2c2d8757",
    }
]

These partNumber are input to API#3 in it’s body.

Body of API#3

{
    "partNumberList": [22b188f7, 2c2d8757]
}

The goal is to delete all order history for a customer by chaining API#1, API#2 and API#3.

In Tests of API#1 I get all the orderIds in test and save it in a variable.

const responseJson = pm.response.json();
var orderIdList = responseJson.List;
var orderIds = []
for(var index in orderIdList) {
    orderIds.push(orderIdList[index].OrderId);
}
pm.variables.set("orderIds", JSON.stringify(orderIds));
pm.variables.set("orderIdsIndex", 0);

In the Pre-request Script of API#2 I set the call to API#2 with the orderId from API#1.

var orderIds = JSON.parse(pm.variables.get('orderIds'));
var index = parseInt(pm.variables.get('orderIdsIndex'));
pm.variables.set('orderId', orderIds[index]);
if (index + 1 < orderIds.length){
    pm.variables.set('orderIdsIndex', index + 1);
    postman.setNextRequest('API#2');
}else{
    postman.setNextRequest(null);
}

Till this point things work as expected, i.e. API#2 is called in a loop 3 times for 3 orderIds.

In Tests of API#2 I have tried the following. The run stops when partNumbers array below is non empty. It calls API#3 and then stops. How do I make it continue to the next iteration?

const responseJson = pm.response.json();
var partNumbers = [];
for (var key in responseJson){
    partNumbers.push(responseJson[key].PartNumber);
}
pm.variables.set('partNumbers', JSON.stringify(partNumbers));
if (receiptHandles.length != 0) {
    postman.setNextRequest('API#3');
}else{
    postman.setNextRequest('API#2');
}

And in body of API#3 I have

{
    "partNumberList": {{partNumbers}}
}
ontherocks
  • 1,747
  • 5
  • 26
  • 43
  • why can't you store everything before hand before calling the third endpoint . Why going back to api2? – PDHide Jul 07 '21 at 07:40

2 Answers2

2

As far as I understand, your problem is:

  • API#1 --> list of "x". [x1, x2]
  • for each item (x) --> call API#2 --> list of "y" x1 --> [y1, y2] x2 --> [y3, y4, y5]
  • for each item (y) --> call API#3. [y1, y2] --> DELETE [y1, y2] then stop execution

My solution is:

In step 2, you save x and y in format

[
    {
        "x1": ["y1", "y2"]
    },
    {
        "x2": ["y3", "y4", "y5"]
    }
]

In step 3, you just need for-each in list of the above object, to DELETE each pair x and y

Example code: In test of API#2

let listOfRequest;
if (pm.environment.get("listOfRequest") == undefined){
    listOfRequest = [];
} else {
    listOfRequest = JSON.parse(pm.environment.get("listOfRequest"));
}

const responseJson = pm.response.json();
let requestObj = {};

requestObj.orderId = _.random(0,100); // you can use your orderId

let y = [];
responseJson.forEach((element) => {
    y.push(element.PartNumber);
});

requestObj.y = y;
listOfRequest.push(requestObj);

console.log(listOfRequest);

pm.environment.set('listOfRequest', JSON.stringify(listOfRequest));
    
//The array will be accumulated for each run of API#2
//1st: [{"orderId":1,"y":["111","222"]},
//2nd: [{"orderId":1,"y":["111","222"]},{"orderId":2,"y":["333","444","555"]}]
    

In pre-request of API#3

// use same technique in pre-request API#2, loop in the array `listOfRequest`
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
  • `partNumbers` is not the problem. With my code too I get `partNumbers` correctly. Then problem is when `partNumbers` is empty, the run stops. I want it to continue to the next iteration. – ontherocks Jul 07 '21 at 02:25
  • 1
    @ontherocks I understand now. My solution is just maintain all `partNumber` in one variable, then you can loop in the array. I will update my answer. – lucas-nguyen-17 Jul 07 '21 at 02:33
  • `partNumbers` is not the issue here. I think I am not able to convey the problem. I made some progress. With my updated code in OP, it is able to loop to API#2 when `partNumber` is empty, but now when it is non empty, it calls API#3 and stops the run. – ontherocks Jul 07 '21 at 03:19
  • @ontherocks I updated more info to clarify what I understand and my solution for this. Dont worry about the `partNumbers`, it's just a name, you can change it to match your situation. – lucas-nguyen-17 Jul 07 '21 at 03:59
  • Appreciate your help. A list of array `[y1, y2] [y3, y4, y5]` is needed because each of them is a function of the `orderId`. API#3 requires `orderid` in the request. A cumulative flat array won't work here. – ontherocks Jul 07 '21 at 04:11
  • For example, `DELETE {{endPoint}}/pullOrders/1/events` use body `[y1, y2]` `DELETE {{endPoint}}/pullOrders/2/events` use body `[y3, y4, y5]` Is this correct? I will think about it. – lucas-nguyen-17 Jul 07 '21 at 05:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234599/discussion-between-lucasnguyen17-and-ontherocks). – lucas-nguyen-17 Jul 07 '21 at 05:55
1

why can't you store everything before hand ?

in API 1 test use:

//storing all orderids to a list 
pm.variable.set("orderIDList",pm.response.json().List.map(elem=>elem.OrderId))

in pre-request of API2

//Getting orderID from the list in the left to right order
let orderIDList = pm.variable.get("orderIDList")
pm.variables.set("orderId", orderIDList.shift())
pm.variables.set("orderIDList", orderIDList)
// Doing sendRequest till all orderIDs are executed
orderIDList.length ? postman.setNextRequest(pm.info.requestName):null

In test script of API2

//storing PartNumber for each orderID as a List of object coontaining 
//orderID:[partnumber1,partnumber2]
let obj = {}
obj[pm.variables.get("orderId")] =  pm.response.json().map(elem=>elem.PartNumber)
let partNumberList = pm.variables.get("partNumberList") 
partNumberList ? partNumberList.push(obj) : partNumberList=[obj]
pm.variables.set("partNumberList",partNumberList) 

and in Pre request of API3

//sending each orderid with the full PartNumberList
let partNumberList = pm.variable.get("partNumberList")
let partNumberMap = partNumberList.shift()
pm.variables.set("orderId", Object.keys(partNumberMap).shift())
pm.variables.set("partNumbers", partNumberMap[orderId])
pm.variables.set("partNumberList", partNumberList)  

partNumberList.length ? postman.setNextRequest(pm.info.requestName):null
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • In test script of API2, I think it should be `let orderId = pm.variables.get("orderId"); obj[orderId] = pm.response.json().map(elem=>elem.PartNumber)`. Also shouldn't it be `pm.variables.set("partNumberList", JSON.stringify(partNumberList)) `? Also in `partNumberList ? partNumberList.push(obj) : [obj]`, should it be `push()`? – ontherocks Jul 07 '21 at 11:11
  • you dont have to stringify before storing to variable, so when you retrieve it it will be already an array. don't need json.parse. all other things are corret as it is give it a try :) – PDHide Jul 07 '21 at 11:29
  • In test script of API2 I get `ReferenceError: orderId is not defined` – ontherocks Jul 07 '21 at 11:39
  • In Pre-request of API3 I am getting `Cannot read property 'shift' of undefined` – ontherocks Jul 07 '21 at 12:44
  • @ontherocks you should run the scripts in collection runner are you running in clllectio nrunner ? – PDHide Jul 07 '21 at 12:50
  • Yes, I am using collection runner. Could we please take this in chat? – ontherocks Jul 07 '21 at 13:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234622/discussion-between-ontherocks-and-pdhide). – ontherocks Jul 07 '21 at 13:40