Skip to content

Commit

Permalink
Merge pull request #232 from plivo/hosted_messaging_number
Browse files Browse the repository at this point in the history
added support for Hosted Messaging Number feature
  • Loading branch information
huzaif-plivo authored Feb 23, 2022
2 parents ee93816 + 3865935 commit 343d0db
Show file tree
Hide file tree
Showing 12 changed files with 821 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [v4.28.0](https://github.com/plivo/plivo-node/tree/v4.28.0) (2022-02-23)
**Features - Numbers: Hosted Messaging API**
- Add support for Hosted Messaging APIs.

## [v4.27.0](https://github.com/plivo/plivo-node/tree/v4.27.0) (2022-02-02)
**Features - MPCStartCallRecording**
- Parameter change from statusCallback to recordingCallback
Expand Down
35 changes: 35 additions & 0 deletions examples/hostedMessagingNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var Plivo = require('../dist/rest/client.js');
var client = new Plivo.Client();

var createParams = {
alias:"alias",
applicationId: "applicationId",
loaId:"loaID",
number:"number"
};

var listParams = {
alias:"alias",
applicationId: "applicationId",
loaId:"loaID",
number:"number",
hostedStatus: "completed",
};


client.hostedMessagingNumber.create(createParams)
.then(function (hmn) {
console.log("\n============ created ===========\n", hmn)
return client.hostedMessagingNumber.get(hmn.hostedMessagingNumberId);
})
.then(function (hmn) {
console.log("\n============ get ===========\n", hmn)
return client.hostedMessagingNumber.list(listParams)
})
.then(function (hmns) {
console.log("\n============ list with params ===========\n", hmns)
})
.catch(function (err) {
console.log("\n============ Error :: ===========\n", err);
});

36 changes: 36 additions & 0 deletions examples/loa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var Plivo = require('../dist/rest/client.js');
var client = new Plivo.Client();

var createParams = {
"alias": "Test loa",
"file": "path to file"

};

var listParams = {
alias: "Test loa",
limit: 10,
offset: 5
};


client.loa.create(createParams)
.then(function (loa) {
console.log("\n============ created ===========\n", loa)
return client.loa.get(loa.loaId);
})
.then(function (loa) {
console.log("\n============ get ===========\n", loa)
return client.loa.delete(loa.loaId);
})
.then(function (result) {
console.log("\n============ deleted ===========\n", result)
return client.loa.list(listParams)
})
.then(function (loas) {
console.log("\n============ list with params ===========\n", loas)
})
.catch(function (err) {
console.log("\n============ Error :: ===========\n", err);
});

225 changes: 225 additions & 0 deletions lib/resources/hostedMessagingNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import {
PlivoResource, PlivoResourceInterface
} from '../base';
import {
extend, validate
} from '../utils/common.js';

const clientKey = Symbol();
const action = 'HostedMessagingNumber/';
const idField = 'hosted_messaging_number_id';

export class HostedMessagingNumberResponse {
constructor(params) {
params = params || {};
this.apiId = params.apiId;
this.alias = params.alias;
this.application = params.application
this.failureReason = params.failureReason
this.file = params.file;
this.hostedMessagingNumberId = params.hostedMessagingNumberId;
this.loaId = params.loaId;
this.number = params.number;
this.hostedStatus = params.hostedStatus;
this.linkedNumbers = params.linkedNumbers;
this.resourceUri = params.resourceUri;
this.createdAt = params.createdAt;
}
}

export class CreateHostedMessagingNumberResponse {
constructor(params) {
params = params || {};
this.apiId = params.apiId;
this.alias = params.alias;
this.application = params.application
this.failureReason = params.failureReason
this.file = params.file;
this.hostedMessagingNumberId = params.hostedMessagingNumberId;
this.loaId = params.loaId;
this.number = params.number;
this.hostedStatus = params.hostedStatus;
this.linkedNumbers = params.linkedNumbers;
this.resourceUri = params.resourceUri;
this.message = params.message;
this.createdAt = params.createdAt;
}
}

export class ListHostedMessagingNumberResponse {
constructor(params) {
params = params || {};
this.apiId = params.apiId;
this.meta = params.metaResponse;
this.objects = params.objects;
}
}


export class HostedMessagingNumber extends PlivoResource {
constructor(client, data = {}) {
super(action, HostedMessagingNumber, idField, client);
if (idField in data) {
this.id = data[idField];
}
this[clientKey] = client;
extend(this, data);
}

/**
* get HostedMessagingNumber by given id
* @method
* @param {string} id - id of the HostedMessagingNumber
* @promise {object} return {@link HostedMessagingNumber} object
* @fail {Error} return Error
*/
get(id) {
let client = this[clientKey];
return new Promise((resolve, reject) => {
if (action !== '' && !id) {
reject(new Error(this[idKey] + ' must be set'));
}
client('GET', action + (id ? id + '/' : ''))
.then(response => {
let object = new HostedMessagingNumberResponse(response.body, client);
Object.keys(object).forEach(key => object[key] === undefined && delete object[key]);
resolve(object);
})
.catch(error => {
reject(error);
});
});
}

/**
* list all HostedMessagingNumber
* @method
* @param {object} params - params containing options to list HostedMessagingNumber by.
* @param {string} [params.alias] - Alias
* @param {string} [params.hostedStatus] - Hosted Status
* @param {string} [params.number] - Phone Number
* @param {string} [params.loaId] - LOA ID
* @fail {Error} return Error
*/
list(params = {}) {
let client = this[clientKey];
return new Promise((resolve, reject) => {
client('GET', action, params)
.then(response => {
resolve(new ListHostedMessagingNumberResponse(response.body, idField));
})
.catch(error => {
reject(error);
});
});
}

/**
* Create a HostedMessagingNumber
* @method
* @param {object} params
* @param {string} [params.alias] - Alias
* @param {string} [params.applicationId] - Application ID
* @param {string} [params.number] - Phone Number
* @param {string} [params.applicationId] - LOA ID
* @fail {Error} return Error
*/
create(params = {}) {
let client = this[clientKey];
return new Promise((resolve, reject) => {
params.multipart = true;
client('POST', action, params)
.then(response => {
let object = new CreateHostedMessagingNumberResponse(response.body, idField);
Object.keys(object).forEach(key => object[key] === undefined && delete object[key]);
resolve(object);
})
.catch(error => {
reject(error);
});
})
}
}

/**
* Represents a HostedMessagingNumber interface
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
*/
export class HostedMessagingNumberInterface extends PlivoResourceInterface {
constructor(client, data = {}) {
super(action, HostedMessagingNumber, idField, client);
extend(this, data);
this[clientKey] = client;
}

/**
* get HostedMessagingNumber by given id
* @method
* @param {string} id - id of the HostedMessagingNumber
* @promise {object} return {@link HostedMessagingNumber} object
* @fail {Error} return Error
*/
get(id) {
let errors = validate([{field: 'id', value: id, validators: ['isRequired', 'isString']},]);
if (errors) {
return errors;
}
return new HostedMessagingNumber(this[clientKey], {id: idField}).get(id)
}

/**
* list all HostedMessagingNumber
* @method
* @param {object} params - params containing options to list HostedMessagingNumber by.
* @param {string} [params.alias] - Alias
* @param {string} [params.hostedStatus] - Hosted Status
* @param {string} [params.number] - Phone Number
* @param {string} [params.loaId] - LOA ID
* @fail {Error} return Error
*/
list(params = {}) {
const validations = []
if (params.hasOwnProperty("alias")) {
validations.push({field: 'alias', value: params.alias, validators: ['isString']})
}
if (params.hasOwnProperty("hostedStatus")) {
validations.push({field: 'hostedStatus', value: params.hostedStatus, validators: ['isString']})
}
if (params.hasOwnProperty("number")) {
validations.push({field: 'number', value: params.number, validators: ['isString']})
}
if (params.hasOwnProperty("loaId")) {
validations.push({field: 'loaId', value: params.loaId, validators: ['isString']})
}
let errors = validate(validations);
if (errors) {
return errors;
}
return new HostedMessagingNumber(this[clientKey], {id: idField}).list(params)
}

/**
* Create a HostedMessagingNumber
* @method
* @param {object} params
* @param {string} [params.alias] - Alias
* @param {string} [params.applicationId] - Application ID
* @param {string} [params.number] - Phone Number
* @param {string} [params.applicationId] - LOA ID
* @fail {Error} return Error
*/
create(params = {}) {
let errors = validate([
{field: 'alias', value: params.alias, validators: ['isRequired', 'isString']},
{field: 'applicationId', value: params.applicationId, validators: ['isRequired', 'isString']},
{field: 'loaId', value: params.loaId, validators: ['isRequired', 'isString']},
{field: 'number', value: params.number, validators: ['isRequired', 'isString']},
]);
if (errors) {
return errors;
}
return new HostedMessagingNumber(this[clientKey], {id: idField}).create(params)
}
}
Loading

0 comments on commit 343d0db

Please sign in to comment.