Last active
December 20, 2015 14:49
-
-
Save jamesflorentino/6149282 to your computer and use it in GitHub Desktop.
client.js
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
/** client.js **/ | |
var https = require('https'); | |
var url = require('url'); | |
function Client() {} | |
/** | |
* All initialization code should be here. | |
*/ | |
Client.prototype.initialize = function() {}; | |
/** | |
* Creates a base request to a remote server. | |
* @param {Object} params | |
*/ | |
Client.prototype.request = function(params) { | |
var type = params.type; // GET, POST, DELETE, UPDATE | |
var url = params.url; | |
var success = params.success; | |
var req = https.request(url, function(result) { | |
var data = ''; | |
result.on('data', function(chunk) { | |
data += chunk; | |
}); | |
result.on('end', function() { | |
if (typeof success === 'function') success(data); | |
}); | |
}); | |
req.on('error', function(err) { | |
throw err; | |
}); | |
req.end(); | |
}; | |
/** | |
* Creates a GET request | |
* @param {String} url | |
* @param {Function} callback | |
*/ | |
Client.prototype.get = function(url, callback) { | |
this.request({ | |
type: 'GET', | |
url: url, | |
success: function(result) { | |
if (typeof callback === 'function') callback(result); | |
} | |
}); | |
}; | |
/** | |
* A factory method for instantiating the client class. | |
*/ | |
Client.create = function() { | |
return new Client(); | |
}; | |
module.exports = Client.create(); |
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
/** Example Usage **/ | |
var client = require('./client'); | |
var url = 'https://api.fastspring.com/company/greenheartgames/order/GRE130427-9394-85103/item/Game%20Dev%20Tycoon?user=ghgapiacc&pass=NPFkPGUk3NPFkPGUk'; | |
client.get(url, function(result) { | |
console.log('result', result); // xml or json result | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment