"use strict";
const config = require("./config.json");
const Promise = require("bluebird");
const apiFunctions = require("./api_functions");
module.exports.endpoint = async(event, _) => {
const vrm = event.pathParameters.vrm;
let responseData = await Promise.join(
apiFunctions.getTestData(config),
apiFunctions.getOtherTestData(config),
function(first, second) {
return {first: first, second:second};
}
);
return {
statusCode: 200,
body: JSON.stringify({
data: responseData
})
};
};
//From api_functions
const async = require("async");
const request = require("request");
module.exports = {
getTestData: async function(config) {
const requestFailed = true;
return new Promise(function(resolve, reject) {
async.auto({
todo1: function(callback) {
request("https://jsonplaceholder.typicode.com/todos/1", async.retryable(config.retryLimit, function(error, response, body) {
if (!error && response.statusCode === 200) {
callback(null, body);
} else {
callback(requestFailed, {});
}
}));
},
todo2: ["todo1", async.retryable(config.retryLimit, function(_, callback) {
request("https://jsonplaceholder.typicode.com/todos/1", function(error, response, body) {
if (!error && response.statusCode === 200) {
callback(null, body);
} else {
callback(requestFailed, {});
}
});
})],
todo3: ["todo1", async.retryable(config.retryLimit, function(_, callback) {
request("https://jsonplaceholder.typicode.com/todos/1", function(error, response, body) {
if (!error && response.statusCode === 200) {
callback(null, body);
} else {
callback(requestFailed, error);
}
});
})]
},
function(err, results) {
if (err) {
reject(results);
}
resolve(results);
});
});
},
getOtherTestData: async function(config) {
const requestFailed = true;
return new Promise(function(resolve, reject) {
async.auto({
post1: async.retryable(config.retryLimit, function(callback) {
request(`https://jsonplaceholder.typicode.com/posts`, function(error, response, body) {
if (!error && response.statusCode === 200) {
callback(null, body);
} else {
callback(requestFailed, {});
}
});
}),
post2: ["post1", async.retryable(config.retryLimit, function(_, callback) {
request("https://jsonplaceholder.typicode.com/posts", function(error, response, body) {
if (!error && response.statusCode === 200) {
callback(null, body);
} else {
callback(requestFailed, {});
}
});
})],
post3: ["post1", async.retryable(config.retryLimit, function(_, callback) {
request("https://jsonplaceholder.typicode.com/posts", function(error, response, body) {
if (!error && response.statusCode === 200) {
callback(null, body);
} else {
callback(requestFailed, {});
}
});
})]
},
function(err, results) {
if (err) {
reject(results);
}
resolve(results);
});
});
}
}