Skip to content

Commit

Permalink
SMS-6063: Go SDK for Verify Session APIs (#180)
Browse files Browse the repository at this point in the history
* Added create and get functions for verify go sdk

* Added list function (without query params) for verify go sdk

* Added validate function for verify go sdk

* List function name changes

* List function query params added

* Added testcases for verify function sdks

* Updated the version

* Updated the changelog

* Name updated

* version added

* Subaccount param added

* updated the version
  • Loading branch information
rajneeshkatkam-plivo authored Aug 10, 2023
1 parent f8c290b commit 060fe62
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Change Log
## [7.36.0] (https://github.com/plivo/plivo-go/tree/v7.36.0) (2023-08-10)
**Feature - Verify**
- Added Create Session API
- Added Get Session API
- Added List Session API
- Added Validate Session API

## [7.35.0](https://github.com/plivo/plivo-go/tree/v7.35.0) (2023-08-07)
**Feature - WhatsApp message support**
- Added new param `template` and new message_type `whatsapp` to [send message API](https://www.plivo.com/docs/sms/api/message#send-a-message)
Expand Down
2 changes: 1 addition & 1 deletion baseclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/google/go-querystring/query"
)

const sdkVersion = "7.35.0"
const sdkVersion = "7.36.0"

const lookupBaseUrl = "lookup.plivo.com"

Expand Down
21 changes: 21 additions & 0 deletions fixtures/verifySessionGetResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"api_id": "a59512b2-b659-4b27-bfbe-3c053990e23c",
"session_uuid": "4124e518-a8c9-4feb-8cff-d86636ba9234",
"app_uuid": "eff1854e-2682-4222-a5c0-b1a16d9bb4aa",
"alias": "Test-25",
"recipient": "918097480998",
"channel": "sms",
"status": "expired",
"count": 0,
"requestor_ip": "172.167.8.2",
"destination_country_iso2": "IN",
"destination_network": "AirTel",
"attempt_details": null,
"charges": {
"total_charge": "0",
"validation_charge": "0.0000",
"attempt_charges": null
},
"created_at": "2023-06-30T12:12:03.187679+05:30",
"updated_at": "0001-01-01T05:53:28+05:53"
}
69 changes: 69 additions & 0 deletions fixtures/verifySessionListResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"api_id": "3c796fab-c610-4b23-930e-471adf6ab16c",
"meta": {
"limit": 20,
"offset": 0,
"next": null,
"previous": null
},
"sessions": [
{
"session_uuid": "7b28af5c-3e25-45e2-be1e-9235e00dc87a",
"app_uuid": "3cdec449-a367-435e-b4f9-40d777a7cfcc",
"recipient": "918707046409",
"channel": "sms",
"status": "expired",
"count": 1,
"attempt_details": [
{
"channel": "sms",
"attempt_uuid": "f26731c0-d076-42d3-b678-264b0a610b87",
"status": "failed",
"time": "2023-07-20T08:02:18.981765Z"
}
],
"charges": {
"total_charge": "0",
"validation_charge": "0.0000",
"attempt_charges": [
{
"attempt_uuid": "f26731c0-d076-42d3-b678-264b0a610b87",
"channel": "sms",
"charge": "0.00000"
}
]
},
"created_at": "2023-07-20T08:02:18.97278Z",
"updated_at": "2023-07-20T08:02:18.981765Z"
},
{
"session_uuid": "18419654-b963-4d7b-bad5-09535f36fed6",
"app_uuid": "3cdec449-a367-435e-b4f9-40d777a7cfcc",
"recipient": "918707046409",
"channel": "sms",
"status": "expired",
"count": 1,
"attempt_details": [
{
"channel": "sms",
"attempt_uuid": "237bb45f-6238-40fc-a27e-355dbaa8ec02",
"status": "failed",
"time": "2023-07-20T07:50:36.623172Z"
}
],
"charges": {
"total_charge": "0",
"validation_charge": "0.0000",
"attempt_charges": [
{
"attempt_uuid": "237bb45f-6238-40fc-a27e-355dbaa8ec02",
"channel": "sms",
"charge": "0.00000"
}
]
},
"created_at": "2023-07-20T07:50:36.612725Z",
"updated_at": "2023-07-20T07:50:36.623172Z"
}
]
}
5 changes: 5 additions & 0 deletions fixtures/verifySessionSendResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"api_id": "948b53a2-3f08-11e7-b6f4-061564b78b75",
"message": "Session initiated",
"session_uuid":"5b40a428-bfc7-4daf-9d06-726c558bf3b8"
}
5 changes: 5 additions & 0 deletions fixtures/verifySessionValidateResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"api_id": "948b53a2-3f08-11e7-b6f4-061564b78b75",
"message": "session validated successfully.",
"session_uuid":"5b40a428-bfc7-4daf-9d06-726c558bf3b8"
}
2 changes: 2 additions & 0 deletions plivoclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Client struct {
Profile *ProfileService
Campaign *CampaignService
MaskingSession *MaskingSessionService
VerifySession *VerifyService
}

/*
Expand Down Expand Up @@ -118,6 +119,7 @@ func NewClient(authId, authToken string, options *ClientOptions) (client *Client
client.Campaign = &CampaignService{client: client}
client.Profile = &ProfileService{client: client}
client.MaskingSession = &MaskingSessionService{client: client}
client.VerifySession = &VerifyService{client: client}
return
}

Expand Down
135 changes: 135 additions & 0 deletions verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package plivo

import (
"time"
)

type VerifyService struct {
client *Client
Session
}

type SessionList struct {
APIID string `json:"api_id"`
Meta struct {
Limit int `json:"limit"`
Offset int `json:"offset"`
Next *string `json:"next"`
Previous *string `json:"previous"`
} `json:"meta"`
Sessions []Session `json:"sessions"`
}

type Session struct {
APIID string `json:"api_id,omitempty"`
SessionUUID string `json:"session_uuid,omitempty"`
AppUUID string `json:"app_uuid,omitempty"`
Alias string `json:"alias,omitempty"`
Recipient string `json:"recipient,omitempty"`
Channel string `json:"channel,omitempty"`
Status string `json:"status,omitempty"`
Count int `json:"count,omitempty"`
RequesterIP string `json:"requestor_ip,omitempty"`
CountryISO string `json:"destination_country_iso2,omitempty"`
DestinationNetwork *string `json:"destination_network,omitempty"`
AttemptDetails []AttemptDetails `json:"attempt_details,omitempty"`
Charges Charges `json:"charges,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}

type AttemptDetails struct {
Channel string `json:"channel,omitempty"`
AttemptUUID string `json:"attempt_uuid,omitempty"`
Status string `json:"status,omitempty"`
Time time.Time `json:"time,omitempty"`
}

type Charges struct {
TotalCharge string `json:"total_charge,omitempty"`
ValidationCharge string `json:"validation_charge,omitempty"`
AttemptCharges []AttemptCharges `json:"attempt_charges,omitempty"`
}

type AttemptCharges struct {
AttemptUUID string `json:"attempt_uuid,omitempty"`
Channel string `json:"channel,omitempty"`
Charge string `json:"charge,omitempty"`
}

type SessionCreateParams struct {
Recipient string `json:"recipient,omitempty"`
// Optional parameters.
AppUUID string `json:"app_uuid,omitempty"`
Channel string `json:"channel,omitempty"`
URL string `json:"url,omitempty"`
Method string `json:"method,omitempty"`
Src string `json:"src,omitempty"`
}

type SessionCreateResponseBody struct {
APIID string `json:"api_id,omitempty"`
Error string `json:"error,omitempty"`
Message string `json:"message,omitempty"`
SessionUUID string `json:"session_uuid,omitempty"`
}

type SessionValidationParams struct {
OTP string `json:"otp,omitempty"`
}

type SessionListParams struct {
Limit int `url:"limit,omitempty"`
Offset int `url:"offset,omitempty"`
Status string `url:"status,omitempty"`
Recipient string `url:"recipient,omitempty"`
AppUUID string `url:"app_uuid,omitempty"`
Country string `url:"country,omitempty"`
Alias string `url:"alias,omitempty"`
SessionTime string `url:"session_time,omitempty"`
Subaccount string `url:"subaccount,omitempty"`
SessionTimeGreaterThan string `url:"session_time__gt,omitempty"`
SessionTimeGreaterOrEqual string `url:"session_time__gte,omitempty"`
SessionTimeLessThan string `url:"session_time__lt,omitempty"`
SessionTimeLessOrEqual string `url:"session_time__lte,omitempty"`
}

func (service *VerifyService) Create(params SessionCreateParams) (response *SessionCreateResponseBody, err error) {
req, err := service.client.NewRequest("POST", params, "Verify/Session")
if err != nil {
return
}
response = &SessionCreateResponseBody{}
err = service.client.ExecuteRequest(req, response)
return
}

func (service *VerifyService) Get(sessionUUID string) (response *Session, err error) {
req, err := service.client.NewRequest("GET", nil, "Verify/Session/%s", sessionUUID)
if err != nil {
return
}
response = &Session{}
err = service.client.ExecuteRequest(req, response)
return
}

func (service *VerifyService) List(params SessionListParams) (response *SessionList, err error) {
req, err := service.client.NewRequest("GET", params, "Verify/Session")
if err != nil {
return
}
response = &SessionList{}
err = service.client.ExecuteRequest(req, response)
return
}

func (service *VerifyService) Validate(params SessionValidationParams, sessionUUID string) (response *SessionCreateResponseBody, err error) {
req, err := service.client.NewRequest("POST", params, "Verify/Session/%s", sessionUUID)
if err != nil {
return
}
response = &SessionCreateResponseBody{}
err = service.client.ExecuteRequest(req, response)
return
}
88 changes: 88 additions & 0 deletions verify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package plivo

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestVerifyService_List(t *testing.T) {
expectResponse("verifySessionListResponse.json", 200)
assert := require.New(t)
resp, err := client.VerifySession.List(SessionListParams{})
assert.NotNil(resp)
assert.Nil(err)
assert.NotEmpty(resp.Sessions[0].SessionUUID)
assert.NotNil(resp.Sessions)
assert.NotNil(resp.Meta)
cl := client.httpClient
client.httpClient = nil
resp, err = client.VerifySession.List(SessionListParams{})
assert.NotNil(err)
assert.Nil(resp)
client.httpClient = cl
assertRequest(t, "GET", "Verify/Session")
}

func TestVerifyService_Get(t *testing.T) {
expectResponse("verifySessionGetResponse.json", 200)
uuid := "4124e518-a8c9-4feb-8cff-d86636ba9234"
assert := require.New(t)
resp, err := client.VerifySession.Get(uuid)
assert.NotNil(resp)
assert.Nil(err)
assert.Equal(resp.SessionUUID, uuid)
cl := client.httpClient
client.httpClient = nil
resp, err = client.VerifySession.Get(uuid)
assert.NotNil(err)
assert.Nil(resp)
client.httpClient = cl

assertRequest(t, "GET", "Verify/Session/%s", uuid)
}

func TestVerifyService_Create(t *testing.T) {
expectResponse("verifySessionSendResponse.json", 202)
assert := require.New(t)
resp, err := client.VerifySession.Create(SessionCreateParams{
Recipient: "9089789099",
})
assert.NotNil(resp)
assert.Nil(err)
assert.NotEmpty(resp.APIID)
assert.NotEmpty(resp.SessionUUID)
assert.Equal(resp.Error, "")
cl := client.httpClient
client.httpClient = nil
resp, err = client.VerifySession.Create(SessionCreateParams{
Recipient: "9089789099",
})
assert.NotNil(err)
assert.Nil(resp)
client.httpClient = cl

assertRequest(t, "POST", "Verify/Session")
}

func TestVerifyService_Validate(t *testing.T) {
expectResponse("verifySessionValidateResponse.json", 200)
assert := require.New(t)
resp, err := client.VerifySession.Validate(SessionValidationParams{
OTP: "9089789",
}, "5b40a428-bfc7-4daf-9d06-726c558bf3b8")
assert.NotNil(resp)
assert.Nil(err)
assert.NotEmpty(resp.APIID)
assert.Equal(resp.Error, "")
cl := client.httpClient
client.httpClient = nil
resp, err = client.VerifySession.Validate(SessionValidationParams{
OTP: "9089789",
}, "5b40a428-bfc7-4daf-9d06-726c558bf3b8")
assert.NotNil(err)
assert.Nil(resp)
client.httpClient = cl

assertRequest(t, "POST", "Verify/Session/%s", "5b40a428-bfc7-4daf-9d06-726c558bf3b8")
}

0 comments on commit 060fe62

Please sign in to comment.