<commit_before>/*
 * client.js: Base client from which all Rackspace clients inherit from
 *
 * (C) 2011 Nodejitsu Inc.
 *
 */

var utile = require('utile'),
    request = require('request'),
    ps = require('pause-stream'),
    auth = require('../common/auth'),
    base = require('../core/base'),
    pkgcloud = require('../../pkgcloud');

var Client = exports.Client = function (options) {
  base.Client.call(this, options);

  this.authUrl    = options.authUrl || 'auth.api.rackspacecloud.com';
  this.serversUrl = options.serversUrl || 'servers.api.rackspacecloud.com';
  this.protocol   = options.protocol || 'https://';
  this.provider   = 'rackspace';

  if (!this.before) {
    this.before = [];
  }

  this.before.push(auth.authToken);
  this.before.push(function (req) {
    req.json = true;
    if (typeof req.body !== 'undefined') {
      req.headers['Content-Type'] = 'application/json';
      req.body = JSON.stringify(req.body);
    }
  });
};

utile.inherits(Client, base.Client);

Client.prototype.failCodes = {
  400: 'Bad Request',
  401: 'Unauthorized',
  403: 'Resize not allowed',
  404: 'Item not found',
  409: 'Build in progress',
  413: 'Over Limit',
  415: 'Bad Media Type',
  500: 'Fault',
  503: 'Service Unavailable'
};

Client.prototype.successCodes = {
  200: 'OK',
  201: 'Created',
  202: 'Accepted',
  203: 'Non-authoritative information',
  204: 'No content'
};

Client.prototype.auth = function (callback) {
  var self = this,
      authOptions;

  authOptions = {
    uri: this.protocol + this.authUrl + '/v1.0',
    headers: {
      'HOST': this.authUrl,
      'X-AUTH-USER': this.config.username,
      'X-AUTH-KEY': this.config.apiKey,
      'User-Agent': utile.format('nodejs-pkgcloud/%s', pkgcloud.version)
    }
  };

  request(authOptions, self.defaultRequestHandler(function(err, body, res) {
    if (err) {
      return callback(err);
    }

    self.authorized = true;
    self.config.serverUrl = res.headers['x-server-management-url'];
    self.config.storageUrl = res.headers['x-storage-url'];
    self.config.cdnUrl = res.headers['x-cdn-management-url'];
    self.config.authToken = res.headers['x-auth-token'];

    callback(null, res);
  }));
};

Client.prototype.getServiceUrl = function (service) {
  var serviceName = service + 'Url';
  return this.config[serviceName];
};

Client.prototype._doRequest = function (options, callback) {

  var self = this;

  //
  // If this instance is not yet authorized, then return
  // a `BufferedStream` which can be piped to in the current
  // tick.
  //

  if (!self.authorized) {

    var buf = ps().pause();

    self.auth(function (err) {
      if (err) {
        return callback(err);
      }

      var apiStream = Client.super_.prototype._doRequest.call(self, options, callback);

      if (options.upload) {
        buf.pipe(apiStream);
      }
      else if (options.download) {
        apiStream.pipe(buf);
      }

      buf.resume();
    });

    return buf;
  }
  else {<commit_msg>[bug] Fixing buffered streams to proxy properly<commit_after>/*
 * client.js: Base client from which all Rackspace clients inherit from
 *
 * (C) 2011 Nodejitsu Inc.
 *
 */

var utile = require('utile'),
    request = require('request'),
    ps = require('pause-stream'),
    auth = require('../common/auth'),
    base = require('../core/base'),
    pkgcloud = require('../../pkgcloud');

var Client = exports.Client = function (options) {
  base.Client.call(this, options);

  this.authUrl    = options.authUrl || 'auth.api.rackspacecloud.com';
  this.serversUrl = options.serversUrl || 'servers.api.rackspacecloud.com';
  this.protocol   = options.protocol || 'https://';
  this.provider   = 'rackspace';

  if (!this.before) {
    this.before = [];
  }

  this.before.push(auth.authToken);
  this.before.push(function (req) {
    req.json = true;
    if (typeof req.body !== 'undefined') {
      req.headers['Content-Type'] = 'application/json';
      req.body = JSON.stringify(req.body);
    }
  });
};

utile.inherits(Client, base.Client);

Client.prototype.failCodes = {
  400: 'Bad Request',
  401: 'Unauthorized',
  403: 'Resize not allowed',
  404: 'Item not found',
  409: 'Build in progress',
  413: 'Over Limit',
  415: 'Bad Media Type',
  500: 'Fault',
  503: 'Service Unavailable'
};

Client.prototype.successCodes = {
  200: 'OK',
  201: 'Created',
  202: 'Accepted',
  203: 'Non-authoritative information',
  204: 'No content'
};

Client.prototype.auth = function (callback) {
  var self = this,
      authOptions;

  authOptions = {
    uri: this.protocol + this.authUrl + '/v1.0',
    headers: {
      'HOST': this.authUrl,
      'X-AUTH-USER': this.config.username,
      'X-AUTH-KEY': this.config.apiKey,
      'User-Agent': utile.format('nodejs-pkgcloud/%s', pkgcloud.version)
    }
  };

  request(authOptions, self.defaultRequestHandler(function(err, body, res) {
    if (err) {
      return callback(err);
    }

    self.authorized = true;
    self.config.serverUrl = res.headers['x-server-management-url'];
    self.config.storageUrl = res.headers['x-storage-url'];
    self.config.cdnUrl = res.headers['x-cdn-management-url'];
    self.config.authToken = res.headers['x-auth-token'];

    callback(null, res);
  }));
};

Client.prototype.getServiceUrl = function (service) {
  var serviceName = service + 'Url';
  return this.config[serviceName];
};

Client.prototype._doRequest = function (options, callback) {

  var self = this;

  //
  // If this instance is not yet authorized, then return
  // a `BufferedStream` which can be piped to in the current
  // tick.
  //

  if (!self.authorized) {

    var buf = ps().pause();

    self.auth(function (err) {

      if (err) {
        return callback(err);
      }

      var apiStream = Client.super_.prototype._doRequest.call(self, options, callback);

      if (options.upload) {
        buf.pipe(apiStream);
      }
      else if (options.download) {
        apiStream.pipe(buf);
      }

      buf.resume();
    });

    return buf;
  }
  else {
{'fraction_non_alphanumeric': 0.1620993190499917, 'fraction_numerical': 0.015944195316392625, 'mean_word_length': 2.9410994764397906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7226843', 'n_tokens_mistral': 2060, 'n_tokens_neox': 1958, 'n_words': 430}