Skip to content

Commit

Permalink
Updated _get method.
Browse files Browse the repository at this point in the history
* 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
Christian Wesselhoeft committed Jan 27, 2013
1 parent d3dadaa commit 83a6e08
Showing 1 changed file with 19 additions and 42 deletions.
61 changes: 19 additions & 42 deletions index.js
Expand Up @@ -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)
}


Expand Down

0 comments on commit 83a6e08

Please sign in to comment.