diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f80ce2..1db41f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Change Log +## [4.1.8](https://github.com/plivo/plivo-node/releases/tag/v4.1.8)(2019-12-20) +- Add Powerpack support. + ## [4.1.7](https://github.com/plivo/plivo-node/releases/tag/v4.1.7)(2019-12-04) - Add MMS support. diff --git a/examples/powerpacks.js b/examples/powerpacks.js new file mode 100644 index 0000000..9f53d57 --- /dev/null +++ b/examples/powerpacks.js @@ -0,0 +1,39 @@ +var Plivo = require('../dist/rest/client.js'); +var client = new Plivo.Client(); + +var params = { + sticky_sender: true, + local_connect: true +}; +// create powerpack +client.powerpacks.create("node sdk test", params).then(function (result) { + console.log(result) +}); + +// get powerpack +client.powerpacks.get("0166c910-1268-47c7-bf30-f5809ee843b9").then( + function (result) { + console.log(result) + }); + +// list powerpack +client.powerpacks.list().then( + function (result) { + console.log(result) + }); +// list numbers +client.powerpacks.get("0166c910-1268-47c7-bf30-f5809ee843b9").then( + function (powerpack) { + return powerpack.list_numbers() + }) + .then(function (result) { + console.log("\n============ list numbers ===========\n", result) + }) +// find number +client.powerpacks.get("0166c910-1268-47c7-bf30-f5809ee843b9").then( + function (powerpack) { + return powerpack.find_number('1234') + }) + .then(function (result) { + console.log("\n============ list numbers ===========\n", result) + }) diff --git a/lib/base.js b/lib/base.js index 4597b8e..d687ac6 100644 --- a/lib/base.js +++ b/lib/base.js @@ -75,6 +75,47 @@ export class PlivoResource { }); }); } + customexecuteAction(url, method = 'GET', params = {}) { + let client = this[clientKey]; + let idField = this[idKey]; + return new Promise((resolve, reject) => { + client(method, url, params) + .then(response => { + resolve(new PlivoGenericResponse(response.body, idField)); + }) + .catch(error => { + reject(error); + }); + }); + } + customexecuteGetNumberAction(url, method = 'GET', params = {}) { + let client = this[clientKey]; + let idField = this[idKey]; + let number = ""; + let promise = client(method, url, params).then(response => { + number = response.body.objects[0].number; + return number; + }).catch(error => { + console.log(error); + return error; + }); + return promise; + } + getMetaResponse(url, method = 'GET', params = {}) { + let client = this[clientKey]; + let idField = this[idKey]; + let count = 0; + return new Promise((resolve, reject) => { + client(method, url, params) + .then(response => { + count = response.body.meta.totalCount; + resolve(count); + }) + .catch(error => { + reject(error); + }); + }); + } } export class PlivoResourceInterface { diff --git a/lib/resources/messages.js b/lib/resources/messages.js index e0d53cd..d674f1c 100644 --- a/lib/resources/messages.js +++ b/lib/resources/messages.js @@ -35,9 +35,7 @@ export class Message extends PlivoResource { deleteMedia() { return super.executeAction(this.id + '/Media/', 'Delete', {}); } - getMedia(mediaID) { - return super.executeAction(this.id + '/Media/' + mediaID + '/', 'Get', {}); - } + } /** * Represents a Message Interface @@ -151,11 +149,7 @@ export class MessageInterface extends PlivoResourceInterface { id: messageUUID }).listMedia(); } - getMedia(messageUUID, mediaID) { - return new Message(this[clientKey], { - id: messageUUID - }).getMedia(mediaID); - } + deleteMedia(messageUUID) { return new Message(this[clientKey], { id: messageUUID diff --git a/lib/resources/powerpacks.js b/lib/resources/powerpacks.js new file mode 100644 index 0000000..71c9cac --- /dev/null +++ b/lib/resources/powerpacks.js @@ -0,0 +1,409 @@ +import { extend, validate } from '../utils/common.js'; +import { PlivoResource, PlivoResourceInterface } from '../base'; +import * as _ from 'lodash'; + +const action = 'Powerpack/'; +const idField = 'uuid'; +const numberpoolIdField = 'numberPool'; +const clientKey = Symbol(); + +/** + * Represents a Powerpack + * @constructor + * @param {function} client - make api call + * @param {object} [data] - data of call + */ +export class Powerpack extends PlivoResource { + constructor(client, data = {}) { + super(action, Powerpack, idField, client); + + if (idField in data) { + this.uuid = data[idField]; + } + if (numberpoolIdField in data) { + this.number_pool_id = data[numberpoolIdField].split('/')[5]; + } + this.number_pool = new NumberPool(client, { + number_pool_id: this.number_pool_id + }); + extend(this, data); + } + + list_numbers(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number/?' + query; + return super.customexecuteAction(path.toString().trim(), 'GET', queryparams); + } + + search_query(params) { + if (params === undefined) { + params = {}; + } + var query = ''; + if (params.type != undefined) { + query = 'type=' + params.type; + } + if (params.starts_with != undefined) { + if (query == '') { + query = 'starts_with=' + params.starts_with; + } else { + query += '&starts_with=' + params.starts_with; + } + } + // return params; + if (params.country_iso2 != undefined) { + if (query == '') { + query = 'country_iso2=' + params.country_iso2; + } else { + query += '&country_iso2=' + params.country_iso2; + } + } + if (params.limit != undefined) { + if (query == '') { + query = 'limit=' + params.limit; + } else { + query += '&limit=' + params.limit; + } + } + if (params.offset != undefined) { + if (query == '') { + query = 'offset=' + params.offset; + } else { + query += '&offset=' + params.offset; + } + } + + query = query + '&'; + + return query; + } + + count_numbers(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number/?' + query; + return super.getMetaResponse(path.toString().trim(), 'GET', queryparams); + } + + find_number(number) { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } + add_number(number) { + var params = {}; + params['rent'] = 'false'; + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', params); + } + remove_number(number, unrent = false) { + var params = {}; + if (typeof unrent === 'boolean') { + params['unrent'] = unrent.toString(); + } else { + params['unrent'] = unrent; + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'DELETE', params); + } + list_shortcodes(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; + return super.customexecuteAction(path.toString().trim(), 'GET', params); + } + find_shortcode(shortcode) { + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } + buy_add_number(params) { + var number = params.number; + var rentparam = {}; + rentparam['rent'] = 'true'; + if (params.number == undefined) { + try { + if (params.country_iso2 != undefined) { + params['country_iso'] = params.country_iso2; + } + var test = super.customexecuteGetNumberAction('PhoneNumber/', 'GET', params); + return test.then((val) => { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + val + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + }); + } catch (err) { + return err.message; + } + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + } + + /** + * update powerpack + * @method + * @param {object} params - to update Powerpack + * @param {string} [params.name] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @param {string} [params.sticky_sender] + * @param {string} [params.local_connect] + + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ + update(params) { + let path = 'Powerpack/' + this.uuid + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', params); + } + + /** + * delete Powerpack + * @method + * @promise {object} return true on success + * @fail {Error} return Error + */ + delete(unrent_numbers = false) { + let params = {}; + if (typeof unrent_numbers === 'boolean') { + params.unrent_numbers = unrent_numbers.toString(); + } + let path = 'Powerpack/' + this.uuid + '/'; + return super.customexecuteAction(path.toString().trim(), 'DELETE', params); + } +} +const numberPoolField = 'number_pool_id'; +export class NumberPool extends PlivoResource { + constructor(client, data = {}) { + super(action, NumberPool, numberPoolField, client); + this.numbers = new Numbers(client, { + number_pool_id: data.number_pool_id + }); + this.shortcodes = new Shortcode(client, { + number_pool_id: data.number_pool_id + }); + + extend(this, data); + } +} + +export class Numbers extends PlivoResource { + constructor(client, data = {}) { + super(action, Numbers, numberPoolField, client); + extend(this, data); + } + buy_add_number(params) { + var number = params.number; + var rentparam = {}; + rentparam['rent'] = 'true'; + if (params.number == undefined) { + try { + if (params.country_iso2 != undefined) { + params['country_iso'] = params.country_iso2; + } + var test = super.customexecuteGetNumberAction('PhoneNumber/', 'GET', params); + return test.then((val) => { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + val + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + }); + } catch (err) { + return err.message; + } + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', rentparam); + } + + list(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number//?' + query; + return super.customexecuteAction(path.toString().trim(), 'GET', queryparams); + } + count(params) { + let query = this.search_query(params); + var queryparams = {}; + queryparams['search'] = 'hack'; + let path = 'NumberPool/' + this.number_pool_id + '/Number//?' + query; + return super.getMetaResponse(path.toString().trim(), 'GET', queryparams); + } + + search_query(params) { + if (params === undefined) { + params = {}; + } + var query = ''; + if (params.type != undefined) { + query = 'type=' + params.type; + } + if (params.starts_with != undefined) { + if (query == '') { + query = 'starts_with=' + params.starts_with; + } else { + query += '&starts_with=' + params.starts_with; + } + } + // return params; + if (params.country_iso2 != undefined) { + if (query == '') { + query = 'country_iso2=' + params.country_iso2; + } else { + query += '&country_iso2=' + params.country_iso2; + } + } + if (params.limit != undefined) { + if (query == '') { + query = 'limit=' + params.limit; + } else { + query += '&limit=' + params.limit; + } + } + if (params.offset != undefined) { + if (query == '') { + query = 'offset=' + params.offset; + } else { + query += '&offset=' + params.offset; + } + } + + query = query + '&'; + + return query; + } + + find(number) { + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } + add(number) { + var params = {}; + params['rent'] = 'false'; + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'POST', params); + } + remove(number, unrent = false) { + var params = {}; + if (typeof unrent === 'boolean') { + params['unrent'] = unrent.toString(); + } else { + params['unrent'] = unrent; + } + let path = 'NumberPool/' + this.number_pool_id + '/Number/' + number + '/'; + return super.customexecuteAction(path.toString().trim(), 'DELETE', params); + } +} +export class Shortcode extends PlivoResource { + constructor(client, data = {}) { + super(action, Shortcode, numberPoolField, client); + extend(this, data); + this.number_pool_id = data.number_pool_id; + } + list(params) { + if (params === undefined) { + params = {}; + } + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/'; + return super.customexecuteAction(path.toString().trim(), 'GET', params); + } + find(shortcode) { + let path = 'NumberPool/' + this.number_pool_id + '/Shortcode/' + shortcode + '/'; + return super.customexecuteAction(path.toString().trim(), 'GET'); + } +} +/** + * Represents a Powerpack interface + * @constructor + * @param {function} client - make api call + * @param {object} [data] - data of call + */ +export class PowerpackInterface extends PlivoResourceInterface { + constructor(client, data = {}) { + super(action, Powerpack, idField, client); + extend(this, data); + } + + /** + * get Powerpack by given id + * @method + * @param {string} uuid - id of Powerpack + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ + get(uuid) { + return super.get(uuid); + } + + /** + * create Powerpack + * @method + * @param {string} name - name of Powerpack + * @param {object} params - params to create Powerpack + * @param {string} [params.sticky_sender] - + * @param {string} [params.local_connect] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @promise {object} return {@link PlivoGenericResponse} object + * @fail {Error} return Error + */ + create(name, params = {}) { + let errors = validate([ + { + field: 'name', + value: name, + validators: [ 'isRequired', 'isString' ] + } + ]); + + if (errors) { + return errors; + } + + params.name = name; + + return super.create(params); + } + + /** + * update Powerpack + * @method + * @param {string} uuid - id of Powerpack + * @param {object} params - to update Powerpack + * @param {string} [params.name] + * @param {string} [params.sticky_sender] + * @param {string} [params.local_connect] + * @param {string} [params.application_type] + * @param {string} [params.application_id] + * @promise {object} return {@link Powerpack} object + * @fail {Error} return Error + */ + update(uuid, params) { + let errors = validate([ + { + field: 'uuid', + value: uuid, + validators: [ 'isRequired' ] + } + ]); + + if (errors) { + return errors; + } + return new Powerpack(this[clientKey], { + uuid: uuid + }).update(params); + } + + /** + * Get All Call Detail + * @method + * @param {object} params - params to get all call details. + * @promise {object[]} returns list of Call Object + * @fail {Error} returns Error + */ + list(params) { + return super.list(params); + } +} diff --git a/lib/rest/client-test.js b/lib/rest/client-test.js index 0362087..ef44b03 100644 --- a/lib/rest/client-test.js +++ b/lib/rest/client-test.js @@ -7,6 +7,7 @@ import { ApplicationInterface } from '../resources/applications.js'; import { ConferenceInterface } from '../resources/conferences.js'; import { EndpointInterface } from '../resources/endpoints.js'; import { MessageInterface } from '../resources/messages.js'; +import { PowerpackInterface } from '../resources/powerpacks.js'; import { NumberInterface } from '../resources/numbers.js'; import { PricingInterface } from '../resources/pricings.js'; import { RecordingInterface } from '../resources/recordings.js'; @@ -47,6 +48,7 @@ export class Client { this.conferences = new ConferenceInterface(client); this.endpoints = new EndpointInterface(client); this.messages = new MessageInterface(client); + this.powerpacks = new PowerpackInterface(client); this.numbers = new NumberInterface(client); this.pricings = new PricingInterface(client); this.recordings = new RecordingInterface(client); diff --git a/lib/rest/client.js b/lib/rest/client.js index 870cbc9..ba04f67 100644 --- a/lib/rest/client.js +++ b/lib/rest/client.js @@ -8,6 +8,7 @@ import { ApplicationInterface } from "../resources/applications"; import { ConferenceInterface } from "../resources/conferences"; import { EndpointInterface } from "../resources/endpoints"; import { MessageInterface } from "../resources/messages"; +import { PowerpackInterface } from "../resources/powerpacks"; import { NumberInterface } from "../resources/numbers"; import { PricingInterface } from "../resources/pricings"; import { RecordingInterface } from "../resources/recordings"; @@ -60,6 +61,7 @@ export class Client { this.conferences = new ConferenceInterface(client); this.endpoints = new EndpointInterface(client); this.messages = new MessageInterface(client); + this.powerpacks = new PowerpackInterface(client); this.numbers = new NumberInterface(client); this.pricings = new PricingInterface(client); this.recordings = new RecordingInterface(client); @@ -121,4 +123,4 @@ export class PhloClient { } } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 8003c30..27f0b5c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plivo", - "version": "4.1.7", + "version": "4.1.8", "description": "A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML", "homepage": "https://github.com/plivo/plivo-node", "files": [ diff --git a/test/powerpacks.js b/test/powerpacks.js new file mode 100644 index 0000000..9956646 --- /dev/null +++ b/test/powerpacks.js @@ -0,0 +1,91 @@ +import assert from 'assert'; +import sinon from 'sinon'; +import { + Client +} from '../lib/rest/client-test'; +import { + PlivoGenericResponse +} from '../lib/base.js'; + +let client = new Client('sampleid', 'sammpletoken', 'sampleproxy'); + +describe('PowerpackInterface', function () { + it('Get Details of a Powerpack', function () { + return client.powerpacks.get('5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46') + .then(function (powerpack) { + assert.equal(powerpack.uuid, '5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46') + }) + }); + it('should create powerpack via interface', function () { + return client.powerpacks.create("node sdk test") + .then(function (powerpack) { + assert.equal(powerpack.name, 'node sdk test') + }) + }); + + it('list powerpacks numbers via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.list_numbers() + }) + .then(function (result) { + assert.notEqual(result.length, 0) + }) + }); + + it('delete powerpacks via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.delete() + }) + .then(function (result) { + assert.notEqual(result.response, "success") + }) + }); + it('list powerpacks numbers via interface', function () { + return client.powerpacks.list() + .then(function (powerpack) { + assert.notEqual(powerpack.length, 0) + }) + }); + + it('add numbers to powerpack via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.add_number('14845733595') + }) + .then(function (result) { + assert.Equal(result.number, "14845733595") + }) + }); + + it('find numbers to powerpack via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.find_number('14845733595') + }) + .then(function (result) { + assert.Equal(result.number, "14845733595") + }) + }); + + it('find shortcode via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.find_shortcode('4444444') + }) + .then(function (result) { + assert.Equal(result.shortcode, "4444444") + }) + }); + it('list shortcode via interface', function () { + client.powerpacks.get("5ec4c8c9-cd74-42b5-9e41-0d7670d6bb46").then( + function (powerpack) { + return powerpack.list_shortcode('4444444') + }) + .then(function (result) { + assert.notEqual(result.length, 0) + }) + }); + +});