|
| 1 | +var restify = require('restify'); |
| 2 | +var util = require('util'); |
| 3 | +var host = process.env.HOST || 'localhost'; |
| 4 | +var port = process.env.PORT; |
| 5 | +var ENV = process.env.NODE_ENV; |
| 6 | + |
| 7 | +if (!port) { |
| 8 | + if (host === 'localhost') { |
| 9 | + port = '8080'; |
| 10 | + } else { |
| 11 | + port = '80'; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function HttpClient(args) { |
| 16 | + args = args || {}; |
| 17 | + console.log(args); |
| 18 | + if (!args.host) { |
| 19 | + throw new Error('HTTP Client requires a host param'); |
| 20 | + } |
| 21 | + |
| 22 | + this.host = args.host; |
| 23 | + this.protocol = args.protocol || 'http'; |
| 24 | + this.port = args.port; |
| 25 | + this.url = 'http://' + this.host + ':' + this.port; |
| 26 | + |
| 27 | + this.client = restify.createStringClient({url: this.url}); |
| 28 | +} |
| 29 | + |
| 30 | +HttpClient.prototype.get = function(path_or_options, cb) { |
| 31 | + this.client.get(path_or_options, function(err, req, res, data) { |
| 32 | + cb(err, data, req, res); |
| 33 | + }); |
| 34 | +}; |
| 35 | + |
| 36 | +HttpClient.prototype.post = function(path_or_options, body, cb) { |
| 37 | + this.client.post(path_or_options, body, function(err, req, res, data) { |
| 38 | + cb(err, data, req, res); |
| 39 | + }); |
| 40 | +}; |
| 41 | + |
| 42 | +HttpClient.prototype.put = function(path_or_options, body, cb) { |
| 43 | + this.client.put(path_or_options, body, function(err, req, res, data) { |
| 44 | + cb(err, data, req, res); |
| 45 | + }); |
| 46 | +}; |
| 47 | + |
| 48 | +HttpClient.prototype.del = function(path_or_options, cb) { |
| 49 | + this.client.del(path_or_options, function(err, req, res, data) { |
| 50 | + cb(err, data, req, res); |
| 51 | + }); |
| 52 | +}; |
| 53 | + |
| 54 | +var http = { |
| 55 | + client: function() { |
| 56 | + return new HttpClient({host: host, port: port}); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +module.exports = http; |
| 61 | + |
0 commit comments