<commit_before>'use strict';

var https = require('https');
var pjson = require('../package.json');
var Xml2js = require('xml2js');
var parser = new Xml2js.Parser({explicitArray: false});

exports.create = function (config) {
  config.RECURLY_HOST = config.SUBDOMAIN + '.recurly.com';
  return {

    request: function (route, callback, data) {
      var endpoint = route[0];
      var method = route[1];
      var headers = route[2];
      var that = this;
      var options = {
        host: config.RECURLY_HOST,
        port: 443,
        path: endpoint,
        method: method,
        headers: {
          Authorization: 'Basic ' + (new Buffer(config.API_KEY)).toString('base64'),
          Accept: 'application/xml',
          'Content-Length': (data) ? Buffer.byteLength(data, 'utf8') : 0,
          'User-Agent': 'recurly-js/' + pjson.version
        }
      };

      if (headers) {
        for (var p in headers) {
          if (!headers.hasOwnProperty(p)) continue;

          options.headers[p] = headers[p];
        }
      }

      if (method.toLowerCase() === 'post' || method.toLowerCase() === 'put') {
        options.headers['Content-Type'] = 'application/xml';
        that.debug(data);
      }
      that.debug(options);
      var req = https.request(options, function (res) {

        var responsedata = [];
        res.on('data', function (d) {
          responsedata.push(d);
        });
        res.on('end', function () {
          responsedata = Buffer.concat(responsedata);
          that.debug('Response is: ' + res.statusCode);
          that.debug(responsedata);
          try {
            // 200–299 success
            if (res.statusCode >= 200 && res.statusCode <= 299) {
              if (responsedata === '') {
                return _cb(res);
              }
              else if (options.headers.Accept !== 'application/xml') {
                return _cb(res, null, responsedata);
              }
              return parser.parseString(responsedata, function (err, result) {
                return _cb(res, null, result);
              });
            }
            // 400–499 client request errors
            // 500–599 server errors
            if ([404, 412, 422, 500].indexOf(res.statusCode) !== -1) {
              return parser.parseString(responsedata, function (err, result) {
                return _cb(res, result);
              });
            }
            if (res.statusCode >= 400) {
              return _cb(res, responsedata);
            }
          }
          catch (e) {
            return _cb(null, e);
          }
        });
      });
      if (data) {
        req.write(data);
      }
      req.end();
      req.on('error', function (e) {
        return _cb(null, e);
      });
      // fallback for backward compatibility
      function _cb(res, err, data) {
        // callback objects acquired from parent scope
        if (typeof callback === 'undefined') {
          throw new Error('Missing argument: callback function');
        }
        if (typeof callback !== 'function') {
          throw new Error('Callback should be a function');
        }<commit_msg>For all response status over 400, parse the response body as XML.<commit_after>'use strict';

var https = require('https');
var pjson = require('../package.json');
var Xml2js = require('xml2js');
var parser = new Xml2js.Parser({explicitArray: false});

exports.create = function (config) {
  config.RECURLY_HOST = config.SUBDOMAIN + '.recurly.com';
  return {

    request: function (route, callback, data) {
      var endpoint = route[0];
      var method = route[1];
      var headers = route[2];
      var that = this;
      var options = {
        host: config.RECURLY_HOST,
        port: 443,
        path: endpoint,
        method: method,
        headers: {
          Authorization: 'Basic ' + (new Buffer(config.API_KEY)).toString('base64'),
          Accept: 'application/xml',
          'Content-Length': (data) ? Buffer.byteLength(data, 'utf8') : 0,
          'User-Agent': 'recurly-js/' + pjson.version
        }
      };

      if (headers) {
        for (var p in headers) {
          if (!headers.hasOwnProperty(p)) continue;

          options.headers[p] = headers[p];
        }
      }

      if (method.toLowerCase() === 'post' || method.toLowerCase() === 'put') {
        options.headers['Content-Type'] = 'application/xml';
        that.debug(data);
      }
      that.debug(options);
      var req = https.request(options, function (res) {

        var responsedata = [];
        res.on('data', function (d) {
          responsedata.push(d);
        });
        res.on('end', function () {
          responsedata = Buffer.concat(responsedata);
          that.debug('Response is: ' + res.statusCode);
          that.debug(responsedata);
          try {
            // 200–299 success
            if (res.statusCode >= 200 && res.statusCode <= 299) {
              if (responsedata === '') {
                return _cb(res);
              }
              else if (options.headers.Accept !== 'application/xml') {
                return _cb(res, null, responsedata);
              }
              return parser.parseString(responsedata, function (err, result) {
                return _cb(res, null, result);
              });
            }
            // 400–499 client request errors
            // 500–599 server errors
            if ([404, 412, 422, 500].indexOf(res.statusCode) !== -1) {
              return parser.parseString(responsedata, function (err, result) {
                return _cb(res, result);
              });
            }
            if (res.statusCode >= 400) {
              return parser.parseString(responsedata, function (err, result) {
                return _cb(res, result);
              });
            }
          }
          catch (e) {
            return _cb(null, e);
          }
        });
      });
      if (data) {
        req.write(data);
      }
      req.end();
      req.on('error', function (e) {
        return _cb(null, e);
      });
      // fallback for backward compatibility
      function _cb(res, err, data) {
        // callback objects acquired from parent scope
        if (typeof callback === 'undefined') {
          throw new Error('Missing argument: callback function');
        }
        if (typeof callback !== 'function') {
          throw new Error('Callback should be a function');
        }
{'fraction_non_alphanumeric': 0.13766193226158888, 'fraction_numerical': 0.017012642422350555, 'mean_word_length': 1.7443254817987153, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1299132', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1769, 'n_words': 438}