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

abstracting

parent 75ef163f
......@@ -2,13 +2,14 @@
var qs = require('querystring');
var https = require('https');
//
// ### function Client (options)
// #### @options {Object} Options for this instance
// Constructor function for the Client base responsible
// for communicating with Challonge API
//
var errorHandler = require('./error-handler');
/*
### function Client (options)
#### @options {Object} Options for this instance
Constructor function for the Client base responsible
for communicating with Challonge API
*/
var Client = exports.Client = function(options) {
this.options = options;
......@@ -21,35 +22,38 @@ var Client = exports.Client = function(options) {
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) {
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 path = obj.path;
var format = this.options.get('format');
var method = obj.method;
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 = '';
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (typeof(obj[prop]) === 'object') {
for (var attr in obj[prop]) {
compiledParams += '&';
compiledParams += prop+'['+attr+']='+encodeURIComponent(obj[prop][attr]);
compiledParams += prop + '[' + attr + ']=' + encodeURIComponent(obj[prop][attr]);
}
propertiesToDelete.push(prop);
}
}
}
propertiesToDelete.forEach(function(prop){ delete obj[prop]; });
propertiesToDelete.forEach(function(prop) {
delete obj[prop];
});
//generate path
path = path + '.' + format + '?' + qs.stringify(obj) + compiledParams;
path = path + '.' + this.options.get('format') + '?' + qs.stringify(obj) + compiledParams;
var options = {
hostname: 'api.challonge.com',
......@@ -68,54 +72,18 @@ Client.prototype.makeRequest = function(obj) {
resData += chunk;
});
res.on('end', function() {
//error
if (res.statusCode !== 200) {
// 422 - Validation error(s) for create or update method - we can parse these
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);
errorHandler.handle(res, resData, callback);
return;
}
// 200 ok
if (format == 'json') { resData = JSON.parse(resData); }
if (self.options.get('format') == 'json') {
resData = JSON.parse(resData);
}
callback(err, resData);
});
});
req.end();
};
process.on('uncaughtException',function(error){
console.log(error);
console.log("hmph");
});
\ No newline at end of file
};
\ 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