-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (52 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"use strict";
var request;
function handler(event, context, callback) {
var dryRun, config, pageRequestConfig, url;
// When using CloudWatch, the config is passed as an event using the
// "Constant (JSON text)" option. For clarity, rename here. It's already
// parsed into an object.
config = event;
if (config.dryRun) {
dryRun = true;
console.log("Enabling dry run mode.");
}
if (!config.urlTemplate) {
return callback(new Error("Missing `urlTemplate` parameter in JSON config."));
}
// Pass 'true' to always use UTC.
url = new Date().format(config.urlTemplate, true);
console.log("URL: " + url);
// Build a request
pageRequestConfig = {
url: url
};
if (config.method) {
pageRequestConfig.method = config.method;
}
if (config.username || config.password) {
pageRequestConfig.username = config.username;
pageRequestConfig.password = config.password;
}
// Make the request
if (dryRun) {
console.log("Aborting now. Dry run was enabled.");
return callback();
}
request(pageRequestConfig, function (error, response, body) {
if (error) {
console.log("Error reported: " + error.toString());
return callback(error);
}
console.log("Status code: " + response.statusCode);
console.log(body);
if (response.statusCode < 200 || response.statusCode > 299) {
return callback(new Error("Response code indicates error."));
}
callback();
});
}
require("date.format");
request = require("request");
module.exports = {
handler: handler
};