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

comments

parent 95cfe9b2
......@@ -13,6 +13,7 @@ var errorHandler = require('./error-handler');
var Client = exports.Client = function(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') {
this.options.get = function(key) {
return this[key];
......@@ -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
Client.prototype.makeRequest = function(obj) {
var err;
// clean up the object to get ready to send it to the API
var self = this;
// cache vars that are about to be removed
var callback = obj.callback;
var path = obj.path;
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
var compiledParams = '';
......@@ -48,31 +48,35 @@ Client.prototype.makeRequest = function(obj) {
}
}
// remove params
propertiesToDelete.forEach(function(prop) {
delete obj[prop];
});
//generate path
// generate path
path = path + '.' + this.options.get('format') + '?' + qs.stringify(obj) + compiledParams;
// opts for the https call
var options = {
hostname: 'api.challonge.com',
path: path,
method: method,
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);
var req = https.request(options, function(res) {
// store the chunked data as it comes back
var resData = '';
res.on('data', function(chunk) {
resData += chunk;
});
res.on('end', function() {
//error
// error
if (res.statusCode !== 200) {
errorHandler.handle(res, resData, callback);
return;
......@@ -81,7 +85,7 @@ Client.prototype.makeRequest = function(obj) {
if (self.options.get('format') == 'json') {
resData = JSON.parse(resData);
}
callback(err, resData);
callback(null, resData); //no error, so no err object
});
});
......
......@@ -7,6 +7,7 @@
// 422 - Validation error(s) for create or update method
exports.handle = function(res, resData, callback) {
var err;
// 401 - Invalid API key
if (res.statusCode === 401) {
err = {
......
......@@ -6,7 +6,7 @@ var util = require('util');
var Client = require('./client').Client;
var Matches = exports.Matches = function(options) {
Client.call(this, options);
Client.call(this, options); // call parent constructor
};
// Inherit from Client base object
......
......@@ -4,11 +4,12 @@
// update PUT tournaments/:tournament/participants/:participant_id
// destroy DELETE tournaments/:tournament/participants/:participant_id
// randomize GET tournaments/:tournament/participants/randomize
var util = require('util');
var Client = require('./client').Client;
var Participants = exports.Participants = function(options) {
Client.call(this, options);
Client.call(this, options); // call parent constructor
};
// Inherit from Client base object
......
......@@ -11,7 +11,7 @@ var util = require('util');
var Client = require('./client').Client;
var Tournaments = exports.Tournaments = function(options) {
Client.call(this, options);
Client.call(this, options); // call parent constructor
};
// Inherit from Client base object
......
......@@ -6,14 +6,15 @@ parts.forEach(function forEach(k) {
exports[k] = require('./api/' + k.toLowerCase())[k];
});
//
// ### function createClient(options)
// #### @options {Object} options for the clients
// Generates a new API client.
//
/*
### function createClient(options)
#### @options {Object} options for the clients
Generates a new API client.
*/
exports.createClient = function createClient(options) {
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) {
var endpoint = k.toLowerCase();
client[endpoint] = new exports[k](options);
......
......@@ -203,7 +203,6 @@ function mupdate() {
index();
//show();
//create();
//show();
//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