Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Challonge
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Challonge
Commits
95cfe9b2
Commit
95cfe9b2
authored
Nov 24, 2013
by
Aaron Tidwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
abstracting
parent
75ef163f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
56 deletions
+82
-56
lib/api/client.js
lib/api/client.js
+24
-56
lib/api/error-handler.js
lib/api/error-handler.js
+58
-0
No files found.
lib/api/client.js
View file @
95cfe9b2
...
@@ -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 server
s
//
cleans the passed in object, generates the API url/query-string, makes the request, delegates errors and calls callback
s
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
lib/api/error-handler.js
0 → 100644
View file @
95cfe9b2
// 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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment