Last active
January 1, 2016 02:19
-
-
Save tresni/8078603 to your computer and use it in GitHub Desktop.
node-zendesk with redis caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var zendesk = require('node-zendesk'), | |
redis = require('redis').createClient(), | |
util = require('util'), | |
qs = require('querystring'), | |
_ = require('underscore'); | |
config = require('./config'); | |
redis.select(config.redis.zendesk.index); | |
function createKey(args, everything, uri) { | |
var temp = _.toArray(args), | |
params = '', | |
subdomain = /https?:\/\/([^.]+)\./.exec(uri)[1]; | |
// Let's not modify what is passed by reference... | |
temp[1] = _.clone(temp[1]); | |
if (_.isObject(temp[1]) && _.isArray(temp[1])) { | |
params = temp[1].pop(); | |
if (_.isObject(params)) { | |
params = qs.stringify(params); | |
} | |
temp[1].push(params); | |
} | |
return util.format("%s:%s:%s:%s:%s", | |
config.redis.zendesk.prefix, | |
subdomain, | |
everything ? "FULL" : "SINGLE", | |
temp[0], | |
temp[1].join("::") | |
); | |
} | |
exports.createClient = function (opts) { | |
opts.debug = true; | |
var client = zendesk.createClient(opts); | |
// Cheating but this *should* work :D | |
var _request = null, | |
_requestAll = null; | |
function useRedisItsAwesome(method) { | |
var args = _.toArray(arguments); | |
var processor = args.pop(); | |
var everything = args.pop(); | |
if (method != "GET") { | |
return processor.apply(this, arguments); | |
} | |
var key = createKey(args, everything, opts.remoteUri); | |
var cb = args.pop(); | |
var self = this; | |
redis.get(key, function(err, data) { | |
data = JSON.parse(data); | |
if (data) { | |
return cb(null, 200, data.body, null, data.res); | |
} | |
args.push(function (err, status, body, response, res) { | |
if (!_.isArray(status)) { | |
status = [ status ]; | |
} | |
if (_.every(status, function (item) { | |
return item == 200; | |
})) { | |
redis.set(key, | |
JSON.stringify({ | |
body: body, | |
res: res | |
}), "EX", "300", function () { | |
cb(err, status, body, response, res); | |
}); | |
} | |
else { | |
cb(err, status, body, response, res); | |
} | |
}); | |
processor.apply(self, args); | |
}); | |
} | |
function hijackedRequest() { | |
var args = _.toArray(arguments); | |
args.push(false); | |
args.push(_request); | |
useRedisItsAwesome.apply(this, args); | |
} | |
function hijackRequestAll() { | |
var args = _.toArray(arguments); | |
args.push(true); | |
args.push(_requestAll); | |
useRedisItsAwesome.apply(this, args); | |
} | |
for (var x in client) { | |
if (!_request) { _request = client[x].request; } | |
if (!_requestAll) { _requestAll = client[x].requestAll; } | |
client[x].request = hijackedRequest; | |
client[x].requestAll = hijackRequestAll; | |
} | |
return client; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment