From 6bed2dccab989e9e84b1b087f2b3bd791b3f245d Mon Sep 17 00:00:00 2001 From: Mohsin Mamoon Hafiz <84492744+mohsin-plivo@users.noreply.github.com> Date: Tue, 31 Jan 2023 11:46:58 +0530 Subject: [PATCH] SMS-5025: Add Requester IP to Get and List Messages (#157) * SMS-5025: Add requester ip to List and Get MDR * Dockerisation setup * Address review comments * Bump version --- .gitignore | 12 +++++-- CHANGELOG.md | 4 +++ Dockerfile | 12 +++++++ Makefile | 7 ++++ README.md | 18 ++++++++++ baseclient.go | 2 +- docker-compose.yml | 18 ++++++++++ fixtures/messageGetResponse.json | 3 +- fixtures/messageListResponse.json | 60 ++++++++++++++++++++----------- messages.go | 1 + messages_test.go | 3 ++ setup_sdk.sh | 60 +++++++++++++++++++++++++++++++ 12 files changed, 175 insertions(+), 25 deletions(-) create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 docker-compose.yml create mode 100755 setup_sdk.sh diff --git a/.gitignore b/.gitignore index 821882a6..99237f13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .idea/ # Binaries for programs and plugins *.exe +*.exe~ *.dll *.so *.dylib @@ -11,6 +12,11 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out -# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 -.glide/ -.DS_Store +# Dependency directories (remove the comment below to include it) +vendor/ + +# Go workspace file +go.work + +# testing files +go-sdk-test/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d5d2870..ad8b8958 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## [7.18.0](https://github.com/plivo/plivo-go/tree/v7.18.0) (2023-01-25) +**Feature - Added New Param 'requester_ip' in Get Message and List Mssage APIs** +- Add `requester_ip` 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) + ## [7.17.1](https://github.com/plivo/plivo-go/tree/v7.17.1) (2023-01-18) **Feature - Adding new param 'message_expiry' in Send Message API** - Added new param 'message_expiry' in Send Message API diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..3507f373 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.17-alpine + +WORKDIR /usr/src/app +RUN apk update && apk add git vim bash gcc musl-dev + +ENV PATH $PATH:/usr/local/go/bin + +# 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" ] diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..85a57cb5 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: build test + +build: + docker-compose up --build --remove-orphans + +test: + docker exec -it $$CONTAINER /bin/bash -c "go test -v -race -cover" \ No newline at end of file diff --git a/README.md b/README.md index 92fa35ec..69e570cb 100644 --- a/README.md +++ b/README.md @@ -220,3 +220,21 @@ func testPhloRunWithParams() { ### More examples Refer to the [Plivo API Reference](https://www.plivo.com/docs/sms/api/overview/) for more examples. + +## 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= +export PLIVO_AUTH_TOKEN= +export PLIVO_API_DEV_HOST= +export PLIVO_API_PROD_HOST= +``` +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. +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 `/go-sdk-test/test.go` in host + (or `/usr/src/app/go-sdk-test/test.go` 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. +6. To run unit tests, run `make test CONTAINER=` in host, where `` is the docker container id created in 2. +(The docker container should be running) \ No newline at end of file diff --git a/baseclient.go b/baseclient.go index 49ef0aaf..21fd7301 100644 --- a/baseclient.go +++ b/baseclient.go @@ -13,7 +13,7 @@ import ( "github.com/google/go-querystring/query" ) -const sdkVersion = "7.17.1" +const sdkVersion = "7.18.0" const lookupBaseUrl = "lookup.plivo.com" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..13b77265 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3' + +services: + + goSDK: + build: + context: . + image: gosdk + container_name: goSDK + 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 \ No newline at end of file diff --git a/fixtures/messageGetResponse.json b/fixtures/messageGetResponse.json index d92a4852..61278014 100644 --- a/fixtures/messageGetResponse.json +++ b/fixtures/messageGetResponse.json @@ -11,5 +11,6 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.1" } diff --git a/fixtures/messageListResponse.json b/fixtures/messageListResponse.json index c6e5a386..26ac981f 100644 --- a/fixtures/messageListResponse.json +++ b/fixtures/messageListResponse.json @@ -19,7 +19,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.1" }, { "error_code": null, @@ -33,7 +34,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.2" }, { "error_code": null, @@ -47,7 +49,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.3" }, { "error_code": null, @@ -61,7 +64,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.4" }, { "error_code": null, @@ -75,7 +79,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.5" }, { "error_code": null, @@ -89,7 +94,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.6" }, { "error_code": null, @@ -103,7 +109,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.7" }, { "error_code": null, @@ -117,7 +124,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.8" }, { "error_code": "000", @@ -131,7 +139,8 @@ "to_number": "919876543210", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.9" }, { "error_code": null, @@ -145,7 +154,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.10" }, { "error_code": "000", @@ -159,7 +169,8 @@ "to_number": "919876543210", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.11" }, { "error_code": null, @@ -173,7 +184,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.12" }, { "error_code": "000", @@ -187,7 +199,8 @@ "to_number": "919876543210", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.13" }, { "error_code": null, @@ -201,7 +214,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.14" }, { "error_code": "000", @@ -215,7 +229,8 @@ "to_number": "919876543210", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.15" }, { "error_code": null, @@ -229,7 +244,8 @@ "to_number": "911231231231", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.16" }, { "error_code": "000", @@ -243,7 +259,8 @@ "to_number": "919876543210", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.17" }, { "error_code": "000", @@ -257,7 +274,8 @@ "to_number": "919876543210", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.18" }, { "error_code": "000", @@ -271,7 +289,8 @@ "to_number": "919876543210", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.19" }, { "error_code": "000", @@ -285,7 +304,8 @@ "to_number": "919876543210", "total_amount": "0.00250", "total_rate": "0.00250", - "units": 1 + "units": 1, + "requester_ip": "192.168.1.20" } ] } diff --git a/messages.go b/messages.go index 327c9852..b8962730 100644 --- a/messages.go +++ b/messages.go @@ -41,6 +41,7 @@ type Message struct { MessageTime string `json:"message_time,omitempty" url:"message_time,omitempty"` ErrorCode string `json:"error_code,omitempty" url:"error_code,omitempty"` PowerpackID string `json:"powerpack_id,omitempty" url:"powerpack_id,omitempty"` + RequesterIP string `json:"requester_ip,omitempty" url:"requester_ip,omitempty"` } // Stores response for ending a message. diff --git a/messages_test.go b/messages_test.go index 7144323f..ee91b54e 100644 --- a/messages_test.go +++ b/messages_test.go @@ -13,6 +13,8 @@ func TestMessageService_List(t *testing.T) { assert.NotNil(resp) assert.Nil(err) assert.NotEmpty(resp.Objects[0].MessageUUID) + assert.Equal(resp.Objects[0].RequesterIP, "192.168.1.1") + assert.Equal(resp.Objects[19].RequesterIP, "192.168.1.20") assert.NotNil(resp.Objects) assert.NotNil(resp.Meta) cl := client.httpClient @@ -32,6 +34,7 @@ func TestMessageService_Get(t *testing.T) { assert.NotNil(resp) assert.Nil(err) assert.Equal(resp.MessageUUID, uuid) + assert.Equal(resp.RequesterIP, "192.168.1.1") cl := client.httpClient client.httpClient = nil resp, err = client.Messages.Get(uuid) diff --git a/setup_sdk.sh b/setup_sdk.sh new file mode 100755 index 00000000..66793daf --- /dev/null +++ b/setup_sdk.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +set -e +testDir="go-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/*.go -type f -exec sed -i "s/$PLIVO_API_PROD_HOST/$PLIVO_API_DEV_HOST/g" {} \; + +if [ ! -d $testDir ]; then + echo "Creating test dir..." + mkdir -p $testDir +fi + +if [ ! -f $testDir/test.go ]; then + echo "Creating test file..." + cd $testDir + echo -e "package main\n" > test.go + echo -e "import (" >> test.go + echo -e "\t\"fmt\"" >> test.go + echo -e "\t\"github.com/plivo/plivo-go\"" >> test.go + echo -e ")\n" >> test.go + echo "func main() {" >> test.go + echo -e "\t_, err := plivo.NewClient(\"\", \"\", &plivo.ClientOptions{})" >> test.go + echo -e "\tif err != nil {" >> test.go + echo -e "\t\tfmt.Printf(\"Error: %s\", err.Error())" >> test.go + echo -e "\t}" >> test.go + echo -e "}" >> test.go + cd - +fi + +if [ ! -f $testDir/go.mod ]; then + echo "Setting up test package..." + cd $testDir + go mod init SDKtest + go mod edit -replace github.com/plivo/plivo-go=/usr/src/app + go mod tidy +else + echo "package has been setup at $testDir" +fi + +echo -e "\n\nSDK setup complete!" +echo "To test your changes:" +echo -e "\t1. Add your test code in /$testDir/test.go on host (or /usr/src/app/$testDir/test.go 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 go run test.go$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 \ No newline at end of file