Permalink
Browse files

Updated _get method.

* Removed keep-alive header.
* Removed full url handling.
* Removed lastModified handling.
* Added return for request stream.
* Added full response as third param of callback.
* Passed options object to request method.
* Method will now throw if options.region is not provided.
  • Loading branch information...
1 parent d3dadaa commit 83a6e08a2b56301e292c55c22fc039b380ddb1bb Christian Wesselhoeft committed Dec 16, 2012
Showing with 19 additions and 42 deletions.
  1. +19 −42 index.js
View
@@ -3,66 +3,43 @@ var request = require('request')
var armory = { privateKey: null, publicKey: null }
-// Makes request
+// Makes the request.
armory._get = function(path, options, callback) {
- var headers = { 'Connection': 'keep-alive' }
- , uri
+ options.headers = options.headers || {}
+ options.jar = false
+ options.json = true
- // Handle full URLs
- if (path.indexOf('http://') === 0) {
- uri = path
- path = uri.split('battle.net')[1]
+ path = '/api/wow' + path
- } else {
- if (options.locale) {
- options.query.push('locale=' + options.locale)
- }
-
- options.query = '?' + options.query.join('&')
- path = '/api/wow' + path
+ if (options.locale) { options.query.push('locale=' + options.locale) }
+ if (!options.region) { throw new Error('region must be provided') }
- uri = encodeURI('http://' + options.region + '.battle.net' + path +
- options.query)
- }
+ options.query = options.query.length ? '?' + options.query.join('&') : ''
- // Last-Modified
- if (options.lastModified) {
- headers['If-Modified-Since'] = new Date(options.lastModified)
- .toUTCString()
- }
+ options.uri = 'http://' + options.region + '.battle.net' + path
+ options.uri = encodeURI(options.uri + options.query)
// Authentication
if (this.privateKey && this.publicKey) {
var signature = crypto.createHmac('sha1', this.privateKey)
- signature.update(
- 'GET\n' +
- new Date().toUTCString() + '\n' +
- path + '\n'
- )
+ signature.update(['GET', new Date().toUTCString(), path].join('\n') + '\n')
- headers['Authorization'] = 'BNET ' + this.publicKey + ':' +
+ options.headers['Authorization'] = 'BNET ' + this.publicKey + ':' +
signature.digest('base64')
}
- request({ uri: uri, headers: headers }, function(err, res, body) {
- if (err || !body) {
-
- if (res && res.statusCode !== 304) {
- err = err || new Error(res.statusCode)
+ if (callback) {
+ var cb = function(err, res, body) {
+ if (body && body.status === 'nok') {
+ err = err || new Error(body.reason)
}
- return callback(err)
- }
-
- body = JSON.parse(body)
-
- if (body.status === 'nok') {
- return callback(new Error(body.reason))
+ callback.call(this, err, body, res)
}
+ }
- callback(null, body)
- })
+ return request(options, cb)
}

0 comments on commit 83a6e08

Please sign in to comment.