Created
January 17, 2018 10:28
-
-
Save Adesh/8ffb7a6d85c39a9925ceaf8d59f8cf66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var path = require('path'); | |
const BASEURL = process.env.BASEURL || ''; | |
const ACCESSTOKEN = process.env.ACCESSTOKEN || ''; | |
const HEADERS = { | |
'User-Agent': 'Super Agent/0.0.1', | |
'Authorization': 'Bearer ' + ACCESSTOKEN, | |
'Content-Type': 'application/json', | |
'Accept': 'application/json' | |
}; | |
let API = { | |
GET : (_path, _qs={key1:'v1', key2:'v2'})=>{ | |
return new Promise((resolve, reject)=>{ | |
request({ | |
url : path.join(BASEURL,_path), | |
method : 'GET', | |
headers : headers, | |
qs : _qs | |
}, (error, response, body) => { | |
if (!error && response.statusCode == 200) { | |
resolve({body:body, response:response}); | |
} else { | |
reject(error); | |
} | |
}); | |
}) | |
}, | |
POST : (_path, _json={key1:'v1', key2:'v2'})=>{ | |
return new Promise((resolve, reject)=>{ | |
request({ | |
url : path.join(BASEURL,_path), | |
method : 'POST', | |
headers : headers, | |
json : _json | |
}, (error, response, body) => { | |
if (!error && response.statusCode == 200) { | |
resolve({body:body, response:response}); | |
} else { | |
reject(error); | |
} | |
}); | |
}) | |
}, | |
}; | |
module.exports = API |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment