Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for Provisioning API.

  • Loading branch information...
commit d79110389bdcf4d8241816270f7d19e79f7840b8 1 parent b9e4cb7
Mark Headd mheadd authored
1  lib/base.js
View
@@ -173,6 +173,7 @@ function replaceNull (key, value) {
return value;
};
+exports.serializeProperty = serializeProperty;
exports.Ask = Ask;
exports.Call = Call;
exports.Choices = Choices;
123 lib/tropo-provisioning.js
View
@@ -0,0 +1,123 @@
+var EventEmitter = require('events').EventEmitter;
+var http = require('http');
+var base = require('./base');
+
+var provisioningUrlHost = 'api.tropo.com';
+var provisioningUrlPath = '/v1/';
+
+TropoProvision = function(userid, password) {
+ this.userid = userid;
+ this.password = password;
+};
+
+TropoProvision.prototype = new EventEmitter;
+
+TropoProvision.prototype.createApplication = function(name, voiceUrl, messagingUrl, platform, partition) {
+ payload = base.serializeProperty(new Application(null, name, voiceUrl, messagingUrl, platform, partition));
+ var path = provisioningUrlPath + 'applications';
+ this.makeApiCall('POST', provisioningUrlHost, path, payload);
+};
+
+TropoProvision.prototype.updateApplicationAddress = function(applicationID, type, prefix, number, city, state, channel, username, password, token) {
+ payload = base.serializeProperty(new Address(type, prefix, number, city, state, channel, username, password, token));
+ var path = provisioningUrlPath + 'applications/' + applicationID + '/addresses';
+ return this.makeApiCall('POST', provisioningUrlHost, path, payload);
+};
+
+TropoProvision.prototype.updateApplicationProperty = function(applicationID, href, name, voiceUrl, messagingUrl, platform, partition) {
+ payload = base.serializeProperty(new Application(null, href, name, voiceUrl, messagingUrl, platform, partition));
+ var path = provisioningUrlPath + 'applications/' + applicationID;
+ return this.makeApiCall('PUT', provisioningUrlHost, path, payload);
+};
+
+TropoProvision.prototype.deleteApplication = function(applicationID) {
+ var path = provisioningUrlPath + 'applications/' + applicationID;
+ return this.makeApiCall('DELETE', provisioningUrlHost, path, null);
+};
+
+TropoProvision.prototype.deleteApplicationAddress = function(applicationID, type, address) {
+ var path = provisioningUrlPath + 'applications/' + applicationID + '/addresses/' + type + '/' + address;
+ return this.makeApiCall('DELETE', provisioningUrlHost, path, null);
+};
+
+TropoProvision.prototype.viewApplications = function() {
+ var path = provisioningUrlPath + 'applications';
+ return this.makeApiCall('GET', provisioningUrlHost, path, null);
+};
+
+TropoProvision.prototype.viewSpecificApplication = function(applicationID) {
+ var path = provisioningUrlPath + 'applications/' + applicationID;
+ return this.makeApiCall('GET', provisioningUrlHost, path, null);
+};
+
+TropoProvision.prototype.viewAddresses = function(applicationID) {
+ var path = provisioningUrlPath + 'applications/' + applicationID + '/addreses';
+ return this.makeApiCall('GET', provisioningUrlHost, path, null);
+};
+
+vviewExchanges = function() {
+ var path = provisioningUrlPath + 'exchanges';
+ return this.makeApiCall('GET', provisioningUrlHost, path, null);
+};
+
+TropoProvision.prototype.makeApiCall = function(method, host, path, payload) {
+
+ var self = this;
+
+ // Set up basic auth.
+ var auth = 'Basic ' + new Buffer(this.userid + ':' + this.password).toString('base64');
+
+ // Set up request headers.
+ var payloadLength = payload === null ? 0 : payload.length;
+ var headers = {'host': host, 'Authorization': auth , 'Content-Length': payloadLength, 'Content-Type': 'application/json'};
+
+ // Set up the HTTP client, set headers and stream the JSON body.
+ var tropo = http.createClient('80', host);
+ var request = tropo.request(method, path, headers);
+ if(payload) {
+ request.write(payload);
+ }
+ request.end();
+
+ // Process the response.
+ request.on('response', function(response) {
+ self.emit('responseCode', response.statusCode);
+ response.setEncoding('utf8');
+ response.on('data', function(data) {
+ self.emit('responseBody', data.toString());
+ });
+ });
+
+};
+
+exports.TropoProvision = TropoProvision;
+
+// Objects representing provisioning resources.
+Application = function(href, name, voiceUrl, messagingUrl, platform, partition) {
+ this.href = href;
+ this.name = name;
+ this.voiceUrl = voiceUrl;
+ this.messagingUrl = messagingUrl;
+ this.platform = platform;
+ this.partition = partition;
+
+};
+
+Address = function(type, prefix, number, city, state, channel, username, password, token) {
+ this.type = type;
+ this.prefix = prefix;
+ this.city = city;
+ this.state = state;
+ this.channel = channel;
+ this.username = username;
+ this.password = password;
+ this.token = token;
+};
+
+Exchange = function(prefix, city, state, country) {
+ this.prefix = prefix;
+ this.city = city;
+ this.state = state;
+ this.country = country;
+};
+
25 samples/provisioning/add-address.js
View
@@ -0,0 +1,25 @@
+/**
+ * Updating an Application to Add an AIM Address
+ * The updateApplicationAddress() method can be used to add an AIM account to your application.
+ * The same method can be used to add YAHOO, MSN, JABBER, GTALK and SKYPE.
+ *
+ */
+
+require('../../lib/tropo-provisioning');
+var sys = require('sys');
+
+var userid = '';
+var password = '';
+var applicationID = '';
+
+var p = new TropoProvision(userid, password);
+
+p.updateApplicationAddress(applicationID, 'aim', null, null, null, null, null, 'AIMUser01', 'password', null);
+
+p.addListener('responseCode', function(code) {
+ sys.puts('Response code: ' + code);
+});
+
+p.addListener('responseBody', function(body) {
+ sys.puts('Response body: ' + body);
+});
26 samples/provisioning/add-number.js
View
@@ -0,0 +1,26 @@
+
+/**
+ * Updating an Application to Add a Number from the Pool
+ * the updateApplicationAddress() method can be used to add a number from the pool of available Tropo numbers,
+ * based on a specified prefix.
+ *
+ */
+
+require('../../lib/tropo-provisioning');
+var sys = require('sys');
+
+var userid = '';
+var password = '';
+var applicationID = '';
+
+var p = new TropoProvision(userid, password);
+
+p.updateApplicationAddress(applicationID, 'number', '1407', null, null, null, null, null, null, null);
+
+p.addListener('responseCode', function(code) {
+ sys.puts('Response code: ' + code);
+});
+
+p.addListener('responseBody', function(body) {
+ sys.puts('Response body: ' + body);
+});
25 samples/provisioning/add-token.js
View
@@ -0,0 +1,25 @@
+/**
+ * Updating an Application to Add a Voice Token
+ * The updateApplicationAddress() method can be used to add a voice token to your application; you can add a messaging token just by
+ * changing the channel to "messaging" instead of "voice".
+ *
+ */
+
+require('../../lib/tropo-provisioning');
+var sys = require('sys');
+
+var userid = '';
+var password = '';
+var applicationID = '';
+
+var p = new TropoProvision(userid, password);
+
+p.updateApplicationAddress(applicationID, 'token', null, null, null, null, 'messaging', null, null, null);
+
+p.addListener('responseCode', function(code) {
+ sys.puts('Response code: ' + code);
+});
+
+p.addListener('responseBody', function(body) {
+ sys.puts('Response body: ' + body);
+});
32 samples/provisioning/create-app.js
View
@@ -0,0 +1,32 @@
+/**
+ * Creating a New Application
+ * Use the createApplication() method to add a brand new application.
+ * You can define a voice and messaging URL in the Request Body, but this
+ * method won't assign any addresses. You'll need to update the
+ * application once it's created to add a phone number or IM account.
+ *
+ */
+
+require('../../lib/tropo-provisioning');
+var sys = require('sys');
+
+var userid = '';
+var password = '';
+var name = 'My Awesome Node App';
+var voiceURL = 'http://somefakehost.com/tropo/app.json';
+var messagingURL = 'http://somefakehost.com/tropo/app.json';
+var platform = 'webapi';
+var partition = 'staging';
+
+var p = new TropoProvision(userid, password);
+
+p.createApplication(name, voiceURL, messagingURL, platform, partition);
+
+p.addListener('responseCode', function(code) {
+ sys.puts('Response code: ' + code);
+});
+
+p.addListener('responseBody', function(body) {
+ sys.puts('Response body: ' + body);
+});
+
25 samples/provisioning/delete-address.js
View
@@ -0,0 +1,25 @@
+/**
+ * Deleting an Address
+ * You can use the deleteApplicationAddress() method remove a phone number, IM account or token from an application.
+ *
+ */
+
+require('../../lib/tropo-provisioning');
+var sys = require('sys');
+
+var userid = '';
+var password = '';
+var applicationID = '';
+var myNumber = '';
+
+var p = new TropoProvision(userid, password);
+
+p.deleteApplicationAddress(applicationID, 'number', myNumber);
+
+p.addListener('responseCode', function(code) {
+ sys.puts('Response code: ' + code);
+});
+
+p.addListener('responseBody', function(body) {
+ sys.puts('Response body: ' + body);
+});
25 samples/provisioning/delete-app.js
View
@@ -0,0 +1,25 @@
+/**
+ * Deleting an Application
+ * Use the deleteApplication() method to remove an application. This cannot be undone; once an application has been
+ * deleted, it cannot be restored without recreating it from scratch.
+ *
+ */
+
+require('../../lib/tropo-provisioning');
+var sys = require('sys');
+
+var userid = '';
+var password = '';
+var applicationID = '';
+
+var p = new TropoProvision(userid, password);
+
+p.deleteApplication(applicationID);
+
+p.addListener('responseCode', function(code) {
+ sys.puts('Response code: ' + code);
+});
+
+p.addListener('responseBody', function(body) {
+ sys.puts('Response body: ' + body);
+});
Please sign in to comment.
Something went wrong with that request. Please try again.