-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from plivo/hosted_messaging_number
added support for Hosted Messaging Number feature
- Loading branch information
Showing
12 changed files
with
821 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.