Skip to content

Commit

Permalink
Merge pull request #284 from Nexmo/v2.6.0
Browse files Browse the repository at this point in the history
Allow API URL overriding
  • Loading branch information
AlexLakatos authored Jan 20, 2020
2 parents 51773f5 + d158604 commit bd19e4b
Show file tree
Hide file tree
Showing 23 changed files with 547 additions and 47 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).

## 2.60
- ADDED: Change host via the config object, using `apiHost` & `restHost`

## 2.5.3
- FIXED: URI Encode Signed SMS Message

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ Options are:
warn: function() {args...}
},
// Set a custom timeout for requests to Nexmo in milliseconds. Defaults to the standard for Node http requests, which is 120,000 ms.
timeout: integer
timeout: integer,
// Set a custom host for requests instead of api.nexmo.com
apiHost: string,
// Set a custom host for requests instead of rest.nexmo.com
restHost: string
}
```

Expand Down
4 changes: 3 additions & 1 deletion examples/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ var config = {
CODE: process.env.CODE || '',
APP_ID: process.env.APP_ID || '',
PRIVATE_KEY: process.env.PRIVATE_KEY || '',
DEBUG: process.env.DEBUG === 'true'
DEBUG: process.env.DEBUG === 'true',
REST_HOST: process.env.REST_HOST || 'rest.nexmo.com',
API_HOST: process.env.API_HOST || 'api.nexmo.com'
};

module.exports = config;
50 changes: 50 additions & 0 deletions examples/ex-change-api-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module.exports = function(callback, config) {

var Nexmo = require('../lib/Nexmo');

var nexmo = new Nexmo({
apiKey: config.API_KEY,
apiSecret: config.API_SECRET
}, {
debug: config.DEBUG,
apiHost: config.API_HOST
});

nexmo.applications.create({
name: 'My nexmo-node Example V2 App',
capabilities: {
voice: {
webhooks: {
answer_url: {
address: "https://example.com",
http_method: "GET"
},
event_url: {
address: "https://example.com",
http_method: "POST"
}
}
},
messages: {
webhooks: {
inbound_url: {
address: "https://example.com",
http_method: "POST"
},
status_url: {
address: "https://example.com",
http_method: "POST"
}
}
},
rtc: {
webhooks: {
event_url: {
address: "https://example.com",
http_method: "POST"
}
}
}
}
}, callback);
};
14 changes: 14 additions & 0 deletions examples/ex-change-rest-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = function(callback, config) {

var Nexmo = require('../lib/Nexmo');

var nexmo = new Nexmo({
apiKey: config.API_KEY,
apiSecret: config.API_SECRET
}, {
debug: config.DEBUG,
restHost: config.REST_HOST
});

nexmo.message.sendSms(config.FROM_NUMBER, config.TO_NUMBER, 'testing', callback);
};
2 changes: 2 additions & 0 deletions examples/run-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ function runExample(exampleFile, callback) {
// By default all examples are run.
// Use this array to run a select number of examples.
exampleFiles = [
// 'ex-change-api-host.js',
// 'ex-change-rest-host.js',
// 'ex-check-balance.js',
// 'ex-create-update-delete-app.js',
// 'ex-dtmf-to-call.js',
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nexmo",
"author": "nexmo",
"version": "2.5.3",
"version": "2.6.0",
"main": "lib/Nexmo",
"types": "./typings/index.d.ts",
"keywords": [
Expand Down
8 changes: 4 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class App {
const authorization = `${this.creds.apiKey}:${this.creds.apiSecret}`;

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: App.PATH,
method: "POST",
body: params,
Expand Down Expand Up @@ -172,7 +172,7 @@ class App {
}

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: Utils.createPathWithQuery(App.PATH, params),
method: "GET",
body: undefined,
Expand Down Expand Up @@ -210,7 +210,7 @@ class App {
const authorization = `${this.creds.apiKey}:${this.creds.apiSecret}`;

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: `${App.PATH}/${appId}`,
method: "PUT",
body: params,
Expand All @@ -236,7 +236,7 @@ class App {
const authorization = `${this.creds.apiKey}:${this.creds.apiSecret}`;

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: `${App.PATH}/${appId}`,
method: "DELETE",
body: "{}",
Expand Down
6 changes: 3 additions & 3 deletions src/CallsResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CallsResource {
params = JSON.stringify(params);

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: CallsResource.PATH,
method: "POST",
body: params,
Expand Down Expand Up @@ -87,7 +87,7 @@ class CallsResource {
}

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: `${CallsResource.PATH}${pathExt}`,
method: "GET",
headers: {
Expand All @@ -109,7 +109,7 @@ class CallsResource {
params = JSON.stringify(params);

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: `${CallsResource.PATH}/${callId}`,
method: "PUT",
body: params,
Expand Down
2 changes: 1 addition & 1 deletion src/DtmfResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DtmfResource {
params = JSON.stringify(params);

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: DtmfResource.PATH.replace("{call_uuid}", callId),
method: "PUT",
body: params,
Expand Down
2 changes: 1 addition & 1 deletion src/FilesResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FilesResource {
fileIdOrUrl = fileIdOrUrl.split("/").pop(-1);

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: `${FilesResource.PATH}/${fileIdOrUrl}`,
method: "GET",
headers: {
Expand Down
15 changes: 12 additions & 3 deletions src/Nexmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,26 @@ class Nexmo {

// This is legacy, everything should use rest or api going forward
this.options.httpClient = new HttpClient(
Object.assign({ host: "rest.nexmo.com" }, this.options),
Object.assign(
{ host: this.options.restHost || "rest.nexmo.com" },
this.options
),
this.credentials
);

// We have two different hosts, so we use two different HttpClients
this.options.api = new HttpClient(
Object.assign({ host: "api.nexmo.com" }, this.options),
Object.assign(
{ host: this.options.apiHost || "api.nexmo.com" },
this.options
),
this.credentials
);
this.options.rest = new HttpClient(
Object.assign({ host: "rest.nexmo.com" }, this.options),
Object.assign(
{ host: this.options.restHost || "rest.nexmo.com" },
this.options
),
this.credentials
);

Expand Down
4 changes: 2 additions & 2 deletions src/StreamResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class StreamResource {
params = JSON.stringify(params);

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: StreamResource.PATH.replace("{call_uuid}", callId),
method: "PUT",
body: params,
Expand All @@ -51,7 +51,7 @@ class StreamResource {
*/
stop(callId, callback) {
var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: StreamResource.PATH.replace("{call_uuid}", callId),
method: "DELETE",
headers: {
Expand Down
4 changes: 2 additions & 2 deletions src/TalkResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TalkResource {
params = JSON.stringify(params);

var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: TalkResource.PATH.replace("{call_uuid}", callId),
method: "PUT",
body: params,
Expand All @@ -51,7 +51,7 @@ class TalkResource {
*/
stop(callId, callback) {
var config = {
host: "api.nexmo.com",
host: this.options.apiHost || "api.nexmo.com",
path: TalkResource.PATH.replace("{call_uuid}", callId),
method: "DELETE",
headers: {
Expand Down
91 changes: 64 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@

var querystring = require("querystring");

var msgpath = { host: "rest.nexmo.com", path: "/sms/json" };
var shortcodePath = { host: "rest.nexmo.com", path: "/sc/us/${type}/json" };
var ttsEndpoint = { host: "api.nexmo.com", path: "/tts/json" };
var ttsPromptEndpoint = { host: "api.nexmo.com", path: "/tts-prompt/json" };
var callEndpoint = { host: "rest.nexmo.com", path: "/call/json" };
var verifyEndpoint = { host: "api.nexmo.com", path: "/verify/json" };
var checkVerifyEndpoint = { host: "api.nexmo.com", path: "/verify/check/json" };
var msgpath = {
host: "rest.nexmo.com",
path: "/sms/json"
};
var shortcodePath = {
host: "rest.nexmo.com",
path: "/sc/us/${type}/json"
};
var ttsEndpoint = {
host: "api.nexmo.com",
path: "/tts/json"
};
var ttsPromptEndpoint = {
host: "api.nexmo.com",
path: "/tts-prompt/json"
};
var callEndpoint = {
host: "rest.nexmo.com",
path: "/call/json"
};
var verifyEndpoint = {
host: "api.nexmo.com",
path: "/verify/json"
};
var checkVerifyEndpoint = {
host: "api.nexmo.com",
path: "/verify/check/json"
};
var controlVerifyEndpoint = {
host: "api.nexmo.com",
path: "/verify/control/json"
Expand All @@ -17,10 +38,22 @@ var searchVerifyEndpoint = {
host: "api.nexmo.com",
path: "/verify/search/json"
};
var niEndpoint = { host: "api.nexmo.com", path: "/ni/advanced/async/json" };
var niBasicEndpoint = { host: "api.nexmo.com", path: "/ni/basic/json" };
var niStandardEndpoint = { host: "api.nexmo.com", path: "/ni/standard/json" };
var niAdvancedEndpoint = { host: "api.nexmo.com", path: "/ni/advanced/json" };
var niEndpoint = {
host: "api.nexmo.com",
path: "/ni/advanced/async/json"
};
var niBasicEndpoint = {
host: "api.nexmo.com",
path: "/ni/basic/json"
};
var niStandardEndpoint = {
host: "api.nexmo.com",
path: "/ni/standard/json"
};
var niAdvancedEndpoint = {
host: "api.nexmo.com",
path: "/ni/advanced/json"
};
var up = {};
var numberPattern = new RegExp("^[0-9 +()-]*$");

Expand Down Expand Up @@ -59,7 +92,7 @@ var ERROR_MESSAGES = {
product: "Invalid product. Should be one of [voice, sms]"
};

exports.initialize = function(pkey, psecret, options) {
exports.initialize = function(pkey, psecret, options, hosts) {
if (!pkey || !psecret) {
throw "key and secret cannot be empty, set valid values";
}
Expand All @@ -68,6 +101,25 @@ exports.initialize = function(pkey, psecret, options) {
api_secret: psecret
};
_options = options;

if (_options.restHost) {
msgpath.host = _options.restHost;
shortcodePath.host = _options.restHost;
callEndpoint.host = _options.restHost;
}

if (_options.apiHost) {
ttsEndpoint.host = _options.apiHost;
ttsPromptEndpoint.host = _options.apiHost;
verifyEndpoint.host = _options.apiHost;
checkVerifyEndpoint.host = _options.apiHost;
controlVerifyEndpoint.host = _options.apiHost;
searchVerifyEndpoint.host = _options.apiHost;
niEndpoint.host = _options.apiHost;
niBasicEndpoint.host = _options.apiHost;
niStandardEndpoint.host = _options.apiHost;
niAdvancedEndpoint.host = _options.apiHost;
}
};

exports.sendBinaryMessage = function(sender, recipient, body, udh, callback) {
Expand Down Expand Up @@ -577,18 +629,3 @@ function sendError(callback, err, returnData) {
throw err;
}
}

exports.setHost = function(aHost) {
msgpath.host = aHost;
shortcodePath.host = aHost;
ttsEndpoint.host = aHost;
ttsPromptEndpoint.host = aHost;
callEndpoint.host = aHost;
verifyEndpoint.host = aHost;
checkVerifyEndpoint.host = aHost;
controlVerifyEndpoint.host = aHost;
searchVerifyEndpoint.host = aHost;
niEndpoint.host = aHost;
niBasicEndpoint.host = aHost;
niStandardEndpoint.host = aHost;
};
Loading

0 comments on commit bd19e4b

Please sign in to comment.