Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![build status][build_image]][build]

# node-ari-client
# node-ari-client-plus

This module contains the Node.js client library for the Asterisk REST Interface.
It builds upon the swagger-js library, providing an improved, Asterisk-specific
Expand All @@ -11,7 +11,7 @@ API over the API generated by swagger-js.
## Installation

```bash
$ npm install ari-client
$ npm install ari-client-plus
```

## API
Expand All @@ -22,14 +22,14 @@ of ARI and to configure a client with all available resources and operations.
Callbacks:

```javascript
var client = require('ari-client');
var client = require('ari-client-plus');
client.connect(url, username, password, function (err, ari) {})
```

Promises:

```javascript
var client = require('ari-client');
var client = require('ari-client-plus');
client.connect(url, username, password)
.then(function (ari) {})
.catch(function (err) {});
Expand Down Expand Up @@ -3423,7 +3423,7 @@ function (err) {}
Callbacks:

```javascript
var client = require('ari-client'),
var client = require('ari-client-plus'),
util = require('util');

client.connect('http://localhost:8088', 'user', 'secret', client_loaded);
Expand Down Expand Up @@ -3481,7 +3481,7 @@ function client_loaded (err, ari) {
Promises:

```javascript
var client = require('ari-client'),
var client = require('ari-client-plus'),
Promise = require('bluebird'),
util = require('util');

Expand Down Expand Up @@ -3542,7 +3542,7 @@ client.connect('http://localhost:8088', 'user', 'secret')

# Testing

To run the mocha tests for ari-client, run the following:
To run the mocha tests for ari-client-plus, run the following:

```bash
$ npm test
Expand All @@ -3565,7 +3565,7 @@ Then run the following to run jshint and mocha tests:
$ npm test
```

jshint will enforce a minimal style guide. It is also a good idea to create unit tests when adding new features to ari-client.
jshint will enforce a minimal style guide. It is also a good idea to create unit tests when adding new features to ari-client-plus.

To generate a test coverage report run the following:

Expand Down
2 changes: 1 addition & 1 deletion dev/README.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ API over the API generated by swagger-js.
## Installation

```bash
$ npm install ari-client
$ npm i ari-client-plus
```

## API
Expand Down
59 changes: 41 additions & 18 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var _utils = require('./utils.js');
* @prop {Object} _instanceListeners - array of instance objects for
* instances that registered for scoped events keyed by event type
*/
function Client(baseUrl, user, pass) {
function Client(baseUrl, user, pass, options) {
var self = this;
events.EventEmitter.call(self);

Expand All @@ -55,6 +55,10 @@ function Client(baseUrl, user, pass) {
* @prop {string} hostname
* @prop {string} user - username for ARI instance
* @prop {string} pass - password for ARI instance
* @prop {Object} options - options for ARI instance
* @prop {number} options.maxRetries - maximum number of retries
* @prop {number} options.retryDelay - delay between retries
* @prop {number} options.retryMaxDelay - maximum delay between retries
*/
self._connection = {
protocol: parsedUrl.protocol,
Expand All @@ -63,7 +67,8 @@ function Client(baseUrl, user, pass) {
// support optional path prefix in asterisk http.conf
prefix: parsedUrl.pathname === '/' ? '' : parsedUrl.pathname,
user: user,
pass: pass
pass: pass,
options: options || {},
};

// Keep track of instance event listeners. once true means that the callback
Expand Down Expand Up @@ -111,20 +116,34 @@ Client.prototype._attachApi = function () {
self._connection.prefix
);

request(ariUrl, function (err) {
if (err &&
['ETIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED'].indexOf(err.code) !== -1) {
err.name = 'HostIsNotReachable';
request({
url: ariUrl,
auth: {
user: self._connection.user,
pass: self._connection.pass
}
}, function (err, res, body) {
if (err) {
return reject(err);
}

self.emit('APILoadError', err);
reject(err);
} else {
self._swagger = new swagger.SwaggerApi({
url: ariUrl,
success: swaggerLoaded,
failure: swaggerFailed
});
if (res?.statusCode === 401) {
const authErr = new Error("ARI Unauthorized (check username/password)");
self.emit("APILoadError", authErr);
return reject(authErr);
}

if (res?.statusCode >= 400) {
const httpErr = new Error(`ARI HTTP error ${res.statusCode}`);
self.emit("APILoadError", httpErr);
return reject(httpErr);
}

self._swagger = new swagger.SwaggerApi({
url: ariUrl,
success: swaggerLoaded,
failure: swaggerFailed
});
});

/**
Expand Down Expand Up @@ -349,7 +368,9 @@ Client.prototype.start = function (apps, subscribeAll, callback) {
}

var retry = backoff.create({
delay: 100
delay: self._connection.options.retryDelay || 100,
maxDelay: self._connection.options.retryMaxDelay,
maxRetries: self._connection.options.maxRetries,
});

connect();
Expand Down Expand Up @@ -478,7 +499,9 @@ Client.prototype.start = function (apps, subscribeAll, callback) {
processingError = false;
// reset backoff handler when we successfully connect
retry = backoff.create({
delay: 100
delay: self._connection.options.retryDelay || 100,
maxDelay: self._connection.options.retryMaxDelay,
maxRetries: self._connection.options.maxRetries,
});
self.emit('WebSocketConnected');
// onced, will not be called when an automatic reconnect succeeds.
Expand Down Expand Up @@ -594,7 +617,7 @@ Client.prototype.ping = function () {
* The callback to be called upon connection
* @returns {Q} promise - a promise that will resolve to a client
*/
module.exports.connect = function (baseUrl, user, pass,
module.exports.connect = function (baseUrl, user, pass, options,
/**
* @callback connectCallback
* @memberof module:ari-client
Expand All @@ -603,7 +626,7 @@ module.exports.connect = function (baseUrl, user, pass,
*/
callback) {

var client = new Client(baseUrl, user, pass);
var client = new Client(baseUrl, user, pass, options);
client.setMaxListeners(0);

return client._attachApi().asCallback(callback);
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "ari-client",
"version": "2.2.0",
"name": "ari-client-plus",
"version": "1.0.10",
"description": "JavaScript client for Asterisk REST Interface.",
"homepage": "https://github.com/asterisk/node-ari-client",
"homepage": "https://github.com/mostafaroshdy1/node-ari-client-plus",
"keywords": [
"Asterisk",
"ARI"
Expand All @@ -15,7 +15,7 @@
},
"repository": {
"type": "git",
"url": "git://github.com/asterisk/node-ari-client"
"url": "git://github.com/mostafaroshdy1/node-ari-client-plus"
},
"bugs": {
"url": "https://issues.asterisk.org",
Expand All @@ -32,6 +32,7 @@
"uuid": "^3.0.0",
"ws": "^6.0.0"
},
"types":"./types/index.d.ts" ,
"engines": {
"node": ">=8"
},
Expand Down
Loading