mg-api-node
Advanced tools
Comparing version 1.0.0 to 1.0.1
15
api.js
@@ -142,3 +142,14 @@ var https = require('https'); | ||
post(method, params, function (err, data) { | ||
if(err && err.message && (err.message.toLowerCase().indexOf("invaliduserexception") > -1 || err.message.toLowerCase().indexOf("dbunavailablexception") > -1) && tryCount < 1){ | ||
var reauthenticate = false; | ||
if (err && err.errors) { | ||
// check if any errors require re-authentication | ||
err.errors.forEach(function (error) { | ||
if (error.name.toLowerCase().indexOf("invaliduserexception") > -1 || error.name.toLowerCase().indexOf("dbunavailablexception") > -1) { | ||
reauthenticate = true; | ||
} | ||
}); | ||
} | ||
if (reauthenticate === true && tryCount < 1) { | ||
tryCount++; | ||
@@ -148,3 +159,3 @@ doAuthenticate(callback); | ||
tryCount = 0; | ||
callback(err, data) | ||
callback(err, data); | ||
} | ||
@@ -151,0 +162,0 @@ }); |
{ | ||
"name": "mg-api-node", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Unofficial nodejs client for the MyGeotab API", | ||
@@ -5,0 +5,0 @@ "main": "api.js", |
@@ -8,3 +8,3 @@ # mg-api-node # | ||
``` | ||
$ git clone https://github.com/Geotab/mg-api-node.git mg-api-node | ||
$ npm install mg-api-node --save | ||
``` | ||
@@ -11,0 +11,0 @@ |
@@ -24,2 +24,13 @@ var expect = require('chai').expect; | ||
var invalidUserError = { | ||
error: { | ||
message: 'Incorrect MyGeotab login credentials @ \'g560\'', | ||
name: 'JSONRPCError', | ||
errors: [{ | ||
message: 'Incorrect MyGeotab login credentials @ \'g560\'', | ||
name: 'InvalidUserException' | ||
}] | ||
} | ||
}; | ||
describe('#authenticate', function() { | ||
@@ -117,14 +128,4 @@ var validateSuccess = function(err, data, server) { | ||
it('authenticates with invalid credentials', function(done) { | ||
var error = { | ||
error: { | ||
message: 'Incorrect MyGeotab login credentials @ \'g560\'', | ||
name: 'JSONRPCError', | ||
errors: [{ | ||
message: 'Incorrect MyGeotab login credentials @ \'g560\'', | ||
name: 'InvalidUserException' | ||
}] | ||
} | ||
}; | ||
nock('https://my.geotab.com').post('/apiv1').reply(200, error); | ||
nock('https://my.geotab.com').post('/apiv1').reply(200, invalidUserError); | ||
@@ -135,3 +136,3 @@ api = new API('foo@bar.com', password, database); | ||
expect(err).to.be.a('object'); | ||
expect(err).to.deep.equal(error.error); | ||
expect(err).to.deep.equal(invalidUserError.error); | ||
done(); | ||
@@ -211,3 +212,3 @@ }); | ||
nock('https://my3.geotab.com').post('/apiv1').reply(200, error); | ||
api.call('Get', { | ||
@@ -246,2 +247,27 @@ typeName: 'FooBar' | ||
}); | ||
it('re-authenticates and retries on invaliduserexception', function(done) { | ||
nock('https://my3.geotab.com').post('/apiv1').reply(200, credentialsResult); | ||
api = new API(userName, password, database, server, { | ||
compression: null | ||
}); | ||
api.authenticate(function(err, data) { | ||
nock('https://my3.geotab.com').post('/apiv1').reply(200, invalidUserError) | ||
.post('/apiv1').reply(200, credentialsResult) | ||
.post('/apiv1').reply(200, user); | ||
api.call('Get', { | ||
typeName: 'User', | ||
search: { | ||
name: userName | ||
} | ||
}, function(err, data) { | ||
expect(err).to.be.a('null'); | ||
expect(data).to.deep.equal(user.result); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -248,0 +274,0 @@ |
28089
444