Skip to content
This repository has been archived by the owner on Nov 28, 2018. It is now read-only.

Commit

Permalink
Add Content-Length header and default use https protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
calvertyang committed May 5, 2014
1 parent 303f24b commit b05fa62
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 71 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## Changelog
#### 1.0.6
_2014-05-05_
* Change default protocol to https
* Add `Content-Length` header

#### 1.0.5
_2014-04-21_
* Changed back to JavaScript for easy maintenance
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,5 @@ Refer [offical docuemnts](https://docs.nexmo.com/) to get the schema for the ret
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/2751da50234951068e9184e6ab22730a "githalytics.com")](http://githalytics.com/CalvertYang/simple-nexmo)
165 changes: 95 additions & 70 deletions lib/nexmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @constant
*/
var _VERSION = '1.0.5';
var _VERSION = '1.0.6';

/**
* API http request header
Expand Down Expand Up @@ -98,7 +98,7 @@
*/
var _apiKey = '';
var _apiSecret = '';
var _useHttps = false;
var _useHttps = true;
var _debugMode = false;
var _initialized = false;

Expand All @@ -117,7 +117,7 @@

_apiKey = key;
_apiSecret = secret;
_useHttps = protocol === 'https';
_useHttps = !(protocol === 'http');
_debugMode = debug;
_initialized = true;
};
Expand Down Expand Up @@ -214,9 +214,7 @@
* @param {requestCallback} callback - The callback that handles the response
*/
var getBalance = function getBalance (callback) {
var endpoint = getPath(_ENDPOINT.accountGetBalance);

sendRequest(endpoint, callback);
sendRequest(_ENDPOINT.accountGetBalance, null, callback);
};

/**
Expand All @@ -229,9 +227,11 @@
if (!countryCode || countryCode.length !== 2) {
sendErrorResponse(callback, new Error(_ERROR_MESSAGES.invalidCountryCode));
} else {
var endpoint = getPath(_ENDPOINT.accountPricing) + '&country=' + countryCode;
var data = {
country: countryCode
};

sendRequest(endpoint, callback);
sendRequest(_ENDPOINT.accountPricing, data, callback);
}
};

Expand All @@ -245,9 +245,11 @@
if (!newSecret || newSecret.length > 8) {
sendError(callback, new Error(_ERROR_MESSAGES.invalidNewSecret));
} else {
var endpoint = getPath(_ENDPOINT.accountSettings) + '&newSecret=' + encodeURIComponent(newSecret);
var data = {
newSecret: encodeURIComponent(newSecret)
};

sendRequest(endpoint, 'POST', callback);
sendRequest(_ENDPOINT.accountSettings, data, 'POST', callback);
}
};

Expand All @@ -261,9 +263,11 @@
if (!newUrl) {
sendError(callback, new Error(_ERROR_MESSAGES.invalidCallbackUrl));
} else {
var endpoint = getPath(_ENDPOINT.accountSettings) + '&moCallBackUrl=' + encodeURIComponent(newUrl);
var data = {
moCallBackUrl: encodeURIComponent(newUrl)
};

sendRequest(endpoint, 'POST', callback);
sendRequest(_ENDPOINT.accountSettings, data, 'POST', callback);
}
};

Expand All @@ -277,9 +281,11 @@
if (!newUrl) {
sendError(callback, new Error(_ERROR_MESSAGES.invalidCallbackUrl));
} else {
var endpoint = getPath(_ENDPOINT.accountSettings) + '&drCallBackUrl=' + encodeURIComponent(newUrl);
var data = {
drCallBackUrl: encodeURIComponent(newUrl)
};

sendRequest(endpoint, 'POST', callback);
sendRequest(_ENDPOINT.accountSettings, data, 'POST', callback);
}
};

Expand All @@ -293,9 +299,11 @@
if (!transactionId) {
sendError(callback, new Error(_ERROR_MESSAGES.invalidTransactionId));
} else {
var endpoint = getPath(_ENDPOINT.accountTopUp) + '&trx=' + transactionId;
var data = {
trx: transactionId
};

sendRequest(endpoint, 'POST', callback);
sendRequest(_ENDPOINT.accountTopUp, data, 'POST', callback);
}
};

Expand All @@ -305,9 +313,7 @@
* @param {requestCallback} callback - The callback that handles the response
*/
var getNumbers = function getNumbers (callback) {
var endpoint = getPath(_ENDPOINT.accountNumbers);

sendRequest(endpoint, callback);
sendRequest(_ENDPOINT.accountNumbers, null, callback);
};

/**
Expand All @@ -323,24 +329,27 @@
if (!countryCode || countryCode.length !== 2) {
sendErrorResponse(callback, new Error(_ERROR_MESSAGES.invalidCountryCode));
} else {
var endpoint = getPath(_ENDPOINT.numberSearch) + '&country=' + countryCode;
var data = {
country: countryCode
};

if (typeof pattern === 'function') {
callback = pattern;
} else {
endpoint += '&pattern=' + pattern;
data['pattern'] = pattern;
if (typeof index === 'function') {
callback = index;
} else {
endpoint += '&index=' + index;
data['index'] = index;
if (typeof size === 'function') {
callback = size;
} else {
endpoint += '&size=' + size;
data['size'] = size;
}
}
}

sendRequest(endpoint, callback);
sendRequest(_ENDPOINT.numberSearch, data, callback);
}
};

Expand All @@ -357,9 +366,12 @@
} else if (!msisdn || msisdn.length < 10) {
sendErrorResponse(callback, new Error(_ERROR_MESSAGES.invalidMsisdn));
} else {
var endpoint = getPath(_ENDPOINT.numberBuy) + '&country=' + countryCode + '&msisdn=' + msisdn;
var data = {
country: countryCode,
msisdn: msisdn
};

sendRequest(endpoint, 'POST', callback);
sendRequest(_ENDPOINT.numberBuy, data, 'POST', callback);
}
};

Expand All @@ -376,9 +388,12 @@
} else if (!msisdn || msisdn.length < 10) {
sendErrorResponse(callback, new Error(_ERROR_MESSAGES.invalidMsisdn));
} else {
var endpoint = getPath(_ENDPOINT.numberCancel) + '&country=' + countryCode + '&msisdn=' + msisdn;
var data = {
country: countryCode,
msisdn: msisdn
};

sendRequest(endpoint, 'POST', callback);
sendRequest(_ENDPOINT.numberCancel, data, 'POST', callback);
}
};

Expand All @@ -405,9 +420,11 @@
if (!messageId) {
sendErrorResponse(callback, new Error(_ERROR_MESSAGES.invalidMessageId));
} else {
var endpoint = getPath(_ENDPOINT.searchMessage) + '&id=' + messageId;
var data = {
id: messageId
};

sendRequest(endpoint, callback);
sendRequest(_ENDPOINT.searchMessage, data, callback);
}
};

Expand All @@ -424,17 +441,13 @@
if (messageIds.length > 10) {
sendErrorResponse(callback, new Error(_ERROR_MESSAGES.tooManyMessageId));
} else {
var id;
var endpoint = getPath(_ENDPOINT.searchMessages) + '&';
var data = { ids: [] };

for (var idx = 0, len = messageIds.length; idx < len; idx++) {
id = messageIds[idx];
endpoint += 'ids=' + id;
if (idx < len - 1) {
endpoint += '&';
}
data.ids.push(messageIds[idx]);
}

sendRequest(endpoint, callback);
sendRequest(_ENDPOINT.searchMessages, data, callback);
}
}
};
Expand All @@ -452,9 +465,12 @@
} else if (!to) {
sendErrorResponse(callback, new Error(_ERROR_MESSAGES.invalidRecipient));
} else {
var endpoint = getPath(_ENDPOINT.searchMessages) + '&date=' + date + '&to=' + to;
var data = {
date: date,
to: to
};

sendRequest(endpoint, callback);
sendRequest(_ENDPOINT.searchMessages, data, callback);
}
};

Expand All @@ -469,15 +485,17 @@
if (!date) {
sendErrorResponse(callback, new Error(_ERROR_MESSAGES.invalidDate));
} else {
var endpoint = getPath(_ENDPOINT.searchRejections) + '&date=' + date;
var data = {
date: date
};

if (typeof to === 'function') {
callback = to;
} else if (to) {
endpoint += '&to=' + to;
data.to = to;
}

sendRequest(endpoint, callback);
sendRequest(_ENDPOINT.searchRejections, data, callback);
}
};

Expand All @@ -494,10 +512,9 @@
} else if (!data.to) {
sendErrorResponse(callback, new Error(_ERROR_MESSAGES.invalidRecipient));
} else {
var endpoint = getPath(_ENDPOINT.sms) + '&' + querystring.stringify(data);
log('Sending message from ' + data.from + ' to ' + data.to + ' with message ' + data.text);

sendRequest(endpoint, 'POST', function(err, apiResponse) {
sendRequest(_ENDPOINT.sms, data, 'POST', function(err, apiResponse) {
if (!err && apiResponse.status && apiResponse.messages[0].status > 0) {
sendErrorResponse(callback, new Error(apiResponse.messages[0]['error-text'], apiResponse));
} else {
Expand All @@ -512,37 +529,60 @@
/**
* Send HTTP/HTTPS request to nexmo
*
* @param {string} path - API endpoint
* @param {string} endpoint - API endpoint
* @param {string} data - Stringify data
* @param {string} method - HTTP method
* @param {requestCallback} callback - The callback that handles the response
* @private
*/
var sendRequest = function sendRequest (path, method, callback) {
var sendRequest = function sendRequest (endpoint, data, method, callback) {
if (!_initialized) {
throw _ERROR_MESSAGES.initializeRequired;
}

var credentials = {
api_key: _apiKey,
api_secret: _apiSecret
};

var dataString = querystring.stringify(credentials);

if (data) {
dataString += '&' + querystring.stringify(data);
}

if (typeof method === 'function') {
callback = method;
method = 'GET';
}

// Setting Content-Length header when using POST action
if (method === 'POST') {
_HEADERS['Content-Length'] = dataString.length;
} else {
endpoint += '?' + dataString;
}

var options = {
host: _BASE_URL,
port: 80,
path: path,
port: 443,
path: endpoint,
method: method,
headers: _HEADERS
};

log(options);

var request;
if (_useHttps) {
options.port = 443;
request = https.request(options);
} else {
if (!_useHttps) {
options.port = 80;
request = http.request(options);
} else {
request = https.request(options);
}

log(options);

if (method === 'POST') {
request.write(dataString);
}

request.end();
Expand Down Expand Up @@ -590,21 +630,6 @@
});
};

/**
* Get API endpoint path with credentials
*
* @param {string} action - endpoint path
* @private
*/
var getPath = function getPath (action) {
var credentials = {
api_key: _apiKey,
api_secret: _apiSecret
};

return action + '?' + querystring.stringify(credentials);
};

/**
* Send back or throw out error response
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "simple-nexmo",
"author": "Calvert Yang <calvert@wavinfo.com>",
"version": "1.0.5",
"version": "1.0.6",
"description": "A nodejs wrapper for nexmo API to send SMS",
"contributors": [
"Calvert Yang <calvert@wavinfo.com>"
Expand Down

0 comments on commit b05fa62

Please sign in to comment.