mg-api-node
Advanced tools
Comparing version 1.2.0 to 1.3.0
16
api.js
@@ -37,3 +37,3 @@ var https = require('https'); | ||
var post = function (method, params, callback) { | ||
var post = function (method, params, callback, timeout, timeoutCallback) { | ||
var paramsStr = getRpc(method, params); | ||
@@ -88,2 +88,8 @@ var option = getOptions(method, paramsStr); | ||
}); | ||
// If timeout was provided and requesting with http, we add a timeout to the request | ||
if (typeof timeout === 'number' && !options.ssl){ | ||
typeof timoutCallback === 'function' ? post_req.setTimeout(timeout, timeoutCallback) : post_req.setTimeout(timeout); | ||
} | ||
post_req.on('error', function (err) { | ||
@@ -129,3 +135,3 @@ callback(err, null) | ||
var call = function (method, params, callback) { | ||
var call = function (method, params, callback, timeout = null, timeoutCallback = null) { | ||
var doAuthenticate = function (callback) { | ||
@@ -176,6 +182,6 @@ authenticate(function (err, data) { | ||
} | ||
}); | ||
}, timeout, timeoutCallback); | ||
}; | ||
var multicall = function (calls, callback) { | ||
var multicall = function (calls, callback, timeout = null, timeoutCallback = null) { | ||
var formattedCalls; | ||
@@ -203,3 +209,3 @@ | ||
calls: formattedCalls | ||
}, callback); | ||
}, callback, timeout, timeoutCallback); | ||
}; | ||
@@ -206,0 +212,0 @@ |
{ | ||
"name": "mg-api-node", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Unofficial nodejs client for the MyGeotab API", | ||
@@ -8,4 +8,4 @@ "main": "api.js", | ||
"chai": "^1.9.2", | ||
"mocha": "^1.21.4", | ||
"nock": "^2.12.0" | ||
"mocha": "^7.1.2", | ||
"nock": "^12.0.3" | ||
}, | ||
@@ -12,0 +12,0 @@ "scripts": { |
@@ -0,1 +1,3 @@ | ||
# This project has been deprecated. Please use [mg-api-js](https://github.com/Geotab/mg-api-js). | ||
# mg-api-node # | ||
@@ -7,3 +9,3 @@ | ||
``` | ||
```javascript | ||
$ npm install mg-api-node --save | ||
@@ -13,3 +15,3 @@ ``` | ||
### Usage ### | ||
``` | ||
```javascript | ||
var api = new API(userName, password, database); | ||
@@ -43,2 +45,37 @@ | ||
#### HTTP Timeout #### | ||
If you need to handle potential slow HTTP requests, you can access [request.setTimeout](https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback) by passing an optional timeout (ms) and timeoutCallback to `api.call()` and `api.multicall()`: | ||
```javascript | ||
var api = new API(userName, password, database, server, {ssl: false}, sessionId); | ||
// Timeout (ms) and TimeoutCallback | ||
api.call('Get', { | ||
typeName: 'User', | ||
resultsLimit: 1 | ||
}, function(err, data) { | ||
if(err){ | ||
console.log('Error', err); | ||
return; | ||
} | ||
console.log('User', data); | ||
}, 10000, function(){ | ||
console.log('Timeout'); | ||
}); | ||
// Just Timeout | ||
api.call('Get', { | ||
typeName: 'User', | ||
resultsLimit: 1 | ||
}, function(err, data) { | ||
if(err){ | ||
console.log('Error', err); | ||
return; | ||
} | ||
console.log('User', data); | ||
}, 10000); | ||
``` | ||
#### Session ID #### | ||
@@ -48,3 +85,3 @@ | ||
``` | ||
```javascript | ||
var api = new API(userName, password, database, server, options, sessionId); | ||
@@ -69,3 +106,3 @@ | ||
### Running Tests ### | ||
``` | ||
```javascript | ||
$ npm install -g mocha | ||
@@ -72,0 +109,0 @@ $ npm install |
@@ -310,2 +310,37 @@ var expect = require('chai').expect; | ||
}); | ||
it('makes a call with timeout length only', function (done) { | ||
nock('https://my3.geotab.com').post('/apiv1').delay(200).reply(200, credentialsResult); | ||
api = new API(userName, null, database, server, {ssl: false}, sessionId); | ||
let timedOut = true; | ||
api.call('Get', { | ||
typeName: 'User', | ||
search: { | ||
name: userName | ||
} | ||
}, function (err, data) { | ||
timedOut = false; | ||
done(); | ||
}, 100); | ||
expect(timedOut, 'api ran callback when timeout had passed'); | ||
}); | ||
it('makes a call with full timeout options', function (done) { | ||
nock('https://my3.geotab.com').post('/apiv1').delay(200).reply(200, credentialsResult); | ||
let timedOut = false; | ||
api.call('Get', { | ||
typeName: 'User', | ||
search: { | ||
name: userName | ||
} | ||
}, function (err, data) { | ||
done(); | ||
}, 100, function(){ | ||
timedOut = true; | ||
done(); | ||
}); | ||
expect(timedOut, 'api did not run timeoutCallback'); | ||
}); | ||
}); | ||
@@ -338,5 +373,5 @@ | ||
}; | ||
// http for convenience - api instance from above has ssl: false - gives 302 redirect as nock response with https | ||
nock('http://my3.geotab.com').post('/apiv1').reply(200, results); | ||
nock('https://my3.geotab.com').post('/apiv1').reply(200, results); | ||
api.multicall(calls, function (err, data) { | ||
@@ -348,2 +383,18 @@ expect(err).to.be.a('null'); | ||
}); | ||
it('times out on multicall', function (done) { | ||
var results = { | ||
result: [{ | ||
name: userName | ||
}, '5.7.22334.11'] | ||
}; | ||
nock('http://my3.geotab.com').post('/apiv1').delay(200).reply(200, results); | ||
let timedOut = false; | ||
api.multicall(calls, function (err, data) { | ||
done(); | ||
}, 100, function() { | ||
timedOut = true; | ||
}); | ||
expect(timedOut, 'Multicall did not timeout'); | ||
}); | ||
}); |
35045
539
107
6