"use strict";

var Parser = require('./Parser'),
    Methods = require('./Methods'),
    UrlNormalizer = require('./UrlNormalizer'),
    inherits = require('inherits'),
    HttpClient = require('./HttpClient'),
    Promise = require('bluebird'),
    _ = require('lodash');

var BaseHttpClient = function (http) {
    this._http = http;
};

inherits(BaseHttpClient, HttpClient);

BaseHttpClient.prototype._getNetworkPromise = function (url, method, data, headers) {
    switch (method) {
        case Methods.GET:
            return this._http.get(url, {headers: headers});
        case Methods.POST:
            return this._http.post(url, data, {headers: headers});
        case Methods.PUT:
            return this._http.put(url, data, {headers: headers});
        case Methods.DELETE:
            return this._http.delete(url, {headers: headers});
        case Methods.JSONP:
            return this._http.jsonp(url, {headers: headers});
    }
};

BaseHttpClient.prototype._handleRequest = function (url, method, data, parser, headers) {
    return new Promise(_.bind(function (resolve, reject) {
        parser = parser || new Parser();
        var unwrapResponse = this.unwrapResponse();
        this._getNetworkPromise(url, method, data, headers)
            .success(function (response, status, headers, config) {
                if (!unwrapResponse)
                    resolve(parser.parse(response));
                else
                    resolve([parser.parse(response), status, headers, config]);
            })
            .error(function (error, status, headers, config) {
                if (!unwrapResponse)
                    reject(error);
                else
                    reject({data: error, status: status, headers: headers, config: config});
            });
    }, this));
};

BaseHttpClient.prototype.unwrapResponse = function () {
    return false;
};

BaseHttpClient.prototype.get = function (url, parser, headers) {
    return this._handleRequest(url, Methods.GET, null, parser, headers);
};

BaseHttpClient.prototype.post = function (url, data, parser, headers) {
    return this._handleRequest(url, Methods.POST, data, parser, headers);
};

BaseHttpClient.prototype.put = function (url, data, parser, headers) {
    return this._handleRequest(url, Methods.PUT, data, parser, headers);
};

BaseHttpClient.prototype.delete = function (url, parser, headers) {
    return this._handleRequest(url, Methods.DELETE, null, parser, headers);
};

BaseHttpClient.prototype.jsonp = function (url, parser, headers) {
    url = UrlNormalizer.normalizeJsonp(url);
    return this._handleRequest(url, Methods.JSONP, null, parser, headers);
};

module.exports = BaseHttpClient;

@@ -454,24 +454,162 @@
  headers) %7B%0A
+    var credentialsEnabled = this.credentialsRequestsEnabled(),%0A        config = %7Bheaders: headers, withCredentials: credentialsEnabled%7D;%0A
     switch (
@@ -683,34 +683,22 @@
 et(url, 
-%7Bheaders: headers%7D
+config
 );%0A     
@@ -765,34 +765,22 @@
 , data, 
-%7Bheaders: headers%7D
+config
 );%0A     
@@ -845,34 +845,22 @@
 , data, 
-%7Bheaders: headers%7D
+config
 );%0A     
@@ -925,34 +925,22 @@
 te(url, 
-%7Bheaders: headers%7D
+config
 );%0A     
@@ -1007,26 +1007,14 @@
 rl, 
-%7Bheaders: headers%7D
+config
 );%0A 
@@ -1956,24 +1956,114 @@
  false;%0A%7D;%0A%0A
+BaseHttpClient.prototype.credentialsRequestsEnabled = function () %7B%0A    return false;%0A%7D;%0A%0A
 BaseHttpClie

Add possibility to do requests with credentials