Created
February 3, 2014 13:18
-
-
Save imlucas/8783634 to your computer and use it in GitHub Desktop.
notes on postman scripting
This file contains hidden or 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
// Notes on how to extend [postman](http://www.getpostman.com/) to support | |
// sharing small util scripts, like fabric tasks, along with a collection | |
// so we have repeatables/easy access for non-tech folks. | |
// | |
// How to extend schema? Build on collection or a new ground up like | |
// environments? Struct looks something like: | |
var scripts = [ | |
{ | |
"name": "Continuous Health Check", | |
"description": "Runs health-check every 60 seconds and logs result", | |
"code": "<see below. note: code in json is pretty nasty...>" | |
}, | |
{ | |
"name": "Kill all", | |
"description": "Fetch all running process ids and kill them.", | |
"code": "<see below>" | |
} | |
] | |
function continuousHealthCheck(){ | |
setInterval(function(){ | |
postman('/health-check', function(err, res){ | |
console.log(new Date(), res.text}); | |
}, | |
60 * 1000); | |
} | |
function killall(){ | |
postman('list processes', {'running': true}, function(err, res){ | |
res.body.processes.map(function(proc){ | |
kill(proc.id); | |
}); | |
}); | |
} | |
function kill(id){ | |
postman('kill process', {'id': id}, function(err, res){ | |
console.log('Killed', res.body.process); | |
}); | |
} | |
// | |
// ## Wrapper function `postman` | |
// | |
// module.exports usage: | |
postman('/health-check', {'environment': 'dev'}, function(err, res){ | |
console.log(res.body); | |
}); | |
// global config/defaults style. | |
// using ctx to merge objects: | |
postman.ctx({ | |
'environment': 'dev', | |
'actor': 'lucas' | |
}); | |
// Or just a wildcard setter: | |
postman.actor = 'lucas'; | |
// Then we can just call `request(request.name)` to actually | |
// create a new agent request obj. | |
postman.request('/health-check').end(function(err, res){ | |
console.log(res.body); | |
}); | |
// ### impl notes | |
// | |
// probably just extend superagent? | |
var util = require('util'), | |
superagent = require('superagent'); | |
module.exports = Postman; | |
function Postman(name, ctx, fn){ | |
this.name = name; | |
this._ctx = ctx; | |
if(fn) return this.end(fn); | |
} | |
util.inherits(Postman, superagent.Agent); | |
Postman.prototype.ctx = function(obj){ | |
this.update(ctx, obj); | |
return this: | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment