UNPKG

1.15 kBJavaScriptView Raw
1var https = require('https'),
2 aws4 = require('aws4');
3
4module.exports = {
5 AWSRequest: function(data, done) {
6 var _ = this,
7 opts = {
8 service: 'apigateway',
9 region: _.aws.region,
10 method: data.method,
11 path: data.path
12 };
13
14 if (data.body) opts.body = JSON.stringify(data.body);
15
16 opts = aws4.sign(opts, {
17 accessKeyId: _.aws.accessKeyId,
18 secretAccessKey: _.aws.secretAccessKey
19 });
20
21 var req = https.request(opts, function(res) {
22 var body = '';
23
24 res.on('data', function(d) {
25 body += d;
26 });
27
28 res.on('end', function() {
29 body = JSON.parse(body);
30 if (body.logref) {
31 console.error(body.message);
32 return done(body, null);
33 }
34 return done(null, body);
35 });
36 });
37
38 req.on('error', function(err) {
39 done(err, null);
40 });
41
42 if (opts.body) req.write(opts.body);
43
44 req.end();
45 }
46};