I'm calling an API to request token, and making sure i'm not leaving any corners for errors and handle the result appropriately.
It's a web service POST call in general, The code gets credentials, formats them in the URL and parses the result.
The objective here is I'm looking if code overall is good when it comes to REST call, if any corner I'm missing for an error somewhere.
function Authentication() {
}
Authentication.prototype.requestToken = function(credentials) {
var self = this;
return new Promise(function(resolve, reject){
https.request({
method: 'POST',
hostname: 'hostname.com',
path: '/path-to-api' +
'&client_id=' + credentials.clientId +
'&client_secret=' + credentials.clientSecret +
'&device_id=' + credentials.deviceId +
'&device_token=' + credentials.deviceToken,
headers: {
'accept': 'application/json;charset=UTF-8',
'content-type': 'application/x-www-form-urlencoded'
}
}, function(response) {
var buffers = [];
response.on('data', buffers.push.bind(buffers));
response.on('end', function(){
try {
self.token = JSON.parse(Buffer.concat(buffers).toString());
response.statusCode == 200 ? resolve(self.token) : reject(self.token);
} catch (error) {
reject(error);
}
});
}).on('error', reject).end();
});
};
Authentication.prototype.token;