var request = require('request');

/**
 * Shipwire API client.
 * @param authorization: String authorization
 * @param secret: String api secret
 * @param sandbox: Boolean true uses sandbox, false/null uses production 
 */
var Shipwire = function(authorization, secret, sandbox) {
    this.configure({ authorization: authorization, secret: secret, hostname: sandbox ? 'api.beta.shipwire.com' ? 'api.shipwire.com' });
}

exports.Shipwire = Shipwire;


(function() {

    this.defaults = {
        hostname    	: 'api.beta.shipwire.com', //api.beta.shipwire.com
        path			: '/api/v3/',
        userAgent   	: 'node-shipwire (https://github.com/scott-wyatt/shipwire-node-api)',
        authorization   : null,
        secret      	: null
    };

    this.configure = function(options) {
        var options = options || {};
        this.options = {};

        for (var key in this.defaults) {
            this.options[key] = options[key] !== undefined ? options[key] : this.defaults[key];
        }

        return this;
    };


    /**
     * ORDERS API
     * 
     * https://www.shipwire.com/w/developers/orders/
     */
    this.orders = function(url, cols, callback) {

    };

    /**
     * STOCK API
     * 
     * https://www.shipwire.com/w/developers/stock/
     */
    this.stock = function(url, cols, callback) {

    };

    /**
     * RATE API
     * 
     * https://www.shipwire.com/w/developers/rate/
     */
    this.rate = function(url, cols, callback) {

    };

    /**
     * RECEIVINGS API
     * 
     * https://www.shipwire.com/w/developers/receivings/
     */
    this.receivings = function(url, cols, callback) {

    };

    /**
     * RETURNS API
     * 
     * https://www.shipwire.com/w/developers/returns/
     */
    this.returns = function(url, cols, callback) {

    };

    /**
     * PRODUCTS API
     * 
     * https://www.shipwire.com/w/developers/products/
     */
    this.products = function(url, cols, callback) {

    };

    /**
     * GET Request
     */
    this.get = function(apiPath, params, callback) {
        return this.send(apiPath, params, 'GET', callback);
    };
    
    /**
     * POST Request
     */
    this.post = function(apiPath, params, callback) {
        return this.send(apiPath, params, 'POST', callback);
    };

    /**
     * PUT Request
     */
    this.put = function(apiPath, params, callback) {
        return this.send(apiPath, params, 'PUT', callback);
    };


    /**
     * Send request to the MOZ API
     */
    this.send = function(apiPath, params, method, callback) {

        var path = 'http://' + this.options.hostname + '/' + this.options.path + '/' + apiPath;
        var auth = new Buffer(this.options.authorization + ':' + this.options.secret).toString('base64');

        var options = {
            url: path,
            headers: {
                'User-Agent': this.defaults.userAgent,
                'Authorization': 'Basic ' + auth,
                'Content-Length': 0
            }
        };

        var args = [];
        for (var key in params) {
            args.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]));
        }
        var queryString = args.join('&');
        if (queryString) {
            switch (method) {
                case 'GET':
                    options.url += '?' + queryString;
                    break;
                case 'POST':
                    options.headers['Content-Length'] = queryString.length;
                    break;
                case 'PUT':
                    options.headers['Content-Length'] = queryString.length;
                    break;    
            }
        }

        function cb(error, response, body) {
            
            if (!error && response.statusCode > 200) {
                callback({ status: response.statusCode, msg: body });   
            }
            else {
                callback(null, JSON.parse(body));
            }
        }

        console.log("Calling: "+options.url);
        request(options, cb);
    };


}).call(Shipwire.prototype);
@@ -575,16 +575,14 @@
 %09: '
-/
 api/v3
-/
 ',%0A 
@@ -2573,16 +2573,17 @@
  = 'http
+s
 ://' + t

Update URL paths