Commit 758a0728 authored by Aaron Tidwell's avatar Aaron Tidwell

all working but randomize

parents
key.js
\ No newline at end of file
http://api.challonge.com/v1
shows particpants.randomize as GET when method is actually POST
http://api.challonge.com/v1/documents/participants/create
shows participant_id as a required field when the server does not respect passing this and it is not required
http://api.challonge.com/v1/documents/participants/randomize
shows participant_id as a required field when the server does not respect passing this and it is not required
server response has invalid headers causing [Error: Parse Error] bytesParsed: 0, code: 'HPE_INVALID_CONSTANT' in node
\ No newline at end of file
'use strict';
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 Client = exports.Client = function(options) {
this.options = options;
if (typeof this.options.get !== 'function') {
this.options.get = function(key) {
return this[key];
};
}
};
var propertiesToDelete = ['callback', 'path', 'method'];
//method used to actually make the request to the challonge servers
Client.prototype.makeRequest = function(obj) {
var err;
//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 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]);
}
propertiesToDelete.push(prop);
}
}
}
propertiesToDelete.forEach(function(prop){ delete obj[prop]; });
//generate path
path = path + '.' + format + '?' + qs.stringify(obj) + compiledParams;
var options = {
hostname: 'api.challonge.com',
path: path,
method: method,
headers: {
'Content-Length': 0
}
};
console.log('making request to ', path, options);
var req = https.request(options, function(res) {
var resData = '';
res.on('data', function(chunk) {
resData += chunk;
});
res.on('end', function() {
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);
return;
}
// 200 ok
if (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
// index GET tournaments/:tournament/matches
// show GET tournaments/:tournament/matches/:match_id
// update PUT tournaments/:tournament/matches/:match_id
var util = require('util');
var Client = require('./client').Client;
var Matches = exports.Matches = function(options) {
Client.call(this, options);
};
// Inherit from Client base object
util.inherits(Matches, Client);
Matches.prototype.index = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/matches';
delete obj.id;
obj.method = 'GET';
this.makeRequest(obj);
};
Matches.prototype.show = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/matches/'+obj.matchId;
delete obj.id;
delete obj.matchId;
obj.method = 'GET';
this.makeRequest(obj);
};
Matches.prototype.update = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/matches/'+obj.matchId;
delete obj.id;
delete obj.matchId;
obj.method = 'PUT';
this.makeRequest(obj);
};
\ No newline at end of file
// index GET tournaments/:tournament/participants
// create POST tournaments/:tournament/participants
// show GET tournaments/:tournament/participants/:participant_id
// 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);
};
// Inherit from Client base object
util.inherits(Participants, Client);
Participants.prototype.index = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants';
delete obj.id;
obj.method = 'GET';
this.makeRequest(obj);
};
Participants.prototype.create = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants';
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
};
Participants.prototype.show = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants/'+obj.participantId;
delete obj.id;
delete obj.participantId;
obj.method = 'GET';
this.makeRequest(obj);
};
Participants.prototype.update = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants/'+obj.participantId;
delete obj.id;
delete obj.participantId;
obj.method = 'PUT';
this.makeRequest(obj);
};
Participants.prototype.destroy = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants/'+obj.participantId;
delete obj.id;
delete obj.participantId;
obj.method = 'DELETE';
this.makeRequest(obj);
};
/***********BUSTED*********/
// [Error: Parse Error] bytesParsed: 0, code: 'HPE_INVALID_CONSTANT'
Participants.prototype.randomize = function(obj) {
obj.path = 'v1/tournaments/'+obj.id+'/participants/randomize';
delete obj.id;
obj.method = 'PUT';
this.makeRequest(obj);
}
\ No newline at end of file
// // index GET tournaments
// // create POST tournaments
// // show GET tournaments/:tournament
// // update PUT tournaments/:tournament
// // destroy DELETE tournaments/:tournament
// // start POST tournaments/:tournament/start
// // finalize POST tournaments/:tournament/finalize
// // reset POST tournaments/:tournament/reset
var util = require('util');
var Client = require('./client').Client;
var Tournaments = exports.Tournaments = function(options) {
Client.call(this, options);
};
// Inherit from Client base object
util.inherits(Tournaments, Client);
Tournaments.prototype.index = function(obj) {
obj.path = '/v1/tournaments';
obj.method = 'GET';
this.makeRequest(obj);
};
Tournaments.prototype.create = function(obj) {
obj.path = '/v1/tournaments';
obj.method = 'POST';
this.makeRequest(obj);
};
Tournaments.prototype.show = function(obj) {
obj.path = '/v1/tournaments/'+obj.id;
delete obj.id;
obj.method = 'GET';
this.makeRequest(obj);
};
Tournaments.prototype.update = function(obj) {
obj.path = '/v1/tournaments/'+obj.id;
delete obj.id;
obj.method = 'PUT';
this.makeRequest(obj);
};
Tournaments.prototype.destroy = function(obj) {
obj.path = '/v1/tournaments/'+obj.id;
delete obj.id;
obj.method = 'DELETE';
this.makeRequest(obj);
};
Tournaments.prototype.start = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/start';
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
};
Tournaments.prototype.finalize = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/finalize';
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
};
Tournaments.prototype.reset = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/reset';
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
};
\ No newline at end of file
'use strict';
var parts = ['Client', 'Tournaments', 'Participants', 'Matches'];
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.
//
exports.createClient = function createClient(options) {
var client = {};
parts.forEach(function generate(k) {
var endpoint = k.toLowerCase();
client[endpoint] = new exports[k](options);
});
return client;
};
\ No newline at end of file
{
"name": "node-challonge",
"description": "Wrapper for the challong api",
"author": "Aaron Tiwell <aaron.tidwell@gmail.com>",
"main": "./lib/challonge.js",
"dependencies": {
},
"devDependencies": {
},
"engine": "node >= 0.10.x"
}
\ No newline at end of file
##example
checkout this repo into ./
```
var challonge = require('./../');
var client = challonge.createClient({
apiKey: '***yourAPIKey***'
});
function index() {
client.tournaments.index({
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
```
\ No newline at end of file
var challonge = require('./../');
var client = challonge.createClient({
apiKey: require('./../key.js'),
format: 'json'
});
var tourneyName = 'tststny_2';
function index() {
client.tournaments.index({
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function create() {
client.tournaments.create({
tournament: {
name: 'name-'+tourneyName,
url: tourneyName,
signup_cap: 8,
tournament_type: 'single elimination',
},
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function show() {
client.tournaments.show({
id: tourneyName,
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function update() {
client.tournaments.update({
id: 'test-tourney',
tournament: {
name: 'renamed test tournet'
},
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function destroy() {
client.tournaments.destroy({
id: tourneyName,
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function start() {
client.tournaments.start({
id: tourneyName,
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function finalize() {
client.tournaments.finalize({
id: tourneyName,
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function reset() {
client.tournaments.reset({
id: tourneyName,
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function pindex() {
client.participants.index({
id: tourneyName,
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function pcreate() {
client.participants.create({
id: tourneyName,
participantId: 'arbitraryid',
participant: {
name: 'Tidwell'
},
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function pshow() {
client.participants.show({
id: tourneyName,
participantId: 10846707,
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function pupdate() {
client.participants.update({
id: tourneyName,
participantId: 10846707,
participant: {
name: 'updatdguy'
},
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function pdestroy() {
client.participants.destroy({
id: tourneyName,
participantId: 10846707,
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function prandomize() {
client.participants.randomize({
id: tourneyName,
callback: function(err,data){
if (err) { console.log('err', err); return; }
console.log(data);
}
});
}
function mindex() {
client.matches.index({
id: tourneyName,
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function mshow() {
client.matches.show({
id: tourneyName,
matchId: 15606254,
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
function mupdate() {
client.matches.update({
id: tourneyName,
matchId: 15606254,
match: {
scores_csv: '3-0',
winner_id: 10847219
},
callback: function(err,data){
if (err) { console.log(err); return; }
console.log(data);
}
});
}
index();
//show();
//create();
//show();
//update();
//destroy();
//start();
//finalize();
//reset();
//pindex();
//pshow();
//pupdate();
//pdestroy();
//prandomize();
//mindex();
//mshow();
//mupdate();
\ 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