diff --git a/CHANGELOG.md b/CHANGELOG.md index 681ee4a..3832333 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/baseclient.go b/baseclient.go index 69cc6f2..e65f7fb 100644 --- a/baseclient.go +++ b/baseclient.go @@ -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" diff --git a/fixtures/verifySessionGetResponse.json b/fixtures/verifySessionGetResponse.json new file mode 100644 index 0000000..1565a49 --- /dev/null +++ b/fixtures/verifySessionGetResponse.json @@ -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" +} \ No newline at end of file diff --git a/fixtures/verifySessionListResponse.json b/fixtures/verifySessionListResponse.json new file mode 100644 index 0000000..62687e6 --- /dev/null +++ b/fixtures/verifySessionListResponse.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/fixtures/verifySessionSendResponse.json b/fixtures/verifySessionSendResponse.json new file mode 100644 index 0000000..f6b8d79 --- /dev/null +++ b/fixtures/verifySessionSendResponse.json @@ -0,0 +1,5 @@ +{ + "api_id": "948b53a2-3f08-11e7-b6f4-061564b78b75", + "message": "Session initiated", + "session_uuid":"5b40a428-bfc7-4daf-9d06-726c558bf3b8" +} diff --git a/fixtures/verifySessionValidateResponse.json b/fixtures/verifySessionValidateResponse.json new file mode 100644 index 0000000..733e71d --- /dev/null +++ b/fixtures/verifySessionValidateResponse.json @@ -0,0 +1,5 @@ +{ + "api_id": "948b53a2-3f08-11e7-b6f4-061564b78b75", + "message": "session validated successfully.", + "session_uuid":"5b40a428-bfc7-4daf-9d06-726c558bf3b8" +} diff --git a/plivoclient.go b/plivoclient.go index b6683a7..188421e 100644 --- a/plivoclient.go +++ b/plivoclient.go @@ -48,6 +48,7 @@ type Client struct { Profile *ProfileService Campaign *CampaignService MaskingSession *MaskingSessionService + VerifySession *VerifyService } /* @@ -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 } diff --git a/verify.go b/verify.go new file mode 100644 index 0000000..939ca63 --- /dev/null +++ b/verify.go @@ -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 +} diff --git a/verify_test.go b/verify_test.go new file mode 100644 index 0000000..d5fea31 --- /dev/null +++ b/verify_test.go @@ -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") +}