Skip to content

Commit 308e2c8

Browse files
authored
Merge pull request #16 from Bandwidth/DX-1326
Dx 1326: Manage applications functionality
2 parents 23ac71f + acaf8c1 commit 308e2c8

File tree

5 files changed

+410
-2
lines changed

5 files changed

+410
-2
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,72 @@ All properties are camel-cased for Javascript readability, and are converted on
144144
case by the internals of the API when converted to XML.
145145

146146

147+
## Applications
148+
### Create Voice Application
149+
150+
```Javascript
151+
var data = {
152+
appName:"test app",
153+
callInitiatedCallbackUrl: "http://example.com",
154+
callInitiatedMethod: "POST",
155+
callStatusCallbackUrl: "http://example.com",
156+
callStatusMethod: "POST"
157+
callbackCreds: {
158+
userId: 'my-id',
159+
password: 'my-password'
160+
}
161+
};
162+
163+
numbers.Application.createVoiceApplication(data, callback);
164+
```
165+
166+
### Create Messaging Application
167+
168+
```Javascript
169+
var data = {
170+
appName:"test app",
171+
msgCallbackUrl: "http://example.com",
172+
callbackCreds: {
173+
userId: 'my-id',
174+
password: 'my-password'
175+
}
176+
};
177+
178+
numbers.Application.createMessagingApplication(data, callback);
179+
```
180+
181+
### List All Applications
182+
183+
```Javascript
184+
numbers.Application.list(callback);
185+
```
186+
187+
### Get an Application
188+
```Javascript
189+
numbers.Application.get(id, callback);
190+
```
191+
192+
### Update an Application
193+
```Javascript
194+
numbers.Application.get(id, (err, app) => {
195+
app.appName = "new name";
196+
app.update(app, callback);
197+
});
198+
```
199+
200+
### Delete an Application
201+
```Javascript
202+
numbers.Application.get(id, (err, app) => {
203+
app.delete(callback)
204+
});
205+
```
206+
207+
### Get SipPeers Associated With and Application
208+
```Javascript
209+
numbers.Application.get(id, (err, app) => {
210+
app.getSipPeers(callback);
211+
});
212+
```
147213

148214
## Available Numbers
149215

lib/application.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
var Client = require("./client");
2+
var APPLICATION_PATH = "applications";
3+
4+
function Application(){
5+
6+
}
7+
8+
Application.get = function(client, id, callback) {
9+
if(arguments.length === 2){
10+
callback = id;
11+
id = client;
12+
client = new Client();
13+
}
14+
client.makeRequest("get", client.concatAccountPath(APPLICATION_PATH), null, id, function(err, res){
15+
if(err){
16+
return callback(err);
17+
}
18+
var item = res.application;
19+
item.client = client;
20+
item.__proto__ = Application.prototype;
21+
callback(null, item);
22+
});
23+
}
24+
25+
Application.list = function(client, callback){
26+
if(arguments.length === 1){
27+
callback = client;
28+
client = new Client();
29+
}
30+
client.makeRequest("get", client.concatAccountPath(APPLICATION_PATH), function(err, res){
31+
if(err){
32+
return callback(err);
33+
}
34+
var items = res.applicationList.application || [];
35+
if(!Array.isArray(items)){
36+
items = [items];
37+
}
38+
var result = items.map(function(item){
39+
var i = item;
40+
i.client = client;
41+
i.__proto__ = Application.prototype;
42+
return i;
43+
});
44+
callback(null, result);
45+
});
46+
};
47+
48+
Application._createApplication = function(client, item, callback){
49+
if(arguments.length === 2){
50+
callback = item;
51+
item = client;
52+
client = new Client();
53+
}
54+
var request = client.createPostRequest(client.concatAccountPath(APPLICATION_PATH), {application: item});
55+
request.buffer().then(res =>{
56+
if(res.ok && res.headers.location){
57+
Client.getIdFromLocationHeader(res.headers.location, function(err, id){
58+
if(err){
59+
return callback(err);
60+
}
61+
Application.get(client, id, callback);
62+
});
63+
}
64+
else{
65+
client.checkResponse(res, callback);
66+
}
67+
}).catch(err => {
68+
return callback(err);
69+
});
70+
};
71+
72+
Application.createVoiceApplication = function(client, item, callback){
73+
if(arguments.length === 2){
74+
callback = item;
75+
item = client;
76+
client = new Client();
77+
}
78+
item.ServiceType = 'Voice-V2';
79+
this._createApplication(client, item, callback);
80+
};
81+
Application.createMessagingApplication = function(client, item, callback){
82+
if(arguments.length === 2){
83+
callback = item;
84+
item = client;
85+
client = new Client();
86+
}
87+
item.ServiceType = 'Messaging-V2'
88+
this._createApplication(client, item, callback);
89+
};
90+
Application.prototype.update = function(data, callback){
91+
this.client.makeRequest("put", this.client.concatAccountPath(APPLICATION_PATH) + "/" + this.applicationId, {application: data}, callback);
92+
};
93+
94+
Application.prototype.delete = function(callback){
95+
this.client.makeRequest("delete", this.client.concatAccountPath(APPLICATION_PATH) + "/" + this.applicationId, callback);
96+
};
97+
98+
Application.prototype.getSipPeers = function(query, callback){
99+
if(arguments.length === 1){
100+
callback = query;
101+
query = {};
102+
}
103+
this.client.makeRequest("get", this.client.concatAccountPath(APPLICATION_PATH) + "/" + this.applicationId + "/associatedsippeers", function(err,res){
104+
if(err){
105+
return callback(err);
106+
}
107+
let sipPeers = res.associatedSipPeers.associatedSipPeer;
108+
if(!Array.isArray(sipPeers)){
109+
sipPeers = [sipPeers];
110+
}
111+
callback(null,sipPeers);
112+
});
113+
};
114+
115+
module.exports = Application;

lib/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ module.exports = {
3030
RemoveImportedTnOrder: require("./removeImportedTnOrder"),
3131
CsrOrder: require("./csrOrder"),
3232
EmergencyNotification: require("./emergencyNotification"),
33-
Aeuis: require("./aeuis")
33+
Aeuis: require("./aeuis"),
34+
Application: require('./application')
3435
};
3536

3637
for (const property in module.exports) {
3738
Promise.promisifyAll(module.exports[property]);
3839
if (module.exports[property].hasOwnProperty("prototype")) {
3940
Promise.promisifyAll(module.exports[property].prototype);
4041
}
41-
}
42+
}

0 commit comments

Comments
 (0)