|
11 | 11 | */ |
12 | 12 | var request = require('request'); |
13 | 13 | var chalk = require('chalk'); |
14 | | -// var config = require('./config').config(); |
| 14 | +var debug = require('debug')('request'); |
15 | 15 | var pkg = require('../package'); |
16 | | - |
17 | | -var warning = chalk.yellow; |
18 | | -var succcess = chalk.green; |
19 | | -var error = chalk.red; |
20 | | -var info = chalk.blue; |
21 | | -var log = console.log; |
22 | 16 | var MAX_RETRY_LIMIT = 5; |
23 | 17 |
|
24 | 18 | function validate(req, cb) { |
@@ -53,34 +47,34 @@ var makeCall = module.exports = function(req, cb, RETRY) { |
53 | 47 | } else if (RETRY > MAX_RETRY_LIMIT) { |
54 | 48 | return cb(new Error('Max retry limit exceeded!')); |
55 | 49 | } |
| 50 | + debug(`Requesting API\n${JSON.stringify(req, null, 2)}`); |
56 | 51 | return request(req, function(err, response, body) { |
57 | 52 | if (err) { |
58 | | - log(error(err)); |
59 | 53 | return cb(err); |
60 | 54 | } |
61 | 55 | var timeDelay; |
62 | 56 | if (response.statusCode >= 200 && response.statusCode <= 399) { |
63 | | - log(succcess(`${req.method}: ${req.uri || req.url} completed succcessfully with ${response.statusCode} statusCode!`)); |
| 57 | + debug(`${req.method}: ${req.uri || req.url} completed succcessfully with ${response.statusCode} statusCode!`); |
64 | 58 | return cb(null, body); |
65 | 59 | } else if (response.statusCode === 429) { |
66 | 60 | timeDelay = Math.pow(Math.SQRT2, RETRY) * 100; |
67 | | - log(warning(`API rate limit exceeded.\nReceived ${response.statusCode} status\nBody ${JSON.stringify(body)}`)); |
68 | | - log(warning(`Retrying ${req.uri || req.url} with ${timeDelay} sec delay`)); |
| 61 | + debug(`API rate limit exceeded.\nReceived ${response.statusCode} status\nBody ${JSON.stringify(body)}`); |
| 62 | + debug(`Retrying ${req.uri || req.url} with ${timeDelay} sec delay`); |
69 | 63 | return setTimeout(function (req, cb, RETRY) { |
70 | 64 | return makeCall(req, cb, RETRY); |
71 | 65 | }, timeDelay, req, cb, RETRY); |
72 | 66 | } else if (response.statusCode >= 500) { |
73 | 67 | // retry, with delay |
74 | 68 | timeDelay = Math.pow(Math.SQRT2, RETRY) * 100; |
75 | | - log(warning(`SERVER ERROR\nRecevied ${response.statusCode} status from Contentstack!\nBody ${JSON.stringify(body)}`)); |
76 | | - log(warning(`Retrying ${req.uri || req.url} with ${timeDelay} sec delay`)); |
| 69 | + debug(`SERVER ERROR\nRecevied ${response.statusCode} status from Contentstack!\nBody ${JSON.stringify(body)}`); |
| 70 | + debug(`Retrying ${req.uri || req.url} with ${timeDelay} sec delay`); |
77 | 71 | RETRY++; |
78 | 72 | return setTimeout(function (req, cb, RETRY) { |
79 | 73 | return makeCall(req, cb, RETRY); |
80 | 74 | }, timeDelay, req, cb, RETRY); |
81 | 75 | } else { |
82 | | - log(error(`Request failed\n${JSON.stringify(req)}`)); |
83 | | - log(error(`Response received\n${JSON.stringify(body)}`)); |
| 76 | + debug(`Request failed\n${JSON.stringify(req)}`); |
| 77 | + debug(`Response received\n${JSON.stringify(body)}`); |
84 | 78 | return cb(body); |
85 | 79 | } |
86 | 80 | }); |
|
0 commit comments