Skip to content

Commit

Permalink
Merge pull request #145 from erikzrekz/revertES5Feature
Browse files Browse the repository at this point in the history
Revert commit 42251c0 due to the introduction of ES5 feature - will w…
  • Loading branch information
erikzrekz committed May 14, 2016
2 parents 6dc4f3c + f941bc8 commit a2fa993
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions lib/client/client.js
Expand Up @@ -56,11 +56,7 @@ Client = exports.Client = function (options) {
util.inherits(Client, EventEmitter);

Client.prototype.request = function (method, uri) {
var options = Object.assign({}, this.options),
url,
self = this,
res,
args = Array.prototype.slice.call(arguments),
var options, url, self = this, res, args = Array.prototype.slice.call(arguments),
callback = args.pop(),
body = 'object' === typeof args[args.length - 1] && !Array.isArray(args[args.length - 1]) && args.pop(),
auth = this.options.get('password') ? ':' + this.options.get('password') : '/token:' + this.options.get('token'),
Expand All @@ -71,33 +67,38 @@ Client.prototype.request = function (method, uri) {

url = assembleUrl(self, uri);

options.headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': self.userAgent
};
if (options) { // is this ever used?
self.options.headers = options;
} else {
self.options.headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': self.userAgent
};
}

if (useOAuth) {// token is an oauth token obtained from OAuth2
options.headers.Authorization = 'Bearer ' + token;
self.options.headers.Authorization = 'Bearer ' + token;
} else {// token is an API token obtained from the Zendesk Settings UI
options.headers.Authorization = 'Basic ' + encoded;
self.options.headers.Authorization = 'Basic ' + encoded;
}

options.uri = url;
options.method = method || 'GET';
self.options.uri = url;
self.options.method = method || 'GET';

if (body) {
options.body = JSON.stringify(body);
} else if ('GET' !== method && 'application/json' === options.headers['Content-Type']) {
options.body = '{}';
self.options.body = JSON.stringify(body);
} else if ('GET' !== method && 'application/json' === self.options.headers['Content-Type']) {
self.options.body = '{}';
}

if (proxy) {
options.proxy = proxy;
self.options.proxy = proxy;
}

self.emit('debug::request', options);
return this._request(options, function (err, response, result) {
self.emit('debug::request', self.options);

return this._request(self.options, function (err, response, result) {
requestCallback(self, err, response, result, callback);
});
};
Expand Down Expand Up @@ -170,22 +171,20 @@ Client.prototype.requestUpload = function (uri, file, callback) {
encoded = new Buffer(this.options.get('username') + auth).toString('base64'),
useOAuth = this.options.get('oauth'),
token = this.options.get('token'),
options = Object.assign({}, this.options);
uploadOptions = self.options;

options.uri = assembleUrl(self, uri);
options.method = 'POST';
self.options.uri = assembleUrl(self, uri);
self.options.method = 'POST';

options.headers = {'Content-Type': 'application/binary'};
self.options.headers = {'Content-Type': 'application/binary'};

if (useOAuth) {
options.headers.Authorization = 'Bearer ' + token;
self.options.headers.Authorization = 'Bearer ' + token;
} else {
options.headers.Authorization = 'Basic ' + encoded;
self.options.headers.Authorization = 'Basic ' + encoded;
}


self.emit('debug::request', options);
out = this._request(options, function (err, response, result) {
out = this._request(self.options, function (err, response, result) {
requestCallback(self, err, response, result, callback);
});

Expand Down

0 comments on commit a2fa993

Please sign in to comment.