Skip to content

Commit

Permalink
use axios instead of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
arghideutis committed Apr 22, 2020
1 parent 44c842e commit 5b0ae5a
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions lib/client/http.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var Client = module.exports

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

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

Expand All @@ -26,14 +27,16 @@ Client.create = function (container) {
}

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

addDefaultHeaders(defaultsOptions)

var client = request.defaults(defaultsOptions)
//var client = request.defaults(defaultsOptions)
console.log('using axios')
var client = axios.defaults(defaultsOptions)

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

Expand All @@ -45,37 +48,29 @@ Client.create = function (container) {
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)
}
//client(options, function (err, res, body) {
axios(options).then((res) {

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

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

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

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

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

d("Request error %s %s", error.code, error.message)
Expand All @@ -93,6 +88,9 @@ Client.create = function (container) {

emit("request.complete", json)
resolve(json)
}).catch(function (error) {
console.error(error)
return reject(error)
})

})
Expand Down Expand Up @@ -139,15 +137,15 @@ Client.create = function (container) {
return instance.request({
method: "PUT",
url: url,
body: data
data: data
})
}

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

Expand Down

0 comments on commit 5b0ae5a

Please sign in to comment.