From 39735119036949f672717c032a968da66d9a2bfb Mon Sep 17 00:00:00 2001 From: Andrew Prentice Date: Wed, 24 Aug 2016 20:03:30 -0400 Subject: [PATCH] feat(dmuse): cached response won't decrement usage --- build/cmds/dmuse_cmds/get.js | 16 ++++++++++++---- src/cmds/dmuse_cmds/get.js | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/build/cmds/dmuse_cmds/get.js b/build/cmds/dmuse_cmds/get.js index 58a92ec..4157c83 100644 --- a/build/cmds/dmuse_cmds/get.js +++ b/build/cmds/dmuse_cmds/get.js @@ -92,6 +92,11 @@ exports.handler = function (argv) { var ctstyle = _.get(chalk, theme.content.style); http({ url: url }, function (error, response) { if (!error && response.statusCode === 200) { + if (response.headers['x-gg-state'] === 'cached') { + config.dmuse.date.remain++; + noon.save(CFILE, config); + if (config.usage) console.log('Cached response, not decrementing usage.'); + } var resp = JSON.parse(response.body); for (var i = 0; i <= resp.length - 1; i++) { var item = resp[i]; @@ -111,10 +116,13 @@ exports.handler = function (argv) { } } if (argv.o) tools.outFile(argv.o, argv.f, tofile); - if (reset) { - console.log(config.dmuse.date.remain + '/' + config.dmuse.date.limit + ' requests remaining today.'); - } else { - if (config.usage) console.log(config.dmuse.date.remain + '/' + config.dmuse.date.limit + ' requests remaining today, will reset in ' + (23 - hours) + ' hours, ' + (59 - minutes) + ' minutes.'); + if (config.usage) { + if (reset) { + console.log('Timestamp expired, reset usage limits.'); + console.log(config.dmuse.date.remain + '/' + config.dmuse.date.limit + ' requests remaining today.'); + } else { + console.log(config.dmuse.date.remain + '/' + config.dmuse.date.limit + ' requests remaining today, will reset in ' + (23 - hours) + ' hours, ' + (59 - minutes) + ' minutes.'); + } } } else { throw new Error('HTTP ' + response.statusCode + ': ' + error); diff --git a/src/cmds/dmuse_cmds/get.js b/src/cmds/dmuse_cmds/get.js index 5f7bcbf..0f38686 100644 --- a/src/cmds/dmuse_cmds/get.js +++ b/src/cmds/dmuse_cmds/get.js @@ -89,6 +89,11 @@ exports.handler = (argv) => { const ctstyle = _.get(chalk, theme.content.style) http({ url }, (error, response) => { if (!error && response.statusCode === 200) { + if (response.headers['x-gg-state'] === 'cached') { + config.dmuse.date.remain++ + noon.save(CFILE, config) + if (config.usage) console.log('Cached response, not decrementing usage.') + } const resp = JSON.parse(response.body) for (let i = 0; i <= resp.length - 1; i++) { const item = resp[i] @@ -108,10 +113,13 @@ exports.handler = (argv) => { } } if (argv.o) tools.outFile(argv.o, argv.f, tofile) - if (reset) { - console.log(`${config.dmuse.date.remain}/${config.dmuse.date.limit} requests remaining today.`) - } else { - if (config.usage) console.log(`${config.dmuse.date.remain}/${config.dmuse.date.limit} requests remaining today, will reset in ${23 - hours} hours, ${59 - minutes} minutes.`) + if (config.usage) { + if (reset) { + console.log('Timestamp expired, reset usage limits.') + console.log(`${config.dmuse.date.remain}/${config.dmuse.date.limit} requests remaining today.`) + } else { + console.log(`${config.dmuse.date.remain}/${config.dmuse.date.limit} requests remaining today, will reset in ${23 - hours} hours, ${59 - minutes} minutes.`) + } } } else { throw new Error(`HTTP ${response.statusCode}: ${error}`)