text: // Generated by CoffeeScript 1.9.0
  var depd, deprecate, fs, helpers, request, requestJson, url;
  
  request = require("request");
  
  fs = require("fs");
  
  url = require("url");
  
  depd = require("depd");
  
  deprecate = depd("request-json");
  
  requestJson = module.exports;
  
  requestJson.createClient = function(url, options) {
    if (options == null) {
      options = {};
    }
    return new requestJson.JsonClient(url, options);
  };
  
  requestJson.newClient = function(url, options) {
    if (options == null) {
      options = {};
    }
    deprecate("newClient() is deprecated, please use createClient()");
    return requestJson.createClient(url, options);
  };
  
  helpers = {
    merge: function(obj1, obj2) {
      var key, result;
      result = {};
      for (key in obj1) {
        result[key] = obj1[key];
      }
      if (obj2 != null) {
        for (key in obj2) {
          result[key] = obj2[key];
        }
      }
      return result;
    },
    buildOptions: function(clientOptions, clientHeaders, host, path, requestOptions) {
      var options;
      if (requestOptions !== {}) {
        options = helpers.merge(clientOptions, requestOptions);
      }
      if ((requestOptions != null) && requestOptions !== {} && requestOptions.headers) {
        options.headers = helpers.merge(clientHeaders, requestOptions.headers);
      } else {
        options.headers = clientHeaders;
      }
      options.uri = url.resolve(host, path);
      return options;
    },
    parseBody: function(error, response, body, callback) {
      var err, msg, parsed;
      if (typeof body === "string" && body !== "") {
        try {
          parsed = JSON.parse(body);
        } catch (_error) {
          err = _error;
          msg = "Parsing error : " + err.message + ", body= \n " + body;
          if (error == null) {
            error = new Error(msg);
          }
          parsed = body;
        }
      } else {
        parsed = body;
      }
      return callback(error, response, parsed);
    }
  };
  
  requestJson.JsonClient = (function() {
    function JsonClient(_at_host, _at_options) {
      var _ref;
      this.host = _at_host;
      this.options = _at_options != null ? _at_options : {};
      this.headers = (_ref = this.options.headers) != null ? _ref : {};
      this.headers['accept'] = 'application/json';
      this.headers['user-agent'] = "request-json/1.0";
    }
  
    JsonClient.prototype.setBasicAuth = function(username, password) {
      var basicCredentials, credentials;
      credentials = username + ":" + password;
      basicCredentials = new Buffer(credentials).toString('base64');
      return this.headers["authorization"] = "Basic " + basicCredentials;
    };
  
    JsonClient.prototype.setToken = function(token) {
      return this.headers["x-auth-token"] = token;
    };
  
    JsonClient.prototype.setBearerToken = function(token) {
      return this.headers["authorization"] = "Bearer " + token;
    };
  
    JsonClient.prototype.get = function(path, options, callback, parse) {
      var opts;
      if (parse == null) {
        parse = true;
      }
      if (typeof options === 'function') {
        if (typeof callback === 'boolean') {
          parse = callback;
        }
        callback = options;
        options = {};
      }
      opts = helpers.buildOptions(this.options, this.headers, this.host, path, options);
      opts.method = 'GET';
      return request(opts, function(error, response, body) {
        if (parse) {
          return helpers.parseBody(error, response, body, callback);
        } else {
          return callback(error, response, body);
        }
      });
    };
  
    JsonClient.prototype.post = function(path, json, options, callback, parse) {
      var opts;
      if (parse == null) {
        parse = true;
      }
      if (typeof options === 'function') {
        if (typeof callback === 'boolean') {
          parse = callback;
        }
        callback = options;
        options = {};
      }
      opts = helpers.buildOptions(this.options, this.headers, this.host, path, options);
      opts.method = "POST";
      opts.json = json;
      return request(opts, function(error, response, body) {
        if (parse) {
          return helpers.parseBody(error, response, body, callback);
        } else {
          return callback(error, response, body);
        }
      });
    };
  
    JsonClient.prototype.put = function(path, json, options, callback, parse) {
      var opts;
      if (parse == null) {
        parse = true;
      }
      if (typeof options === 'function') {
        if (typeof callback === 'boolean') {
          parse = callback;
        }
        callback = options;
        options = {};
      }
      opts = helpers.buildOptions(this.options, this.headers, this.host, path, options);
      opts.method = "PUT";
      opts.json = json;
      return request(opts, function(error, response, body) {
        if (parse) {
          return helpers.parseBody(error, response, body, callback);
        } else {
          return callback(error, response, body);
        }
      });
    };
  
    JsonClient.prototype.patch = function(path, json, options, callback, parse) {
      var opts;
      if (parse == null) {
        parse = true;
      }
      if (typeof options === 'function') {
        if (typeof callback === 'boolean') {
          parse = callback;
        }
        callback = options;
        options = {};
      }
      opts = helpers.buildOptions(this.options, this.headers, this.host, path, options);
      opts.method = "PATCH";
      opts.json = json;
      return request(opts, function(error, response, body) {
        if (parse) {
          return helpers.parseBody(error, response, body, callback);
        } else {
          return callback(error, response, body);
        }
      });
    };
  
    JsonClient.prototype.head = function(path, options, callback) {
      var opts, parse;
      if (typeof options === 'function') {
        if (typeof callback === 'boolean') {
          parse = callback;
        }
        callback = options;
        options = {};
      }
      opts = helpers.buildOptions(this.options, this.headers, this.host, path, options);
      opts.method = 'HEAD';
      return request(opts, function(error, response, body) {
        return callback(error, response);
      });
    };
  
    JsonClient.prototype.del = function(path, options, callback, parse) {
      var opts;
      if (parse == null) {
        parse = true;
      }
      if (typeof options === 'function') {
        if (typeof callback === 'boolean') {
          parse = callback;
        }
        callback = options;
        options = {};
      }
      opts = helpers.buildOptions(this.options, this.headers, this.host, path, options);
      opts.method = "DELETE";
      return request(opts, function(error, response, body) {
        if (parse) {
          return helpers.parseBody(error, response, body, callback);
        } else {
          return callback(error, response, body);
        }
      });
    };
  
    JsonClient.prototype.sendFile = function(path, files, data, callback) {
      var att, file, form, index, req, _i, _len, _results;
      if (typeof data === "function") {
        callback = data;
      }
      req = this.post(path, null, callback, false);
      form = req.form();
      if (typeof data !== "function") {
        for (att in data) {
          form.append(att, data[att]);
        }
      }
      if (typeof files === "string") {
        return form.append("file", fs.createReadStream(files));
      } else if (!Array.isArray(files)) {
        return form.append("file", files);
      } else {
        index = 0;
        _results = [];
        for (_i = 0, _len = files.length; _i < _len; _i++) {
          file = files[_i];
          index++;
          if (typeof file === "string") {
            _results.push(form.append("file" + index, fs.createReadStream(file)));
          } else {
            _results.push(form.append("file" + index, file));
          }
        }
        return _results;
      }
    };
  
    JsonClient.prototype.putFile = function(path, file, data, callback) {
      var req;
      if (typeof data === "function") {
        callback = data;
      }
      req = this.put(path, null, callback, false);
      if (typeof file === "string") {
        return fs.createReadStream(file).pipe(req);
      } else if (!Array.isArray(file)) {
        return file.pipe(req);
      }
    };
  
    JsonClient.prototype.saveFile = function(path, filePath, callback) {
      var stream;
      stream = this.get(path, callback, false);
      return stream.pipe(fs.createWriteStream(filePath));
    };
  
    JsonClient.prototype.saveFileAsStream = function(path, callback) {
      return this.get(path, callback, false);
    };
  
    return JsonClient;
  
  })();
  