Commit 5b7f73ea authored by Aaron Tidwell's avatar Aaron Tidwell

comments

parent 95cfe9b2
...@@ -13,6 +13,7 @@ var errorHandler = require('./error-handler'); ...@@ -13,6 +13,7 @@ var errorHandler = require('./error-handler');
var Client = exports.Client = function(options) { var Client = exports.Client = function(options) {
this.options = options; this.options = options;
// add a getter to the options passed in - DO NOT mess with instance configs in resources
if (typeof this.options.get !== 'function') { if (typeof this.options.get !== 'function') {
this.options.get = function(key) { this.options.get = function(key) {
return this[key]; return this[key];
...@@ -20,19 +21,18 @@ var Client = exports.Client = function(options) { ...@@ -20,19 +21,18 @@ var Client = exports.Client = function(options) {
} }
}; };
var propertiesToDelete = ['callback', 'path', 'method']; var propertiesToDelete = ['callback', 'path', 'method']; // resources generate props internal to https requests
// cleans the passed in object, generates the API url/query-string, makes the request, delegates errors and calls callbacks // 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 self = this;
// cache vars that are about to be removed
// 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 method = obj.method; var method = obj.method;
obj.api_key = this.options.get('apiKey'); //convert for url
var self = this; // massage camel to underscore
obj.api_key = this.options.get('apiKey'); //convert for url
// serialize nested params to tournament[name] style // serialize nested params to tournament[name] style
var compiledParams = ''; var compiledParams = '';
...@@ -48,31 +48,35 @@ Client.prototype.makeRequest = function(obj) { ...@@ -48,31 +48,35 @@ Client.prototype.makeRequest = function(obj) {
} }
} }
// remove params
propertiesToDelete.forEach(function(prop) { propertiesToDelete.forEach(function(prop) {
delete obj[prop]; delete obj[prop];
}); });
//generate path // generate path
path = path + '.' + this.options.get('format') + '?' + qs.stringify(obj) + compiledParams; path = path + '.' + this.options.get('format') + '?' + qs.stringify(obj) + compiledParams;
// opts for the https call
var options = { var options = {
hostname: 'api.challonge.com', hostname: 'api.challonge.com',
path: path, path: path,
method: method, method: method,
headers: { headers: {
'Content-Length': 0 //server throww nginx error without a content-length 'Content-Length': 0 // server throws nginx error without a content-length
} }
}; };
console.log('making request to ', path, options); console.log('making request to ', path, options);
var req = https.request(options, function(res) { var req = https.request(options, function(res) {
// store the chunked data as it comes back
var resData = ''; var resData = '';
res.on('data', function(chunk) { res.on('data', function(chunk) {
resData += chunk; resData += chunk;
}); });
res.on('end', function() { res.on('end', function() {
//error // error
if (res.statusCode !== 200) { if (res.statusCode !== 200) {
errorHandler.handle(res, resData, callback); errorHandler.handle(res, resData, callback);
return; return;
...@@ -81,7 +85,7 @@ Client.prototype.makeRequest = function(obj) { ...@@ -81,7 +85,7 @@ Client.prototype.makeRequest = function(obj) {
if (self.options.get('format') == 'json') { if (self.options.get('format') == 'json') {
resData = JSON.parse(resData); resData = JSON.parse(resData);
} }
callback(err, resData); callback(null, resData); //no error, so no err object
}); });
}); });
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
// 422 - Validation error(s) for create or update method // 422 - Validation error(s) for create or update method
exports.handle = function(res, resData, callback) { exports.handle = function(res, resData, callback) {
var err;
// 401 - Invalid API key // 401 - Invalid API key
if (res.statusCode === 401) { if (res.statusCode === 401) {
err = { err = {
......
...@@ -6,7 +6,7 @@ var util = require('util'); ...@@ -6,7 +6,7 @@ var util = require('util');
var Client = require('./client').Client; var Client = require('./client').Client;
var Matches = exports.Matches = function(options) { var Matches = exports.Matches = function(options) {
Client.call(this, options); Client.call(this, options); // call parent constructor
}; };
// Inherit from Client base object // Inherit from Client base object
......
...@@ -4,11 +4,12 @@ ...@@ -4,11 +4,12 @@
// update PUT tournaments/:tournament/participants/:participant_id // update PUT tournaments/:tournament/participants/:participant_id
// destroy DELETE tournaments/:tournament/participants/:participant_id // destroy DELETE tournaments/:tournament/participants/:participant_id
// randomize GET tournaments/:tournament/participants/randomize // randomize GET tournaments/:tournament/participants/randomize
var util = require('util'); var util = require('util');
var Client = require('./client').Client; var Client = require('./client').Client;
var Participants = exports.Participants = function(options) { var Participants = exports.Participants = function(options) {
Client.call(this, options); Client.call(this, options); // call parent constructor
}; };
// Inherit from Client base object // Inherit from Client base object
......
...@@ -11,7 +11,7 @@ var util = require('util'); ...@@ -11,7 +11,7 @@ var util = require('util');
var Client = require('./client').Client; var Client = require('./client').Client;
var Tournaments = exports.Tournaments = function(options) { var Tournaments = exports.Tournaments = function(options) {
Client.call(this, options); Client.call(this, options); // call parent constructor
}; };
// Inherit from Client base object // Inherit from Client base object
......
...@@ -6,14 +6,15 @@ parts.forEach(function forEach(k) { ...@@ -6,14 +6,15 @@ parts.forEach(function forEach(k) {
exports[k] = require('./api/' + k.toLowerCase())[k]; exports[k] = require('./api/' + k.toLowerCase())[k];
}); });
// /*
// ### function createClient(options) ### function createClient(options)
// #### @options {Object} options for the clients #### @options {Object} options for the clients
// Generates a new API client. Generates a new API client.
// */
exports.createClient = function createClient(options) { exports.createClient = function createClient(options) {
var client = {}; var client = {};
// require each lib in ./api and instantiate a new instance of each object, passing the options we were passed
parts.forEach(function generate(k) { parts.forEach(function generate(k) {
var endpoint = k.toLowerCase(); var endpoint = k.toLowerCase();
client[endpoint] = new exports[k](options); client[endpoint] = new exports[k](options);
......
...@@ -203,7 +203,6 @@ function mupdate() { ...@@ -203,7 +203,6 @@ function mupdate() {
index(); index();
//show();
//create(); //create();
//show(); //show();
//update(); //update();
......
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