From 1fd103a16ad2983ec7be7beb0fa9ba8b0586dd8b Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Sun, 25 Apr 2021 10:11:30 +0530 Subject: [PATCH 1/9] removed string error response --- lib/rest/request.js | 2 -- package.json | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/rest/request.js b/lib/rest/request.js index 94f1c72..336d18f 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -199,8 +199,6 @@ export function Request(config) { if (!_.inRange(response.statusCode, 200, 300)) { body = body || response.body; if (typeof body === 'object') { - reject(new exceptionClass(JSON.stringify(body))); - } else { reject(new exceptionClass(body)); } } diff --git a/package.json b/package.json index 7df55cd..c7458e5 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ }, "dependencies": { "@types/node": "^14.14.14", - "axios": "^0.19.2", + "axios": "^0.21.1", "base-64": "^0.1.0", "build-url": "^1.0.10", "form-data": "^4.0.0", From 8f765c90fc1161b2cad2fa7d04c035d869e7d128 Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Sun, 25 Apr 2021 20:41:13 +0530 Subject: [PATCH 2/9] returning object --- lib/rest/axios.js | 13 +++++++++++++ lib/rest/request.js | 2 ++ 2 files changed, 15 insertions(+) diff --git a/lib/rest/axios.js b/lib/rest/axios.js index d323f07..78348d5 100644 --- a/lib/rest/axios.js +++ b/lib/rest/axios.js @@ -207,6 +207,19 @@ export function Axios(config) { } }) .catch(function (error) { + const exceptionClass = { + 400: Exceptions.InvalidRequestError, + 401: Exceptions.AuthenticationError, + 404: Exceptions.ResourceNotFoundError, + 405: Exceptions.InvalidRequestError, + 500: Exceptions.ServerError, + } [error.response.status] || Error; + if (!_.inRange(error.response.status, 200, 300)) { + let body = error.response.data; + if (typeof body === 'object') { + reject(new exceptionClass(body)); + } + } reject(error.stack + '\n' + JSON.stringify(error.response.data)); }) } diff --git a/lib/rest/request.js b/lib/rest/request.js index 336d18f..94f1c72 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -199,6 +199,8 @@ export function Request(config) { if (!_.inRange(response.statusCode, 200, 300)) { body = body || response.body; if (typeof body === 'object') { + reject(new exceptionClass(JSON.stringify(body))); + } else { reject(new exceptionClass(body)); } } From 2a18964e8d4d458cd7c749729a42b1c9ba47f8ce Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Mon, 26 Apr 2021 08:40:46 +0530 Subject: [PATCH 3/9] handling error --- lib/rest/axios.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/rest/axios.js b/lib/rest/axios.js index 78348d5..c6c40ba 100644 --- a/lib/rest/axios.js +++ b/lib/rest/axios.js @@ -217,7 +217,11 @@ export function Axios(config) { if (!_.inRange(error.response.status, 200, 300)) { let body = error.response.data; if (typeof body === 'object') { - reject(new exceptionClass(body)); + var result = {}; + result['message'] = body.error + result['stack'] = error.stack + result['name'] = error.name + reject(new exceptionClass(result)); } } reject(error.stack + '\n' + JSON.stringify(error.response.data)); From a883815d92dc5773ba6da6fb68d591b13019a519 Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Tue, 27 Apr 2021 12:19:16 +0530 Subject: [PATCH 4/9] exception handling --- lib/rest/axios.js | 6 +----- lib/utils/exceptions.js | 2 +- lib/utils/restException.js | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 lib/utils/restException.js diff --git a/lib/rest/axios.js b/lib/rest/axios.js index c6c40ba..a2173d1 100644 --- a/lib/rest/axios.js +++ b/lib/rest/axios.js @@ -217,11 +217,7 @@ export function Axios(config) { if (!_.inRange(error.response.status, 200, 300)) { let body = error.response.data; if (typeof body === 'object') { - var result = {}; - result['message'] = body.error - result['stack'] = error.stack - result['name'] = error.name - reject(new exceptionClass(result)); + reject(new exceptionClass(error)); } } reject(error.stack + '\n' + JSON.stringify(error.response.data)); diff --git a/lib/utils/exceptions.js b/lib/utils/exceptions.js index 04805f2..9d72ae0 100644 --- a/lib/utils/exceptions.js +++ b/lib/utils/exceptions.js @@ -1,4 +1,4 @@ -export class PlivoRestError extends Error { } +var PlivoRestError = require('./restException'); export class ResourceNotFoundError extends PlivoRestError { } export class ServerError extends PlivoRestError { } export class InvalidRequestError extends PlivoRestError { } diff --git a/lib/utils/restException.js b/lib/utils/restException.js new file mode 100644 index 0000000..e0c7f28 --- /dev/null +++ b/lib/utils/restException.js @@ -0,0 +1,15 @@ + +class PlivoRestError extends Error { + constructor(response) { + response = response.response + super('[HTTP ' + response.status + '] Failed to execute request'); + const body = typeof response.data === 'string' ? JSON.parse(response.data) : response.data; + this.status = response.status; + this.statusText = response.statusText + this.message = body.error; + this.apiID = body.api_id; + this.moreInfo = response.config.data + } +} + +module.exports = PlivoRestError; From dda00548e8e9f8b35316eb5f541fc0260bc89692 Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Tue, 27 Apr 2021 13:27:22 +0530 Subject: [PATCH 5/9] exception handle --- lib/rest/axios.js | 8 +++++++- lib/utils/restException.js | 7 ++++++- package.json | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/rest/axios.js b/lib/rest/axios.js index a2173d1..86505dd 100644 --- a/lib/rest/axios.js +++ b/lib/rest/axios.js @@ -2,6 +2,7 @@ import axios from 'axios'; import queryString from 'querystring'; import * as Exceptions from '../utils/exceptions'; import * as _ from "lodash"; +var HttpsProxyAgent = require('https-proxy-agent'); export function Axios(config) { let auth = 'Basic ' + new Buffer(config.authId + ':' + config.authToken) @@ -137,7 +138,7 @@ export function Axios(config) { } if (typeof config.proxy !== 'undefined') { - options.proxy = config.proxy; + options.httpsAgent = new HttpsProxyAgent(config.proxy); } if (typeof config.timeout !== 'undefined') { @@ -218,6 +219,11 @@ export function Axios(config) { let body = error.response.data; if (typeof body === 'object') { reject(new exceptionClass(error)); + } else { + if (error.response.status >= 500) { + reject(new Exceptions.ServerError(error)); + } + reject(new exceptionClass(error)); } } reject(error.stack + '\n' + JSON.stringify(error.response.data)); diff --git a/lib/utils/restException.js b/lib/utils/restException.js index e0c7f28..d41e200 100644 --- a/lib/utils/restException.js +++ b/lib/utils/restException.js @@ -2,8 +2,13 @@ class PlivoRestError extends Error { constructor(response) { response = response.response + var body; super('[HTTP ' + response.status + '] Failed to execute request'); - const body = typeof response.data === 'string' ? JSON.parse(response.data) : response.data; + try { + body = typeof response.data === 'string' ? JSON.parse(response.data) : response.data; + } catch (err) { + body = {"error": response.data, "api_id": ''} + } this.status = response.status; this.statusText = response.statusText this.message = body.error; diff --git a/package.json b/package.json index c7458e5..dbf571e 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "@types/node": "^14.14.14", "axios": "^0.21.1", "base-64": "^0.1.0", + "https-proxy-agent": "^5.0.0", "build-url": "^1.0.10", "form-data": "^4.0.0", "jsonwebtoken": "^8.5.1", From 925f5e0009c1d374c2026a5f5a7bb6973300da81 Mon Sep 17 00:00:00 2001 From: huzaif Date: Tue, 4 May 2021 17:31:58 +0530 Subject: [PATCH 6/9] add exception for voice --- lib/rest/axios.js | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/rest/axios.js b/lib/rest/axios.js index 86505dd..61f8cde 100644 --- a/lib/rest/axios.js +++ b/lib/rest/axios.js @@ -1,7 +1,9 @@ -import axios from 'axios'; -import queryString from 'querystring'; import * as Exceptions from '../utils/exceptions'; import * as _ from "lodash"; + +import axios from 'axios'; +import queryString from 'querystring'; + var HttpsProxyAgent = require('https-proxy-agent'); export function Axios(config) { @@ -85,7 +87,25 @@ export function Axios(config) { }); }) .catch(function (error) { - reject(error.stack+ "\r\n" + JSON.stringify(error.response.data)); + const exceptionClass = { + 400: Exceptions.InvalidRequestError, + 401: Exceptions.AuthenticationError, + 404: Exceptions.ResourceNotFoundError, + 405: Exceptions.InvalidRequestError, + 500: Exceptions.ServerError, + } [error.response.status] || Error; + if (!_.inRange(error.response.status, 200, 300)) { + let body = error.response.data; + if (typeof body === 'object') { + reject(new exceptionClass(error)); + } else { + if (error.response.status >= 500) { + reject(new Exceptions.ServerError(error)); + } + reject(new exceptionClass(error)); + } + } + reject(error.stack + '\n' + JSON.stringify(error.response.data)); }); }) } @@ -173,7 +193,25 @@ export function Axios(config) { }); }) .catch(function (error) { - reject(error.stack+ "\r\n" + JSON.stringify(error.response.data)); + const exceptionClass = { + 400: Exceptions.InvalidRequestError, + 401: Exceptions.AuthenticationError, + 404: Exceptions.ResourceNotFoundError, + 405: Exceptions.InvalidRequestError, + 500: Exceptions.ServerError, + } [error.response.status] || Error; + if (!_.inRange(error.response.status, 200, 300)) { + let body = error.response.data; + if (typeof body === 'object') { + reject(new exceptionClass(error)); + } else { + if (error.response.status >= 500) { + reject(new Exceptions.ServerError(error)); + } + reject(new exceptionClass(error)); + } + } + reject(error.stack + '\n' + JSON.stringify(error.response.data)); }) } else { From 5a6983c2bf6d1fd0c834b02b0406840c2c2443f5 Mon Sep 17 00:00:00 2001 From: huzaif Date: Tue, 4 May 2021 17:39:07 +0530 Subject: [PATCH 7/9] update readme --- CHANGELOG.md | 4 ++++ README.md | 56 ++++++++++++++++++++++------------------------------ package.json | 2 +- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c82c33..a7d5567 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## [4.17.0](https://github.com/plivo/plivo-node/releases/tag/v4.17.0)(2021-05-04) +- Add Exception Support +- Updated Readme + ## [4.16.0](https://github.com/plivo/plivo-node/releases/tag/v4.16.0)(2021-04-19) - Added SDK support for Voice MultiPartyCall APIs and XML diff --git a/README.md b/README.md index 6e86df8..2c454a1 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ -# plivo-node -The Node.js SDK makes it simpler to integrate communications into your Node.js applications using the Plivo REST API. Using the SDK, you will be able to make voice calls, send SMS and generate Plivo XML to control your call flows. +# Plivo Node.js library + +[![Version](https://img.shields.io/npm/v/plivo.svg)](https://www.npmjs.org/package/plivo) +[![Build Status](https://api.travis-ci.org/plivo/plivo-node.svg?branch=master)](https://travis-ci.org/github/plivo/plivo-node) + +The Node.js SDK simplifies the integration of communications into your Node.js applications through the Plivo REST API. You will be able to use the SDK to make voice calls, send SMS, and generate Plivo XML to manage your call flows. ## Installation Install the SDK using [npm](https://www.npmjs.com/package/plivo) @@ -15,7 +19,7 @@ For features in beta, use the beta branch: ## Getting started ### Authentication -To make the API requests, you need to create a `Client` and provide it with authentication credentials (which can be found at [https://manage.plivo.com/dashboard/](https://manage.plivo.com/dashboard/)). +To make the API requests, you need to create a `Client` and provide it with authentication credentials (which can be found at [https://console.plivo.com/dashboard/](https://console.plivo.com/dashboard/)). We recommend that you store your credentials in the `PLIVO_AUTH_ID` and the `PLIVO_AUTH_TOKEN` environment variables, so as to avoid the possibility of accidentally committing them to source control. If you do this, you can initialise the client with no arguments and it will automatically fetch them from the environment variables: @@ -27,7 +31,7 @@ Alternatively, you can specifiy the authentication credentials while initializin ```javascript let plivo = require('plivo'); -let client = new plivo.Client('your_auth_id', 'your_auth_token'); +let client = new plivo.Client('', ''); ``` ### The basics @@ -52,13 +56,12 @@ let plivo = require('plivo'); let client = new plivo.Client(); client.messages.create( - 'your_source_number', - 'your_destination_number', + '+14156667778', + '+14156667777', 'Hello, world!' -).then(function(message_created) { - console.log(message_created) +).then(function(response) { + console.log(response) }); - ``` ### Make a call @@ -68,27 +71,23 @@ let plivo = require('plivo'); let client = new plivo.Client(); client.calls.create( - 'your_source_number', - 'your_destination_number', + '+14156667778', + '+14156667777', 'http://answer.url' -).then(function(call_created) { - console.log(call_created) +).then(function(response) { + console.log(response) }); - ``` ### Lookup a number ```javascript let plivo = require('plivo'); -let client = new plivo.Client('AUTH_ID', 'AUTH_TOKEN'); +let client = new plivo.Client('', ''); -client.lookup.get( - "" -).then(function(response) { +client.lookup.get('') +.then(function(response) { console.log(response); -}).catch(function(error) { - console.log(error); }); ``` @@ -114,25 +113,18 @@ This generates the following XML: ### Run a PHLO ```javascript -var plivo = require('../dist/rest/client.js'); +let plivo = require('plivo'); var PhloClient = plivo.PhloClient; - -var authId = 'auth-id'; -var authToken = 'auth-token'; -var phloId = 'PHLO_ID'; var phloClient = phlo = null; -// Run phlo -phloClient = new PhloClient(authId, authToken); -phloClient.phlo(phloId).run().then(function (result) { +phloClient = new PhloClient('', ''); +phloClient.phlo('').run().then(function (result) { console.log('Phlo run result', result); -}).catch(function (err) { -console.error('Phlo run failed', err); }); ``` ### More examples -Refer to the [Plivo API Reference](https://api-reference.plivo.com/latest/node/introduction/overview) for more examples. Also refer to the [guide to setting up dev environment](https://developers.plivo.com/getting-started/setting-up-dev-environment/) on [Plivo Developers Portal](https://developers.plivo.com) to setup an Express server & use it to test out your integration in under 5 minutes. +More examples are available [here](https://github.com/plivo/plivo-examples-node). Also refer to the [guides for configuring the Express server to run various scenarios](https://www.plivo.com/docs/sms/quickstart/node-expressjs/) & use it to test out your integration in under 5 minutes. ## Reporting issues -Report any feedback or problems with this version by [opening an issue on Github](https://github.com/plivo/plivo-node/issues). +Report any feedback or problems with this version by [opening an issue on Github](https://github.com/plivo/plivo-node/issues). \ No newline at end of file diff --git a/package.json b/package.json index dbf571e..683199f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plivo", - "version": "4.16.0", + "version": "4.17.0", "description": "A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML", "homepage": "https://github.com/plivo/plivo-node", "files": [ From 09ad9498768867f8a130ede9f78ef8a53a529233 Mon Sep 17 00:00:00 2001 From: huzaif Date: Tue, 4 May 2021 17:52:20 +0530 Subject: [PATCH 8/9] fix: typo --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7d5567..4d6ecad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,7 @@ ## [4.17.0](https://github.com/plivo/plivo-node/releases/tag/v4.17.0)(2021-05-04) - Add Exception Support -- Updated Readme - +- Updated README ## [4.16.0](https://github.com/plivo/plivo-node/releases/tag/v4.16.0)(2021-04-19) - Added SDK support for Voice MultiPartyCall APIs and XML From 1d20d0df400601b0979e04a182a2634c937a3afb Mon Sep 17 00:00:00 2001 From: huzaif Date: Tue, 4 May 2021 18:09:49 +0530 Subject: [PATCH 9/9] fix: typo --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d6ecad..46be12f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [4.17.0](https://github.com/plivo/plivo-node/releases/tag/v4.17.0)(2021-05-04) - Add Exception Support - Updated README + ## [4.16.0](https://github.com/plivo/plivo-node/releases/tag/v4.16.0)(2021-04-19) - Added SDK support for Voice MultiPartyCall APIs and XML