text: // Generated by CoffeeScript 1.6.3
  (function() {
    var fs, parseBody, request, url;
  
    request = require("request");
  
    fs = require("fs");
  
    url = require("url");
  
    parseBody = function(error, response, body, callback) {
      var err, parsed;
      if (typeof body === "string" && body !== "") {
        try {
          parsed = JSON.parse(body);
        } catch (_error) {
          err = _error;
          if (error == null) {
            error = new Error("Parsing error : " + err.message + ", body= \n " + body);
          }
          parsed = body;
        }
      } else {
        parsed = body;
      }
      return callback(error, response, parsed);
    };
  
    exports.newClient = function(url) {
      return new exports.JsonClient(url);
    };
  
    exports.JsonClient = (function() {
      function JsonClient(host) {
        this.host = host;
        this.headers = {
          accept: 'application/json',
          "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.get = function(path, callback, parse) {
        if (parse == null) {
          parse = true;
        }
        return request({
          method: 'GET',
          uri: url.resolve(this.host, path),
          headers: this.headers
        }, function(error, response, body) {
          if (parse) {
            return parseBody(error, response, body, callback);
          } else {
            return callback(error, response, body);
          }
        });
      };
  
      JsonClient.prototype.post = function(path, json, callback, parse) {
        if (parse == null) {
          parse = true;
        }
        return request({
          method: "POST",
          uri: url.resolve(this.host, path),
          json: json,
          headers: this.headers
        }, function(error, response, body) {
          if (parse) {
            return parseBody(error, response, body, callback);
          } else {
            return callback(error, response, body);
          }
        });
      };
  
      JsonClient.prototype.put = function(path, json, callback, parse) {
        if (parse == null) {
          parse = true;
        }
        return request({
          method: "PUT",
          uri: url.resolve(this.host, path),
          json: json,
          headers: this.headers
        }, function(error, response, body) {
          if (parse) {
            return parseBody(error, response, body, callback);
          } else {
            return callback(error, response, body);
          }
        });
      };
  
      JsonClient.prototype.del = function(path, callback, parse) {
        if (parse == null) {
          parse = true;
        }
        return request({
          method: "DELETE",
          uri: url.resolve(this.host, path),
          headers: this.headers
        }, function(error, response, body) {
          if (parse) {
            return 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.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;
  
    })();
  
  }).call(this);
  