Skip to content

Commit

Permalink
Merge pull request #277 from plivo/SMS-5025
Browse files Browse the repository at this point in the history
SMS-5025: Add Requester IP to Get and List Messages
  • Loading branch information
renoldthomas-plivo authored Jan 30, 2023
2 parents b17833f + 1b6cda1 commit ec91115
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ docs/
node_modules
coverage
dist
node-sdk-test/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Change Log

## [4.39.0](https://github.com/plivo/plivo-node/tree/v4.39.0) (2022-01-25)
**Feature - Added New Param(requesterIP) in Get Message and List Mssage APIs**
- Add `requesterIP` to the response for the [list all messages API](https://www.plivo.com/docs/sms/api/message/list-all-messages/) and the [get message details API](https://www.plivo.com/docs/sms/api/message#retrieve-a-message)

## [v4.38.0](https://github.com/plivo/plivo-node/tree/v4.38.0) (2023-01-18)
**Feature - Added New Param(Message Expiry) in Send Message API**
- Added new param(Message Expiry) in Send Message API
Expand Down
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM ubuntu:18.04

WORKDIR /usr/src/app
RUN apt-get update && apt-get install -y wget git vim

# Install node using nvm
RUN mkdir -p /usr/src/.nvm
ENV NVM_DIR /usr/src/.nvm
RUN wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | sh
ENV NODE_VERSION v14.0.0
RUN /bin/sh -c ". $NVM_DIR/nvm.sh && nvm install $NODE_VERSION"

ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/$NODE_VERSION/bin:$PATH

# Copy setup script
COPY setup_sdk.sh /usr/src/app/
RUN chmod a+x /usr/src/app/setup_sdk.sh

ENTRYPOINT [ "/usr/src/app/setup_sdk.sh" ]
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: build test

build:
docker-compose up --build --remove-orphans

test:
docker exec -it $$CONTAINER /bin/bash -c "npm install request --no-save && npm test"
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,23 @@ More examples are available [here](https://github.com/plivo/plivo-examples-node)

## Reporting issues
Report any feedback or problems with this version by [opening an issue on Github](https://github.com/plivo/plivo-node/issues).

## Local Development
> Note: Requires latest versions of Docker & Docker-Compose. If you're on MacOS, ensure Docker Desktop is running.
1. Export the following environment variables in your host machine:
```bash
export PLIVO_AUTH_ID=<your_auth_id>
export PLIVO_AUTH_TOKEN=<your_auth_token>
export PLIVO_API_DEV_HOST=<plivoapi_dev_endpoint>
export PLIVO_API_PROD_HOST=<plivoapi_public_endpoint>
```
2. Run `make build`. This will create a docker container in which the sdk will be setup and dependencies will be installed.
> The entrypoint of the docker container will be the `setup_sdk.sh` script. The script will handle all the necessary changes required for local development. It will also package the sdk and reinstall it as a dependecy for the test program.
3. The above command will print the docker container id (and instructions to connect to it) to stdout.
4. The testing code can be added to `<sdk_dir_path>/node-sdk-test/test.js` in host
(or `/usr/src/app/node-sdk-test/test.js` in container)
5. The sdk directory will be mounted as a volume in the container. So any changes in the sdk code will also be reflected inside the container. However, when any change is made, the dependencies for the test program need to be re-installed. To do that:
* Either restart the docker container
* Or Run the `setup_sdk.sh` script
6. To run unit tests, run `make test CONTAINER=<cont_id>` in host, where `<cont_id>` is the docker container id created in 2.
(The docker container should be running)
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3'

services:

nodeSDK:
build:
context: .
image: nodesdk
container_name: nodeSDK
environment:
- PLIVO_AUTH_ID=${PLIVO_AUTH_ID}
- PLIVO_AUTH_TOKEN=${PLIVO_AUTH_TOKEN}
- PLIVO_API_DEV_HOST=${PLIVO_API_DEV_HOST}
- PLIVO_API_PROD_HOST=${PLIVO_API_PROD_HOST}
volumes:
- .:/usr/src/app
stdin_open: true
tty: true
4 changes: 3 additions & 1 deletion lib/resources/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class MessageGetResponse {
this.totalRate = params.totalRate;
this.units = params.units;
this.powerpackID = params.powerpackId;
this.requesterIP = params.requesterIp;
}
}

Expand All @@ -64,6 +65,7 @@ export class MessageListResponse {
this.totalRate = params.totalRate;
this.units = params.units;
this.powerpackID = params.powerpackId;
this.requesterIP = params.requesterIp;
}
}

Expand Down Expand Up @@ -193,8 +195,8 @@ export class MessageInterface extends PlivoResourceInterface {
var media_ids = src.media_ids;
var log = src.log;
var trackable = src.trackable;
var messageExpiry = src.messageExpiry;
var src = src.src;
var messageExpiry = src.messageExpiry
}

let errors = validate([{
Expand Down
12 changes: 8 additions & 4 deletions lib/rest/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1695,7 +1695,8 @@ export function Request(config) {
to_number: '19352326448',
total_amount: '0.00000',
total_rate: '0.00350',
units: 1
units: 1,
requester_ip: "192.168.1.1"
}
});
}
Expand Down Expand Up @@ -1936,7 +1937,8 @@ export function Request(config) {
to_number: '19352326448',
total_amount: '0.00000',
total_rate: '0.00350',
units: 1
units: 1,
requester_ip: '192.168.1.2'
}
});
}
Expand Down Expand Up @@ -1964,7 +1966,8 @@ export function Request(config) {
to_number: '19352326448',
total_amount: '0.00000',
total_rate: '0.00350',
units: 1
units: 1,
requester_ip: "192.168.1.1"
},
{
error_code: '200',
Expand All @@ -1978,7 +1981,8 @@ export function Request(config) {
to_number: '19352326448',
total_amount: '0.00000',
total_rate: '0.00350',
units: 1
units: 1,
requester_ip: "192.168.1.2"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plivo",
"version": "4.38.0",
"version": "4.39.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": [
Expand Down
51 changes: 51 additions & 0 deletions setup_sdk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

set -e
testDir="node-sdk-test"
GREEN="\033[0;32m"
NC="\033[0m"

if [ ! $PLIVO_API_PROD_HOST ] || [ ! $PLIVO_API_DEV_HOST ] || [ ! $PLIVO_AUTH_ID ] || [ ! $PLIVO_AUTH_TOKEN ]; then
echo "Environment variables not properly set! Please refer to Local Development section in README!"
exit 126
fi

cd /usr/src/app

echo "Setting plivo-api endpoint to dev..."
find /usr/src/app/lib/rest -type f -exec sed -i "s/$PLIVO_API_PROD_HOST/$PLIVO_API_DEV_HOST/g" {} \;

echo "Packaging SDK..."
echo "npm install" && npm install
echo "npm run prepublish" && npm run prepublish
echo "npm pack | tail -n 1" && package=$( npm pack | tail -n 1 )

if [ ! -d $testDir ]; then
echo "Creating test dir..."
mkdir -p $testDir
fi

if [ ! -f $testDir/test.js ]; then
echo "Creating test file..."
echo -e "let plivo = require('plivo')\n" > $testDir/test.js
echo -e "\nlet client = new plivo.Client(process.env.PLIVO_AUTH_ID, process.env.PLIVO_AUTH_TOKEN);" >> $testDir/test.js
fi

echo "Installing dependencies for testing..."
mv $package $testDir
cd $testDir
rm -rf package*.json node_modules
npm init -y
npm install $package
rm $package

echo -e "\n\nSDK setup complete!"
echo "To test your changes:"
echo -e "\t1. Add your test code in <path_to_cloned_sdk>/$testDir/test.js on host (or /usr/src/app/$testDir/test.js in the container)"
echo -e "\t2. Run a terminal in the container using: $GREEN docker exec -it $HOSTNAME /bin/bash$NC"
echo -e "\t3. Navigate to the test directory: $GREEN cd /usr/src/app/$testDir$NC"
echo -e "\t4. Run your test file: $GREEN node test.js$NC"
echo -e "\t5. For running unit tests, run on host: $GREEN make test CONTAINER=$HOSTNAME$NC"

# To keep the container running post setup
/bin/bash
15 changes: 15 additions & 0 deletions test/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,28 @@ describe('message', function () {
})
});

it('should get requester_ip', function () {
return client.messages.get(1)
.then(function (message) {
assert.equal(message.requesterIP, "192.168.1.2")
})
});

it('list messages', function () {
return client.messages.list()
.then(function (messages) {
assert.equal(messages.length, 2)
})
});

it('should have requester_ip in listed messages', function () {
return client.messages.list()
.then(function (messages) {
assert.equal(messages[0].requesterIP, "192.168.1.1")
assert.equal(messages[1].requesterIP, "192.168.1.2")
})
});

it('should create message via interface', function () {
return client.messages.create({src:'src', dst:'dst', text:'text',powerpackUUID: null})
.then(function (message) {
Expand Down
6 changes: 4 additions & 2 deletions types/resources/messages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export class MessageGetResponse {
totalRate: string;
units: string;
powerpackId: string;
requesterIp: string;
}
export class MessageListResponse {
constructor(params: object);
errorCode: string;
errorCode: string;
fromNumber: string;
messageDirection: string;
messageState: string;
Expand All @@ -35,8 +36,9 @@ export class MessageListResponse {
toNumber: string;
totalAmount: string;
totalRate: string;
units: string;
units: string;
powerpackId: string;
requesterIp: string;
}
export class MMSMediaResponse {
constructor(params: object);
Expand Down

0 comments on commit ec91115

Please sign in to comment.