How to Loop Through API Calls - javascript

I am trying to loop through coordinates of an API endpoint and test each response. When I send the request when it is not nested in for loops it works just fine, however, it doesn't seem to send when nested.
How can I automate the testing of this endpoint with various coordinates?
const request = require('request')
const domain = 'host.local'
const port = '8085'
const url = 'http://' + domain + ':' + port + '/v1/vend/item'
const parameters = {
coordinate: {
x: null,
y: null
},
network: {
user: "config",
role: "admin"
}
}
const x_coordinates = [1,2,3,4,5]
const y_coordinates = [6,7,8,9,10]
let options = {
method: 'post',
body: parameters,
json: true,
url: url
}
for (item in x_coordinates) {
parameters.coordinate.x = parseInt(item) + 1
for (item in y_coordinates.length) {
parameters.coordinate.y = parseInt(item) + 7
sleep(10000)
request(options, (err, res, body) => {
var headers = res.headers
var statusCode = res.statusCode
})
}
}
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break
}
}
}
Alternative promise method
for(let i=0; i<x_coordinates.length; i++) {
body.coordinate.x = i
for(let j=0; j<y_coordinates.length; j++) {
body.coordinate.y = j
let options = {
url: 'http://' + domain + ':' + port + '/v1/vend/item',
method: 'post',
json: true,
body: body
}
ps.push(rp(options))
}
}
Promise.all(ps)
.then((results) => {
console.log(results)
})
.catch(err => {
console.log(err)
})
This implementation of promises sent all the requests at once. They need a delay between them. Ideally, once the first request gets a response, the second is sent.

I like to use a little helper function called chainAsync:
https://github.com/30-seconds/30-seconds-of-code#chainasync
Here it is written a bit less densely:
function chainAsync(arrayOfFunctions){
let currentFunctionIndex = 0
const lastFunction = arrayOfFunctions[arrayOfFunctions.length - 1]
goToNextFunction()
function goToNextFunction(){
const currentFunction = arrayOfFunctions[currentFunctionIndex]
if(currentFunction == lastFunction){
currentFunction()
}else{
currentFunction(goToNextFunction)
currentFunctionIndex += 1
}
}
}
You can use it like this:
chainAsync([
function(goToNextFunction){
request(options, (err, res, body)=>{
// Handle the response. Then...
goToNextFunction()
})
},
function(goToNextFunction){
request(options, (err, res, body)=>{
// Handle the response. Then...
goToNextFunction()
})
},
function(){
request(options, (err, res, body)=>{
// Handle the response. Then...
// ...don't go to next function, since there isn't a next function!
})
}
])
This way you have control over the order in which these asynchronous functions take place.
Here's one way of using it to address your use-case:
const requestsToExecute = []
x_coordinates.forEach(x=>{
y_coordinates.forEach(y=>{
const currentRequest = function(goToNextRequest){
const requestOptions = {
url: 'http://host.local:8085/v1/vend/item',
method: 'POST',
json: true,
body: {
coordinate: {x, y},
network: {
user: 'config',
role: 'admin'
}
}
}
request(requestOptions, (err, response, body)=>{
// Handle the response, then...
// ...if there's another request...
if(goToNextRequest){
// ...use setTimeout to wait 1e7 seconds before going to the next request
setTimeout(goToNextRequest, 1e7)
}
})
}
requestsToExecute.push(currentRequest)
})
})
chainAsync(requestsToExecute)

Related

I cannot call an API inside for loop using nodejs

I'm trying to call an API inside a for loop using Nodejs,when the code is executed only the last element is called by the API:
the code :
var array=[12,124,852,256,5677,256,5679,2546,567,28,574]
for(var i=0;i<array.length;i=i++){
var b = array.splice(i,3);
const parameters1 = {
Ids: b.toString(),
limit: 45,
}
const get_request_args1 = querystring.stringify(parameters1);
const options1 = {
method: 'GET',
host: "host",
port: '443',
path: path + '?' + get_request_args1,
headers: {
'Accept': 'application/json',
'authorization': `Bearer ${token}`,
'Accept-Encoding': 'identity',
}
}
var req = http.request(options1, (res) => {
context.log("API CALL...",i);
var body = "";
var pages = 0;
var offset = [];
var limit = 100000;
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
const obj = JSON.parse(body);
//context.log('total pages 3 :', pages);
context.log('total :', obj.total);
context.res = { body: offset };
context.done();
});
}).on("error", (error) => {
context.log('ERROR :', error);
context.res = {
status: 500,
body: error
};
context.done();
});
}
when this code is executed only the last element in the array executed by the API, what I'm looking for is executing the api for each iteration of the for loop, any helps please ?
Not sure how your full function looks like, but you should build your function as fully structured as async-await.
And also you could use map function instead of for.
const yourFunction = async () => {
try {
const array = [12,124,852,256,5677,256,5679,2546,567,28,574];
const requests = array.map(async (item) => {
...
var req = await http.request(async options1, (res) => {
context.log("API CALL...",i);
...
});
await Promise.all(requests);
...
} catch (err) {
console.error(err);
}
}

here i am getting await can only use inside async function error but i am using async in my function

const express = require('express')
const router = express.Router()
const request = require('request')
const endponits = require('../sub/endpoints')
const status = require('../sub/status')
const db = require('../util/db')
const util = require('../util/util')
const CryptoJS = require('crypto-Js')
const fetch = require('node-fetch')
const notify = router.post('/', async (req, res) => {
console.log(req.body);
const CLIENT_SECRET = process.env.PAYMENT_TEST_SECRET_KEY;
// const amt = req.body.orderAmount;
let postData = {
oid: req.body.orderId,
amt: req.body.orderAmount,
rsn: req.body.txMsg,
tt: req.body.txTime
}
let signData =
req.body.orderId +
req.body.orderAmount +
req.body.referenceId +
req.body.txStatus +
req.body.paymentMode +
req.body.txMsg +
req.body.txTime;
// const postData = {
// oid: req.body.orderId,
// amt: req.body.orderAmount,
// refId: req.body.referenceId,
// sts: req.body.txStatus,
// pm: req.body.paymentMode,
// tm: req.body.txMsg,
// tt: req.body.txTime,
// signature: req.body.signature
// }
// var keys = Object.keys(postData);
// var signature = postData.signature;
// keys.sort();
// var signatureData = "";
// keys.forEach((key) => {
// if (key != "signature") {
// signatureData += postData[key];
// }
// });
// var computedSignature = crypto.createHmac('sha256', CLIENT_SECRET).update(signatureData).digest('base64');
// if (computedSignature == signature) {
let sdata = util.computeSign(signData);
if (sdata == req.body.signature) {
let data = {
sts: 'Inprogress',
//'so.pm': req.body.paymentMode || '',
//'so.refId': req.body.referenceId || '',
//uAt: Date.now()
}
db.getref(postData.oid, 'txn', successFunc => {
if (successFunc) {
const txnid = successFunc.id;
const appId = successFunc.appid;
db.updateById(
txnid,
data,
'txn',
success => {
if (success) {
let payload = {}
payload['txnId'] = txnid;
let PAYOUT_URI = 'https://ap.moneyorder.ws/api/v1/payout/test'
let Token = 'ceobrtoen'
let options = {
method: 'POST',
body: JSON.stringify(payload),
headers: {
appid: appId,
token: Token
}
}
try {
let response = await fetch(PAYOUT_URI, options)
let tokenres = await response.json()
//here we call payout Api
// let payload = { txnId: txnid };
// let Token = 'ceobrtoen';
// const PAYOUT_URI = 'https://ap.moneyorder.ws/api/v1/payout/test
// let options = {
// method: 'POST',
// url: PAYOUT_URI,
// body: JSON.stringify(payload),
// headers: {
// appid: appId,
// token: Token
// }
// }
// request(options, (err, response, body) => {
// if (err) {
// res
// .status(status.HTTPS.SERVER_ERROR)
// .json({ msg: 'Something went wrong.' })
// } else {
// let data = JSON.parse(body)
// console.log(data);
// console.log(options.body);
if (tokenres && tokenres.status === 'SUCCESS') {
// 3. update txn record
let updateObj = {
sts: 'Success',
}
db.updateById(
txnid,
updateObj,
'txn',
success => {
if (success) {
cosole.log("payout updated")
} else {
res.status(status.HTTPS.SERVER_ERROR).json({
data: null,
msg: 'Something went wrong at our end.',
success: false
})
}
},
err => {
res.status(status.HTTPS.SERVER_ERROR).json({
data: null,
msg: 'Something went wrong at our end.',
success: false
})
}
)
} else {
res.status(status.HTTPS.SERVER_ERROR).json({
data: null,
msg: "ERROR 1",
success: false
})
}
} catch (error) {
res.status(status.HTTPS.SERVER_ERROR).json({
data: null,
msg: 'Something went wrong at our end.',
success: false
})
}
// })
}
},
err => {
res
.status(status.HTTPS.BAD_REQUEST)
.json({ success: false, msg: 'error 404', data: null })
}
)
} else {
res
.status(status.HTTPS.BAD_REQUEST)
.json({ success: false, msg: "empty response" })
}
})
} else {
console.log(signData)
console.log(sdata)
}
})
module.exports = router
here I am using async and await but I am getting await only can be used inside an async function where I am wrong in this I am trying to hit other API but I am not getting success.i have also use request module instead of node-fetch but it is not working . can anybody tell me where I am wrong.......................................................................................................................................................
The success function of your db.updateById() isn't async, so you can't use await inside of it.
Also, consider abstracting those callback-style db functions, wrapping them in promises. That way, you can use async-await on the main flow of your application rather than nesting callbacks.
Please note you are doing an await inside success callback function of db.updateById()
Which should be async. Try this.
.
db.updateById( txnid,data, 'txn',
async (success) => {
},
async (err)=> {
}
}
if it dosent work and you can not make the success callback async for some reason
just return another function in the callback and make that async
db.updateById( txnid,data, 'txn',
success => {
(async () => {
})()
},
err => {
}
}

JS Functions to make an API call

I have the following code.
I'm trying to make an API call (retrieve) passing since (obj.since), therefore, every time I make the call the API does not retrieve all data. However, so far, I haven't found the way to get since from the last record on my database.
var express = require("express");
var article = require("../models/article");
var request = require('request');
article.findOne({}, {since:1, _id:0}, { sort: { 'since' : -1 } }, function (err,obj) {
var **dataString** = `'{"consumer_key":"XXXXX", "access_token":"XXXXXXX", "since":"${obj.since}"}'`;
});
var options = {
url: 'https://xxxxxxxxx.com/v3/get',
method: 'POST',
headers: headers,
body: **dataString**
}
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
let package = JSON.parse(body);
for(var attributename in package.list){
var title = package.list[attributename]["given_title"] ;
var url = package.list[attributename]["given_url"] ;
var newArticle = {title: title, url: url, since: since}
article.create(newArticle, function(error, newlyCreated){
if(error){
console.log(error);
} else {
console.log(newlyCreated);
}
});
}
}
else {
console.log(error);
}
};;
request(options,callback)
How can I make an API call getting the obj.since from the database (MongoDB) and pass it to an object (options)?
You are doing async callback style operation in for loop which is causing this issue. I will change few things
Change findOne to have exec at the end so it returns promise
article.create already returns a promise if no callback specified.
Convert request to a promise style.
Use for..of loop to do async operation.
The code will look like this
var express = require("express");
var article = require("../models/article");
var request = require('request');
function hitApi(dataString) {
return new Promise((resolve, reject) => {
var options = {
url: 'https://xxxxxxxxx.com/v3/get',
method: 'POST',
headers: headers,
body: dataString
}
request(options, error, response, body => {
if (err) {
reject(err);
}
resolve(body);
});
});
}
async function perform() {
const dataString = await article.findOne({}, {since:1, _id:0}, { sort: { 'since' : -1 } }).exec();
const response = await hitApi(dataString);
const package = JSON.parse(response.body);
for (const attributename of package.list) {
var title = package.list[attributename]["given_title"] ;
var url = package.list[attributename]["given_url"] ;
var newArticle = {title: title, url: url, since: since}
const newlyCreated = await article.create(newArticle);
console.log(newlyCreated);
}
}
You can then call perform function. There might be few syntax error but you will get an idea.

Not getting data after dependent api call using async problem node

I am calling API to fetch data on the basis of that data I am calling different APIs which is based on condition.
I have used a promise to maintain the async code as well but no luck so far. At the end of the block, I want every data pushed inside array but I am getting a blank array.
async (body, nextCall) => {
var tmpArryOfModuleList = []
var bodyData = body.data
var promise = new Promise((resolve, reject) => {
bodyData.forEach(element => {
if (element.hasCRUD === '0') {
var options = {
method: 'GET',
url: `${apiURL}/api/fetchAllCharts`,
headers:
{
Authorization: token
}
};
request(options, function (error, response, dashboardData) {
if (error) {
return nextCall({
error: error
})
}
var parsedDashboardData = JSON.parse(dashboardData)
for (var i = 0; i < parsedDashboardData['data'].length; i++) {
var val = element.name + " - " + parsedDashboardData['data'][i]['name']
var randomID = Math.random().toString(36).slice(2)
tmpArryOfModuleList.push({ "_id": randomID, "submodule": val })
}
});
} else if (element.hasCRUD == '1') {
var options = {
method: 'GET',
url: `${apiURL}/api/fetchAllActions`,
headers:
{
Authorization: token
}
};
request(options, function (error, response, crudData) {
if (error) {
return nextCall({
error: error
})
}
var parsedcrudData = JSON.parse(crudData)
for (var i = 0; i < parsedcrudData['data'].length; i++) {
var val = element.name + " - " + parsedcrudData['data'][i]['name']
var randomID = Math.random().toString(36).slice(2)
tmpArryOfModuleList.push({ "_id": randomID, "submodule": val })
}
});
} else {
console.log('no data found')
reject('No Data Found')
}
})
resolve(tmpArryOfModuleList)
})
console.log('tmpArryOfModuleList', tmpArryOfModuleList)
}
What am I doing wrong? How can I achieve the result on the last array?

How do I tie together 2 asynchronous request.post calls in Express and NodeJS?

I'm still learning how to make calls with using Express. Single asynchronous calls are no problem, but now I have an example where I want to feed the result of one call into the next one.
My current code can be seen below, a really messy way that includes a forloop like function, a foreach and a Timeout. I'd like to learn the proper way to do this. Preferably something that scales.
The first call fills up a result object getWorkbooksResponse with a list of workbooks (incl. workbook id's and workbook names). The second part fires of a getViews call for each workbook in that list. The function checkResponse sorts through the views and puts them in alphabetical order by name.
What is the proper way to tie together 2 request.post calls?
I've been looking at next(), bluebird, async,... but some examples would definitely help.
var express = require('express');
var request = require('request');
var router = express.Router();
//initialize values
var workbookId = -1;
router.get('/workbooks/views', function(req, res) {
var workgroup = req.query.workgroup;
var authToken = req.query.auth_token;
var serverUrl = req.query.server_url;
//Assemble body for POST request...
var requestedBody = {
method: 'getWorkbooks',
params: {
page: {
startIndex: 0,
maxItems: 999
},
filter: {
operator: 'and',
clauses: []
}
}
};
//Send POST request...
request.post({
url: 'https://' + serverUrl + '/vizportal/api/web/v1/getWorkbooks',
body: JSON.stringify(requestedBody),
headers: {
'Cookie': 'workgroup_session_id=' + workgroup + '; XSRF-TOKEN=' + authToken,
'X-XSRF-TOKEN': authToken
}
}, function(err, response, body) {
body = JSON.parse(body);
var result = body.result;
if (result.errors) {
return res.json({
http_code: 401
});
} else {
getWorkbooksResponse = result;
var getViewsWorkbooksResponse = [];
var forloop = function(i) {
if (i < getWorkbooksResponse.totalCount) {
workbookId = getWorkbooksResponse.workbooks[i].id;
var workbookName = getWorkbooksResponse.workbooks[i].name;
request.post({
url: 'https://' + serverUrl + '/vizportal/api/web/v1/getViews',
body: JSON.stringify({
method: 'getViews',
params: {
page: {
startIndex: 0,
maxItems: 999
},
filter: {
operator: 'and',
clauses: [{
operator: 'eq',
field: 'workbookId',
value: workbookId
}]
}
}
}),
headers: {
'Cookie': 'workgroup_session_id=' + workgroup + '; XSRF-TOKEN=' + authToken,
'X-XSRF-TOKEN': authToken
}
}, function(err, response, body) {
body = JSON.parse(body);
var result = body.result;
if (result.errors) {
response = {
http_code: 401
};
} else {
result.views.forEach(function(view) {
view.workbookName = workbookName;
getViewsWorkbooksResponse.push(view);
});
}
});
forloop(i + 1);
} else {
var checkResponse = function() {
if (getViewsWorkbooksResponse) {
//Alphabetize Response array on view name
getViewsWorkbooksResponse.sort(function(a, b){
return a.name.localeCompare(b.name);
});
return res.json({
http_code: 200,
response: getViewsWorkbooksResponse
});
}
};
setTimeout(checkResponse, 1000);
}
};
if (getWorkbooksResponse.totalCount) {
forloop(0);
}
}
});
});
module.exports = router;
With promises it would be the simplest way:
You wrap your request.post calls in
new Promise((resolve,reject)=>
{ ...
request.post(...,function(err,response, body){
if (result.errors){reject(/* error info here */)}
else {resolve(/* data goes here */)}
}
});
And then use
Promise.all[promise1,...,promiseN].then(function(result){
//here is where you gonna do things after all requests are done
})

Categories

Resources