plivo-node/lib/base.js
nixonsam a4e3f9fd47
Add PHLO support (#112)
* Added phlo client.

* Finished implementaiton of initializing phlo client.

* Finished logic for v2 methods of multiparty call(not members).

* Added test cases for multiparty call.

* Finished logic for phlo get api.

* Added member method structure.

* Finished implementation of phlo.run().

* Finished api calls for multi party(except abort transfer).

* Deleted old rest.js file.

* Finished abortTranfer implementation with test cases.

* Finished implementation of member.resumeCall().

* Finished all member functions.

* Reverted debugging logic.

* Removed console.log

* Added samples for all apis. method structure change for abort transfer.

* Resolved nsp issue.

* Audit fix.

* Package Lock Fix.

* Added test proxy for test cases.

* Resolved issue in request.js json format.

* Improved comments.

* Added test cases for .get() methods.

* Removed async await to support older browsers.

* Modified examples of phlo, removed async await.

* Resolved example issues, remove auth credentials.

* Removed es6 declaration syntaxes from phlo example.

* Added logic to pass payload.

* changed beta version 4.1-beta.1

* changed beta version 4.1-beta.1

* changed beta version 4.1-beta.1

* changes in changelog & readme- Add PHLO support
2019-03-11 15:43:57 +05:30

144 lines
3.6 KiB
JavaScript

import { extend } from './utils/common.js';
let actionKey = Symbol('api action');
let klassKey = Symbol('constructor');
let idKey = Symbol('id filed');
let clientKey = Symbol('make api call');
export class PlivoGenericResponse {
constructor(params, idString) {
params = params || {};
if (typeof idString !== 'undefined' && (idString in params)) {
this.id = params[idString];
} else if ('request_uuid' in params) {
this.id = params.request_uuid;
}
extend(this, params);
}
}
export class PlivoResource {
constructor(action, klass, idField, request) {
this[actionKey] = action;
this[klassKey] = klass;
this[idKey] = idField;
this[clientKey] = request;
}
update(params, id) {
let client = this[clientKey];
let action = this[actionKey];
let that = this;
id = typeof id !== 'undefined' ? id : that.id;
return new Promise((resolve, reject) => {
client('POST', action + id + '/', params)
.then(response => {
extend(that, response.body);
extend(that, params);
resolve(that);
})
.catch(error => {
reject(error);
});
});
}
delete(params) {
let client = this[clientKey];
let action = this[actionKey];
let id = this.id;
return new Promise((resolve, reject) => {
client('DELETE', action + id + '/', params)
.then(() => {
resolve(true);
})
.catch(error => {
reject(error);
});
});
}
executeAction(task = '', method = 'GET', params = {}, action) {
let client = this[clientKey];
action = action == null ? this[actionKey] : action;
let idField = this[idKey];
return new Promise((resolve, reject) => {
client(method, action + task, params)
.then(response => {
resolve(new PlivoGenericResponse(response.body, idField));
})
.catch(error => {
reject(error);
});
});
}
}
export class PlivoResourceInterface {
constructor(action, klass, idField, request) {
this[actionKey] = action;
this[klassKey] = klass;
this[idKey] = idField;
this[clientKey] = request;
}
get(id, params = {}) {
let client = this[clientKey];
let action = this[actionKey];
let Klass = this[klassKey];
return new Promise((resolve, reject) => {
if (action !== '' && !id) {
reject(new Error(this[idKey] + ' must be set'));
}
client('GET', action + (id ? id + '/' : ''), params)
.then(response => {
resolve(new Klass(client, response.body));
})
.catch(error => {
reject(error);
});
});
}
list(params) {
let client = this[clientKey];
let action = this[actionKey];
let Klass = this[klassKey];
return new Promise((resolve, reject) => {
client('GET', action, params)
.then(response => {
let objects = [];
Object.defineProperty(objects, 'meta', { value: response.body.meta });
response.body.objects.forEach(item => {
objects.push(new Klass(client, item));
});
resolve(objects);
})
.catch(error => {
reject(error);
});
});
}
create(params) {
let client = this[clientKey];
let idField = this[idKey];
let action = this[actionKey] + (this.id ? this.id + '/' : '');
return new Promise((resolve, reject) => {
client('POST', action, params)
.then(response => {
resolve(new PlivoGenericResponse(response.body, idField));
})
.catch(error => {
reject(error);
});
});
}
}