var Client = module.exports

var Promise = require("bluebird")
var request = require("request")
var routes = require("../routes")

var d = require("debug")("raptorjs:client:http")

Client.create = function (container) {

    var emit = function() {
        container.emit.apply(container, arguments)
    }

    var opts = container.config

    var addDefaultHeaders = function(opts, token) {
        if(!opts.headers) {
            opts.headers = {
                "Content-Type": "application/json"
            }
        }
        if(token) {
            opts.headers.Authorization = `Bearer ${token}`
        }
    }

    var defaultsOptions = {
        baseUrl: opts.url,
        json: true,
        debug: opts.debug
    }

    addDefaultHeaders(defaultsOptions)

    var client = request.defaults(defaultsOptions)

    d("Created new client %s", defaultsOptions.baseUrl)

    var instance = {}

    instance.baseRequest = (options) => {
        return new Promise(function (resolve, reject) {

            options.headers = Object.assign({}, defaultsOptions.headers || {}, options.headers || {})

            d("Performing request %j", options)
            client(options, function (err, res, body) {

                if(err) {
                    d("Request failed %s", err.message)

                    emit("request.error", err)
                    emit("request.complete", false)

                    return reject(err)
                }

                var json
                try {
                    json = JSON.parse(body)
                } catch(e) {
                    json = body
                }

                if(res.statusCode >= 400 || res.statusCode < 200) {

                    if (res.statusCode === 0) {
                        res.statusMessage = "Cannot perform request"
                    }

                    let error = {
                        code: json ? json.code : res.statusCode,
                        message: json ? json.message : res.statusMessage,
                    }

                    if (body) {
                        d("Error body: \n %j", body)
                    }

                    d("Request error %s %s", error.code, error.message)

                    emit("request.error", error)
                    emit("request.complete", false)

                    const err = new Error(error.message)
                    err.code = error.code

                    return reject(err)
                }

                d("Request response: %j", json)

                emit("request.complete", json)
                resolve(json)
            })

        })
    }

    instance.request = function (options) {

        // do not try to login if requesting login / get user
        var userReq =  (
            options.url === routes.LOGIN ||
                options.url === routes.USER_GET_ME ||
                    options.url === routes.REFRESH_TOKEN
        ) ?
            Promise.resolve() : container.Auth().loadUser()

        emit("request.start", options)

        return userReq.then(function () {

            if(container.Auth().getToken()) {
                addDefaultHeaders(options, container.Auth().getToken())
            }

            return instance.baseRequest(options)
        })

    }

    instance.get = function (url) {
        return instance.request({
            method: "GET",
            url: url
        })
    }

    instance.delete = function (url) {
        return instance.request({
            method: "DELETE",
            url: url
        })
    }

    instance.put = function (url, data) {
        return instance.request({
            method: "PUT",
            url: url,
            body: data
        })
    }

    instance.post = function (url, data) {
        return instance.request({
            method: "POST",
            url: url,
            body: data
        })
    }

    instance.client = client

    return instance
}

@@ -56,16 +56,18 @@
 ebird%22)%0A
+//
 var requ
@@ -124,16 +124,45 @@
 routes%22)
+%0Avar axios = require(%22axios%22)
 %0A%0Avar d 
@@ -697,17 +697,16 @@
 opts.url
-,
 %0A       
@@ -702,24 +702,26 @@
 url%0A        
+//
 json: true,%0A
@@ -728,16 +728,18 @@
         
+//
 debug: o
@@ -795,24 +795,26 @@
 tions)%0A%0A    
+//
 var client =
@@ -843,24 +843,104 @@
 ultsOptions)
+%0A    console.log('using axios')%0A    var client = axios.defaults(defaultsOptions)
 %0A%0A    d(%22Cre
@@ -1271,16 +1271,18 @@
         
+//
 client(o
@@ -1321,248 +1321,47 @@
 ) %7B%0A
-%0A                if(err) %7B%0A                    d(%22Request failed %25s%22, err.message)%0A%0A                    emit(%22request.error%22, err)%0A                    emit(%22request.complete%22, false)%0A%0A                    return reject(err)%0A                %7D
+            axios(options).then((res) %7B
 %0A%0A  
@@ -1443,20 +1443,24 @@
 N.parse(
-body
+res.data
 )%0A      
@@ -1509,20 +1509,24 @@
  json = 
-body
+res.data
 %0A       
@@ -1566,20 +1566,16 @@
 s.status
-Code
  %3E= 400 
@@ -1587,20 +1587,16 @@
 s.status
-Code
  %3C 200) 
@@ -1632,20 +1632,16 @@
 s.status
-Code
  === 0) 
@@ -1831,12 +1831,8 @@
 atus
-Code
 ,%0A  
@@ -1894,23 +1894,20 @@
 s.status
-Message
+Text
 ,%0A      
@@ -1947,20 +1947,24 @@
     if (
-body
+res.data
 ) %7B%0A    
@@ -2006,20 +2006,24 @@
 %5Cn %25j%22, 
-body
+res.data
 )%0A      
@@ -2494,24 +2494,138 @@
 solve(json)%0A
+            %7D).catch(function (error) %7B%0A                console.error(error)%0A                return reject(error)%0A
             
@@ -3697,36 +3697,36 @@
 rl,%0A            
-body
+data
 : data%0A        %7D
@@ -3873,20 +3873,20 @@
         
-body
+data
 : data%0A 

use axios instead of requests