I am trying to create a lambda API Call that will go through the results of the DynamoDB table and execute different API calls to 3rd party systems.
The function for scanning is running correctly as it returns me the correct data back.
I have a problem with Iteration here as it wouldn't trigger an Axios call to Linkedin, however, the function is being called console.log bidIt would show in cloud watch.
The cloud watch is not displaying any error.
Ideally function below when finished and added one more API call + modification to dynamo DB will be something like a cron job that will fire every x amount of times within an hour.
If anyone can suggest how can I handle this issue / suggest a different approach I would greatly appreciate it.
const dynamodb = require("aws-sdk/clients/dynamodb");
const axios = require("axios");
const docClient = new dynamodb.DocumentClient();
const tableName = "**************";
exports.executeJobs = async(event) => {
const { httpMethod, path } = event;
if (httpMethod !== "GET") {
throw new Error(
`Method only accepts GET method, you tried: ${httpMethod} method.`
);
}
console.log("received:", JSON.stringify(event));
var params = {
TableName: tableName,
ProjectionExpression: "#timestamper, #userId,#campaignId, #type,#info,#token",
FilterExpression: "#timestamper < :timestamper",
ExpressionAttributeNames: {
"#timestamper": "timestamper",
"#type": "type",
"#info": "info",
"#userId": "userId",
"#campaignId": "campaignId",
"#token": "token",
},
ExpressionAttributeValues: {
":timestamper": Date.now(),
},
};
console.log("Scanning Jobs table.");
let scanResults = [];
let items;
do {
items = await docClient.scan(params).promise();
items.Items.forEach(async function (item) {
scanResults.push(item)
try {
const response = await bidIt(item.campaignId, item.info.currency, item.token, item.info.bid);
console.log(response);
} catch (error) {
console.error(error);
}
});
params.ExclusiveStartKey = items.LastEvaluatedKey;
} while (typeof items.LastEvaluatedKey != "undefined");
const response = {
statusCode: 200,
body: JSON.stringify(scanResults),
};
console.log(
`response from: ${path} statusCode: ${response.statusCode} body: ${response.body}`
);
return response;
};
async function bidIt(campaignId, currency, token, bid) {
console.log("bidIT");
try {
axios
.post(
"https://api.linkedin.com/v2/adCampaignsV2/" + campaignId, {
patch: {
$set: {
unitCost: {
amount: bid,
currencyCode: currency,
},
},
},
}, {
headers: {
"Content-Type": "application/json",
"X-RestLi-Method": "PARTIAL_UPDATE",
Authorization: "Bearer " + token,
},
}
)
.then((result) => {
return UpdateCpc(campaignId, currency, token, bid);
});
} catch (error) {
console.log("error", error);
// appropriately handle the error
}
}
Update: Thanks, Kalev for your reply, I have modified code however that didn't still didn't wait for API call to be made and it closed query. Screenshot attached from cloud watch. (I changed bidIt to getMinBid name)
async function getMinBid(campaignId, currency, token, bid) {
try {
console.log("getMinBid");
let result = await axios.post(
"https://api.linkedin.com/v2/adCampaignsV2/" + campaignId, {
patch: {
$set: {
unitCost: {
amount: bid,
currencyCode: currency,
},
},
},
}, {
headers: {
"Content-Type": "application/json",
"X-RestLi-Method": "PARTIAL_UPDATE",
Authorization: "Bearer " + token,
},
}
)
.then((result) => {
console.log("axios success");
console.log(JSON.stringify(result));
let minCost = result.data.split("lower than ").pop();
minCost = parseFloat(minCost) + bid;
});
return UpdateCpc(campaignId, currency, token, minCost);
} catch (error) {
console.log('test bid')
console.log("error", error);
// appropriately handle the error
}
}
