braintree
Version:
A library for integrating with Braintree.
136 lines (116 loc) • 3.98 kB
JavaScript
//@ sourceMappingURL=http.map
// Generated by CoffeeScript 1.6.1
var Buffer, Http, Util, braintree, exceptions, http, https, xml2js;
http = require('http');
https = require('https');
Buffer = require('buffer').Buffer;
braintree = require('../braintree');
xml2js = require('xml2js');
exceptions = require('./exceptions');
Util = require('./util').Util;
Http = (function() {
Http.prototype.timeout = 60000;
function Http(config) {
this.config = config;
this.parser = new xml2js.Parser({
explicitRoot: true
});
}
Http.prototype.checkHttpStatus = function(status) {
switch (status.toString()) {
case '200':
case '201':
case '422':
return null;
case '401':
return exceptions.AuthenticationError("Authentication Error");
case '403':
return exceptions.AuthorizationError("Authorization Error");
case '404':
return exceptions.NotFoundError("Not Found");
case '426':
return exceptions.UpgradeRequired("Upgrade Required");
case '500':
return exceptions.ServerError("Server Error");
case '503':
return exceptions.DownForMaintenanceError("Down for Maintenance");
default:
return exceptions.UnexpectedError('Unexpected HTTP response: ' + status);
}
};
Http.prototype["delete"] = function(url, callback) {
return this.request('DELETE', url, null, callback);
};
Http.prototype.get = function(url, callback) {
return this.request('GET', url, null, callback);
};
Http.prototype.post = function(url, body, callback) {
return this.request('POST', url, body, callback);
};
Http.prototype.put = function(url, body, callback) {
return this.request('PUT', url, body, callback);
};
Http.prototype.request = function(method, url, body, callback) {
var client, options, requestBody, theRequest,
_this = this;
client = this.config.environment.ssl ? https : http;
options = {
host: this.config.environment.server,
port: this.config.environment.port,
method: method,
path: this.config.baseMerchantPath + url,
headers: {
'Authorization': (new Buffer(this.config.publicKey + ':' + this.config.privateKey)).toString('base64'),
'X-ApiVersion': this.config.apiVersion,
'Accept': 'application/xml',
'Content-Type': 'application/json',
'User-Agent': 'Braintree Node ' + braintree.version
}
};
if (body) {
requestBody = JSON.stringify(Util.convertObjectKeysToUnderscores(body));
options.headers['Content-Length'] = Buffer.byteLength(requestBody).toString();
}
theRequest = client.request(options, function(response) {
body = '';
response.on('data', function(responseBody) {
return body += responseBody;
});
response.on('end', function() {
var error;
error = _this.checkHttpStatus(response.statusCode);
if (error) {
return callback(error, null);
}
if (body !== ' ') {
return _this.parser.parseString(body, function(err, result) {
return callback(null, Util.convertNodeToObject(result));
});
} else {
return callback(null, null);
}
});
return response.on('error', function(err) {
var error;
error = exceptions.UnexpectedError('Unexpected response error: ' + err);
return callback(error, null);
});
});
theRequest.setTimeout(this.timeout, function() {
var error;
error = exceptions.UnexpectedError("Request timed out");
return callback(error, null);
});
theRequest.on('error', function(err) {
var error;
error = exceptions.UnexpectedError('Unexpected request error: ' + err);
return callback(error, null);
});
if (body) {
theRequest.write(requestBody);
}
return theRequest.end();
};
return Http;
})();
exports.Http = Http;