11'use strict'
22
3- const request = require ( 'request-promise' )
3+ const axios = require ( 'axios' ) . default
44const nodeify = require ( './utils/nodeify' )
55const {
66 isNonNullObject
@@ -33,19 +33,18 @@ function getValidHeaders (headers) {
3333
3434function normalizeError ( err ) {
3535 throw {
36- statusCode : err . statusCode ,
37- error : err . error . error
36+ statusCode : err . response . status ,
37+ error : err . response . data . error
3838 }
3939}
4040
4141class API {
4242 constructor ( options ) {
43- this . rq = request . defaults ( {
44- baseUrl : options . hostUrl ,
45- json : true ,
43+ this . rq = axios . create ( {
44+ baseURL : options . hostUrl ,
4645 auth : {
47- user : options . key_id ,
48- pass : options . key_secret
46+ username : options . key_id ,
47+ password : options . key_secret
4948 } ,
5049 headers : Object . assign (
5150 { 'User-Agent' : options . ua } ,
@@ -61,48 +60,39 @@ class API {
6160 }
6261
6362 get ( params , cb ) {
64- return nodeify ( this . rq . get ( {
65- url : this . getEntityUrl ( params ) ,
66- qs : params . data ,
63+ return nodeify ( this . rq . get ( this . getEntityUrl ( params ) , {
64+ params : params . data
6765 } ) . catch ( normalizeError ) , cb )
6866 }
6967
7068 post ( params , cb ) {
71- let request = {
72- url : this . getEntityUrl ( params ) ,
73- body : params . data
74- } ;
75- return nodeify ( this . rq . post ( request ) . catch ( normalizeError ) , cb ) ;
69+ return nodeify ( this . rq . post ( this . getEntityUrl ( params ) , params . data )
70+ . catch ( normalizeError ) , cb ) ;
7671 }
7772
7873 // postFormData method for file uploads.
7974 postFormData ( params , cb ) {
80- let request = {
81- url : this . getEntityUrl ( params ) ,
82- formData : params . formData
83- } ;
84- return nodeify ( this . rq . post ( request ) . catch ( normalizeError ) , cb ) ;
75+ return nodeify ( this . rq . post ( this . getEntityUrl ( params ) , params . formData , {
76+ 'headers' : {
77+ 'Content-Type' : 'multipart/form-data'
78+ }
79+ } )
80+ . catch ( normalizeError ) , cb ) ;
8581 }
8682
8783 put ( params , cb ) {
88- return nodeify ( this . rq . put ( {
89- url : this . getEntityUrl ( params ) ,
90- body : params . data
91- } ) . catch ( normalizeError ) , cb )
84+ return nodeify ( this . rq . put ( this . getEntityUrl ( params ) , params . data )
85+ . catch ( normalizeError ) , cb ) ;
9286 }
9387
9488 patch ( params , cb ) {
95- let request = {
96- url : this . getEntityUrl ( params ) ,
97- body : params . data
98- } ;
99- return nodeify ( this . rq . patch ( request ) . catch ( normalizeError ) , cb ) ;
89+ return nodeify ( this . rq . patch ( this . getEntityUrl ( params ) , params . data )
90+ . catch ( normalizeError ) , cb ) ;
10091 }
10192
10293 delete ( params , cb ) {
103- return nodeify ( this . rq . delete ( {
104- url : this . getEntityUrl ( params )
105- } ) . catch ( normalizeError ) , cb )
94+ return nodeify ( this . rq . delete ( this . getEntityUrl ( params ) )
95+ . catch ( normalizeError ) , cb )
10696 }
10797}
10898
0 commit comments