Commit 95cfe9b2 authored by Aaron Tidwell's avatar Aaron Tidwell

abstracting

parent 75ef163f
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
var qs = require('querystring'); var qs = require('querystring');
var https = require('https'); var https = require('https');
var errorHandler = require('./error-handler');
//
// ### function Client (options) /*
// #### @options {Object} Options for this instance ### function Client (options)
// Constructor function for the Client base responsible #### @options {Object} Options for this instance
// for communicating with Challonge API Constructor function for the Client base responsible
// for communicating with Challonge API
*/
var Client = exports.Client = function(options) { var Client = exports.Client = function(options) {
this.options = options; this.options = options;
...@@ -21,35 +22,38 @@ var Client = exports.Client = function(options) { ...@@ -21,35 +22,38 @@ var Client = exports.Client = function(options) {
var propertiesToDelete = ['callback', 'path', 'method']; var propertiesToDelete = ['callback', 'path', 'method'];
//method used to actually make the request to the challonge servers // cleans the passed in object, generates the API url/query-string, makes the request, delegates errors and calls callbacks
Client.prototype.makeRequest = function(obj) { Client.prototype.makeRequest = function(obj) {
var err; var err;
//clean up the object to get ready to send it to the API // clean up the object to get ready to send it to the API
var callback = obj.callback; var callback = obj.callback;
var path = obj.path; var path = obj.path;
var format = this.options.get('format');
var method = obj.method; var method = obj.method;
obj.api_key = this.options.get('apiKey'); //convert for url obj.api_key = this.options.get('apiKey'); //convert for url
//serialize nested params to tournament[name] style var self = this;
// serialize nested params to tournament[name] style
var compiledParams = ''; var compiledParams = '';
for (var prop in obj) { for (var prop in obj) {
if (obj.hasOwnProperty(prop)) { if (obj.hasOwnProperty(prop)) {
if (typeof(obj[prop]) === 'object') { if (typeof(obj[prop]) === 'object') {
for (var attr in obj[prop]) { for (var attr in obj[prop]) {
compiledParams += '&'; compiledParams += '&';
compiledParams += prop+'['+attr+']='+encodeURIComponent(obj[prop][attr]); compiledParams += prop + '[' + attr + ']=' + encodeURIComponent(obj[prop][attr]);
} }
propertiesToDelete.push(prop); propertiesToDelete.push(prop);
} }
} }
} }
propertiesToDelete.forEach(function(prop){ delete obj[prop]; }); propertiesToDelete.forEach(function(prop) {
delete obj[prop];
});
//generate path //generate path
path = path + '.' + format + '?' + qs.stringify(obj) + compiledParams; path = path + '.' + this.options.get('format') + '?' + qs.stringify(obj) + compiledParams;
var options = { var options = {
hostname: 'api.challonge.com', hostname: 'api.challonge.com',
...@@ -68,54 +72,18 @@ Client.prototype.makeRequest = function(obj) { ...@@ -68,54 +72,18 @@ Client.prototype.makeRequest = function(obj) {
resData += chunk; resData += chunk;
}); });
res.on('end', function() { res.on('end', function() {
//error
if (res.statusCode !== 200) { if (res.statusCode !== 200) {
// 422 - Validation error(s) for create or update method - we can parse these errorHandler.handle(res, resData, callback);
if (res.statusCode === 422) {
if (format == 'json') { resData = JSON.parse(resData); }
err = {
error: true,
errors: resData.errors,
statusCode: res.statusCode,
text: resData
};
callback(err, res);
return;
}
// 404 - Object not found within your account scope - we can parse this
if (res.statusCode === 404) {
err = {
error: true,
errors: [],
statusCode: res.statusCode,
text: 'Object not found within your account scope'
};
callback(err,res);
return;
}
// we cant parse the error
err = {
error: true,
errors: [],
statusCode: res.statusCode,
text: resData
};
// ship the response object back as the data
callback(err, res);
return; return;
} }
// 200 ok // 200 ok
if (format == 'json') { resData = JSON.parse(resData); } if (self.options.get('format') == 'json') {
resData = JSON.parse(resData);
}
callback(err, resData); callback(err, resData);
}); });
}); });
req.end(); req.end();
}; };
\ No newline at end of file
process.on('uncaughtException',function(error){
console.log(error);
console.log("hmph");
});
\ No newline at end of file
// Response Codes
// The following HTTP response codes are issued by the API. All other codes are the result of a request not reaching the application.
// 200 - OK
// 401 - Invalid API key
// 404 - Object not found within your account scope
// 422 - Validation error(s) for create or update method
exports.handle = function(res, resData, callback) {
// 401 - Invalid API key
if (res.statusCode === 401) {
err = {
error: true,
errors: [],
statusCode: res.statusCode,
text: 'Invalid API key'
};
callback(err,res);
return;
}
// 404 - Object not found within your account scope
if (res.statusCode === 404) {
err = {
error: true,
errors: [],
statusCode: res.statusCode,
text: 'Object not found within your account scope'
};
callback(err,res);
return;
}
// 422 - Validation error(s) for create or update method
if (res.statusCode === 422) {
if (format == 'json') { resData = JSON.parse(resData); }
err = {
error: true,
errors: resData.errors,
statusCode: res.statusCode,
text: resData
};
callback(err, res);
return;
}
// not an api-documented error
err = {
error: true,
errors: [],
statusCode: res.statusCode,
text: resData
};
// ship the response object back as the data
callback(err, res);
return;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment