From d6cff0d1023f305b9b5951541b638b02fa748bd0 Mon Sep 17 00:00:00 2001 From: Abinaya-plivo <92303844+Abinaya-Shunmugavel@users.noreply.github.com> Date: Wed, 18 Oct 2023 12:42:17 +0530 Subject: [PATCH] Verify caller ID for outbound voice calls - Go SDK changes (#187) * Verify caller ID for outbound voice calls - Go SDK changes * fixed response structure * added versions --- CHANGELOG.md | 5 + baseclient.go | 4 +- fixtures/getVerifiedCallerIDResponse.json | 10 ++ fixtures/initiateVerifyResponse.json | 5 + fixtures/listVerifiedCallerIDResponse.json | 52 +++++++ fixtures/updateVerifiedCallerIDResponse.json | 10 ++ fixtures/verifyCallerIDResponse.json | 9 ++ maskingsession_test.go | 10 +- plivoclient.go | 2 + verify_callerid.go | 138 +++++++++++++++++++ verify_callerid_test.go | 127 +++++++++++++++++ 11 files changed, 365 insertions(+), 7 deletions(-) create mode 100644 fixtures/getVerifiedCallerIDResponse.json create mode 100644 fixtures/initiateVerifyResponse.json create mode 100644 fixtures/listVerifiedCallerIDResponse.json create mode 100644 fixtures/updateVerifiedCallerIDResponse.json create mode 100644 fixtures/verifyCallerIDResponse.json create mode 100644 verify_callerid.go create mode 100644 verify_callerid_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 3057195..fe7c5a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## [7.39.0](https://github.com/plivo/plivo-go/tree/v7.39.0) (2023-10-18) +**Feature - Verify CallerID** +- Added Initiate and Verify VerifyCallerID API +- Added Update, Delete, Get and List verified CallerIDs API + ## [7.38.0](https://github.com/plivo/plivo-go/tree/v7.38.0) (2023-10-16) **Feature - Campaign API Enhancements & New API for Importing Partner Campaigns** - Import Partner Campaign API diff --git a/baseclient.go b/baseclient.go index a99b341..4da98be 100644 --- a/baseclient.go +++ b/baseclient.go @@ -13,7 +13,7 @@ import ( "github.com/google/go-querystring/query" ) -const sdkVersion = "7.38.0" +const sdkVersion = "7.39.0" const lookupBaseUrl = "lookup.plivo.com" @@ -72,7 +72,7 @@ func (client *BaseClient) NewRequest(method string, params interface{}, baseRequ requestUrl.RawQuery = values.Encode() } else { - if reflect.ValueOf(params).Kind().String() != "map" { + if reflect.ValueOf(params).Kind().String() != "map" && params != nil { if err = json.NewEncoder(buffer).Encode(params); err != nil { return } diff --git a/fixtures/getVerifiedCallerIDResponse.json b/fixtures/getVerifiedCallerIDResponse.json new file mode 100644 index 0000000..f5d312f --- /dev/null +++ b/fixtures/getVerifiedCallerIDResponse.json @@ -0,0 +1,10 @@ +{ + "alias": "test", + "api_id": "ee7c3cb1-c921-42c9-832b-950fb8344b9b", + "country": "IN", + "created_at": "2023-09-22T14:11:03.091534Z", + "modified_at": "2023-09-22T14:11:03.091534Z", + "phone_number": "+919768368718", + "subaccount": "", + "verification_uuid": "0f978b20-9e2b-4cfe-99fe-f7087c03b8e1" +} \ No newline at end of file diff --git a/fixtures/initiateVerifyResponse.json b/fixtures/initiateVerifyResponse.json new file mode 100644 index 0000000..eacf966 --- /dev/null +++ b/fixtures/initiateVerifyResponse.json @@ -0,0 +1,5 @@ +{ + "api_id": "2a5e81d7-deb3-46cd-ac34-c05ca9139b6f", + "message": "Verification code is sent to number +919768368717 which is valid for 15 minutes", + "verification_uuid": "407796a6-1f3e-4607-a9d9-5a5376b59a7b" +} \ No newline at end of file diff --git a/fixtures/listVerifiedCallerIDResponse.json b/fixtures/listVerifiedCallerIDResponse.json new file mode 100644 index 0000000..fbdc2bb --- /dev/null +++ b/fixtures/listVerifiedCallerIDResponse.json @@ -0,0 +1,52 @@ +{ + "api_id": "3b21c7d7-09f7-489b-b753-659814a676bf", + "meta": { + "limit": 20, + "next": null, + "offset": 0, + "previous": null, + "total_count": 4 + }, + "objects": [ + { + "alias": "abhishek", + "country": "IN", + "created_at": "2023-09-25T14:40:38.68729Z", + "modified_at": "2023-09-25T14:40:38.68729Z", + "phone_number": "+919653244280", + "resource_uri": "/v1/Account/MADCHANDRESH02TANK06/VerifiedCallerId/919653244280", + "subaccount": "", + "verification_uuid": "01be6b07-b106-46e2-8dfc-096d8e22cc0e" + }, + { + "alias": "abhishek", + "country": "IN", + "created_at": "2023-09-25T13:10:39.968133Z", + "modified_at": "2023-09-25T13:10:39.968133Z", + "phone_number": "+919768368717", + "resource_uri": "/v1/Account/MADCHANDRESH02TANK06/VerifiedCallerId/919768368717", + "subaccount": "", + "verification_uuid": "2e68eb73-4d54-4391-bc98-71cd380911a4" + }, + { + "alias": "test", + "country": "IN", + "created_at": "2023-09-22T14:11:03.091534Z", + "modified_at": "2023-09-22T14:11:03.091534Z", + "phone_number": "+919768368718", + "resource_uri": "/v1/Account/MADCHANDRESH02TANK06/VerifiedCallerId/919768368718", + "subaccount": "", + "verification_uuid": "0f978b20-9e2b-4cfe-99fe-f7087c03b8e1" + }, + { + "alias": "Test2", + "country": "", + "created_at": "2023-08-30T07:47:43.87171Z", + "modified_at": "2023-08-30T07:47:43.87171Z", + "phone_number": "+917691021365", + "resource_uri": "/v1/Account/MADCHANDRESH02TANK06/VerifiedCallerId/917691021365", + "subaccount": "SAMTU0Y2FKNGETYZDKNI", + "verification_uuid": "20265c57-2d8e-46fe-8fa8-e8ab4ee58a8c" + } + ] +} \ No newline at end of file diff --git a/fixtures/updateVerifiedCallerIDResponse.json b/fixtures/updateVerifiedCallerIDResponse.json new file mode 100644 index 0000000..fbc85c7 --- /dev/null +++ b/fixtures/updateVerifiedCallerIDResponse.json @@ -0,0 +1,10 @@ +{ + "alias": "testAbhishek", + "api_id": "6b143cb3-9c8a-42ad-b6b7-412ebd5c60a9", + "country": "IN", + "created_at": "2023-09-22T14:11:03.091534Z", + "modified_at": "2023-09-22T14:11:03.091534Z", + "phone_number": "+919768368718", + "subaccount": "SAMTU0Y2FKNGETYZDKNI", + "verification_uuid": "0f978b20-9e2b-4cfe-99fe-f7087c03b8e1" +} diff --git a/fixtures/verifyCallerIDResponse.json b/fixtures/verifyCallerIDResponse.json new file mode 100644 index 0000000..92568c8 --- /dev/null +++ b/fixtures/verifyCallerIDResponse.json @@ -0,0 +1,9 @@ +{ + "alias": "abhishek", + "api_id": "584a97aa-ca1d-44f2-9dd3-e58fbacd6649", + "channel": "call", + "country": "IN", + "created_at": "2023-09-25T13:10:39.968133341Z", + "phone_number": "+919768368717", + "verification_uuid": "2e68eb73-4d54-4391-bc98-71cd380911a4" +} \ No newline at end of file diff --git a/maskingsession_test.go b/maskingsession_test.go index d6437b2..af4b1a8 100644 --- a/maskingsession_test.go +++ b/maskingsession_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -func TestCallService_CreateMaskingSession(t *testing.T) { +func TestMaskingSessionService_CreateMaskingSession(t *testing.T) { expectResponse("createMaskingSessionResponse.json", 200) if _, err := client.MaskingSession.CreateMaskingSession(CreateMaskingSessionParams{}); err != nil { @@ -24,7 +24,7 @@ func TestCallService_CreateMaskingSession(t *testing.T) { assertRequest(t, "POST", "Masking/Session") } -func TestCallService_DeleteMaskingSession(t *testing.T) { +func TestMaskingSessionService_DeleteMaskingSession(t *testing.T) { expectResponse("deleteMaskingSessionResponse.json", 204) SessionUuid := "15e4256c-be01-475c-9a69-95cf65bbed71" @@ -44,7 +44,7 @@ func TestCallService_DeleteMaskingSession(t *testing.T) { assertRequest(t, "DELETE", "Masking/Session/%s", SessionUuid) } -func TestCallService_GetMaskingSession(t *testing.T) { +func TestMaskingSessionService_GetMaskingSession(t *testing.T) { expectResponse("getMaskingSessionResponse.json", 200) SessionUuid := "15e4256c-be01-475c-9a69-95cf65bbed71" @@ -64,7 +64,7 @@ func TestCallService_GetMaskingSession(t *testing.T) { assertRequest(t, "GET", "Masking/Session/%s", SessionUuid) } -func TestCallService_updateMaskingSession(t *testing.T) { +func TestMaskingSessionService_updateMaskingSession(t *testing.T) { expectResponse("updateMaskingSessionResponse.json", 204) SessionUuid := "15e4256c-be01-475c-9a69-95cf65bbed71" @@ -84,7 +84,7 @@ func TestCallService_updateMaskingSession(t *testing.T) { assertRequest(t, "POST", "Masking/Session/%s", SessionUuid) } -func TestCallService_ListMaskingSession(t *testing.T) { +func TestMaskingSessionService_ListMaskingSession(t *testing.T) { expectResponse("listMaskingSessionResponse.json", 204) if _, err := client.MaskingSession.ListMaskingSession(ListSessionFilterParams{}); err != nil { diff --git a/plivoclient.go b/plivoclient.go index 188421e..aaebfb3 100644 --- a/plivoclient.go +++ b/plivoclient.go @@ -49,6 +49,7 @@ type Client struct { Campaign *CampaignService MaskingSession *MaskingSessionService VerifySession *VerifyService + VerifyCallerId *VerifyCallerIdService } /* @@ -120,6 +121,7 @@ func NewClient(authId, authToken string, options *ClientOptions) (client *Client client.Profile = &ProfileService{client: client} client.MaskingSession = &MaskingSessionService{client: client} client.VerifySession = &VerifyService{client: client} + client.VerifyCallerId = &VerifyCallerIdService{client: client} return } diff --git a/verify_callerid.go b/verify_callerid.go new file mode 100644 index 0000000..9bd4117 --- /dev/null +++ b/verify_callerid.go @@ -0,0 +1,138 @@ +package plivo + +import ( + "time" +) + +type VerifyCallerIdService struct { + client *Client +} + +type InitiateVerify struct { + PhoneNumber string `json:"phone_number"` + Alias string `json:"alias"` + Channel string `json:"channel"` + Country string `json:"country"` + SubAccount string `json:"subaccount"` + AccountID int64 `json:"account_id"` + AuthID string `json:"auth_id"` + AuthToken string `json:"auth_token"` +} + +type InitiateVerifyResponse struct { + ApiID string `json:"api_id,omitempty" url:"api_id,omitempty"` + Message string `json:"message,omitempty" url:"message,omitempty"` + VerificationUUID string `json:"verification_uuid,omitempty" url:"verification_uuid,omitempty"` +} + +type ListVerifiedCallerIdParams struct { + Country string `json:"country,omitempty" url:"country,omitempty"` + SubAccount string `json:"subaccount,omitempty" url:"subaccount,omitempty"` + Alias string `json:"alias,omitempty" url:"alias,omitempty"` + Limit int64 `json:"limit,omitempty" url:"limit,omitempty"` + Offset int64 `json:"offset,omitempty" url:"offset,omitempty"` +} + +type VerifyResponse struct { + Alias string `json:"alias,omitempty"` + ApiID string `json:"api_id,omitempty" url:"api_id,omitempty"` + Channel string `json:"channel"` + Country string `json:"country"` + CreatedAt time.Time `json:"created_at"` + PhoneNumber string `json:"phone_number"` + VerificationUUID string `json:"verification_uuid"` + SubAccount string `json:"subaccount,omitempty"` +} + +type UpdateVerifiedCallerIDParams struct { + Alias string `json:"alias,omitempty"` + SubAccount string `json:"subaccount,omitempty"` +} + +type GetVerifyResponse struct { + Alias string `json:"alias,omitempty"` + ApiID string `json:"api_id,omitempty" url:"api_id,omitempty"` + Country string `json:"country"` + CreatedAt time.Time `json:"created_at"` + ModifiedAt time.Time `json:"modified_at"` + PhoneNumber string `json:"phone_number"` + SubAccount string `json:"subaccount,omitempty" url:"subaccount,omitempty"` + VerificationUUID string `json:"verification_uuid"` +} + +type ListVerifyResponse struct { + Alias string `json:"alias,omitempty"` + Country string `json:"country"` + CreatedAt time.Time `json:"created_at"` + ModifiedAt time.Time `json:"modified_at"` + PhoneNumber string `json:"phone_number"` + ResourceUri string `json:"resource_uri,omitempty"` + SubAccount string `json:"subaccount,omitempty"` + VerificationUUID string `json:"verification_uuid"` +} + +type ListVerifiedCallerIDResponse struct { + ApiID string `json:"api_id,omitempty" url:"api_id,omitempty"` + Meta Meta `json:"meta" url:"meta"` + Objects []ListVerifyResponse `json:"objects" url:"objects"` +} + +func (service *VerifyCallerIdService) InitiateVerify(params InitiateVerify) (response *InitiateVerifyResponse, err error) { + req, err := service.client.NewRequest("POST", params, "VerifiedCallerId") + if err != nil { + return + } + response = &InitiateVerifyResponse{} + err = service.client.ExecuteRequest(req, response, isVoiceRequest()) + return +} + +func (service *VerifyCallerIdService) VerifyCallerID(verificationUuid string, otp string) (response *VerifyResponse, err error) { + req, err := service.client.NewRequest("POST", map[string]string{"otp": otp}, "VerifiedCallerId/Verification/%s", verificationUuid) + if err != nil { + return + } + response = &VerifyResponse{} + err = service.client.ExecuteRequest(req, response, isVoiceRequest()) + return +} + +func (service *VerifyCallerIdService) DeleteVerifiedCallerID(phoneNumber string) (err error) { + req, err := service.client.NewRequest("DELETE", nil, "VerifiedCallerId/%s", phoneNumber) + if err != nil { + return + } + err = service.client.ExecuteRequest(req, nil, isVoiceRequest()) + + return +} + +func (service *VerifyCallerIdService) UpdateVerifiedCallerID(phoneNumber string, params UpdateVerifiedCallerIDParams) (response *GetVerifyResponse, err error) { + req, err := service.client.NewRequest("POST", params, "VerifiedCallerId/%s", phoneNumber) + if err != nil { + return + } + response = &GetVerifyResponse{} + err = service.client.ExecuteRequest(req, response, isVoiceRequest()) + return +} + +func (service *VerifyCallerIdService) GetVerifiedCallerID(phoneNumber string) (response *GetVerifyResponse, err error) { + req, err := service.client.NewRequest("GET", nil, "VerifiedCallerId/%s", phoneNumber) + if err != nil { + return + } + response = &GetVerifyResponse{} + err = service.client.ExecuteRequest(req, response, isVoiceRequest()) + return +} + +func (service *VerifyCallerIdService) ListVerifiedCallerID(params ListVerifiedCallerIdParams) (response *ListVerifiedCallerIDResponse, err error) { + req, err := service.client.NewRequest("GET", params, "VerifiedCallerId") + if err != nil { + return + } + response = &ListVerifiedCallerIDResponse{} + err = service.client.ExecuteRequest(req, response, isVoiceRequest()) + return +} diff --git a/verify_callerid_test.go b/verify_callerid_test.go new file mode 100644 index 0000000..d6ef3a2 --- /dev/null +++ b/verify_callerid_test.go @@ -0,0 +1,127 @@ +package plivo + +import ( + "errors" + "testing" +) + +func TestVerifyCallerIdService_InitiateVerify(t *testing.T) { + expectResponse("initiateVerifyResponse.json", 200) + + if _, err := client.VerifyCallerId.InitiateVerify(InitiateVerify{ + PhoneNumber: "917708772011", + Alias: "test", + Channel: "call", + }); err != nil { + panic(err) + } + cl := client.httpClient + client.httpClient = nil + _, err := client.VerifyCallerId.InitiateVerify(InitiateVerify{ + PhoneNumber: "917708772011", + Alias: "test", + Channel: "call", + }) + if err == nil { + client.httpClient = cl + panic(errors.New("error expected")) + } + client.httpClient = cl + + assertRequest(t, "POST", "VerifiedCallerId") +} + +func TestVerifyCallerIdService_VerifyCallerID(t *testing.T) { + expectResponse("verifyCallerIDResponse.json", 200) + + if _, err := client.VerifyCallerId.VerifyCallerID("eeab1477-e59b-4821-9e61-fd5847c2a5db", "610534"); err != nil { + panic(err) + } + + cl := client.httpClient + client.httpClient = nil + _, err := client.VerifyCallerId.VerifyCallerID("eeab1477-e59b-4821-9e61-fd5847c2a5db", "610534") + if err == nil { + client.httpClient = cl + panic(errors.New("error expected")) + } + client.httpClient = cl + + assertRequest(t, "POST", "VerifiedCallerId/Verification/eeab1477-e59b-4821-9e61-fd5847c2a5db") +} + +func TestVerifyCallerIdService_DeleteVerifiedCallerID(t *testing.T) { + expectResponse("", 204) + + if err := client.VerifyCallerId.DeleteVerifiedCallerID("917708772010"); err != nil { + panic(err) + } + + cl := client.httpClient + client.httpClient = nil + err := client.VerifyCallerId.DeleteVerifiedCallerID("917708772010") + if err == nil { + client.httpClient = cl + panic(errors.New("error expected")) + } + client.httpClient = cl + + assertRequest(t, "DELETE", "VerifiedCallerId/917708772010") +} + +func TestVerifyCallerIdService_GetVerifiedCallerID(t *testing.T) { + expectResponse("getVerifiedCallerIDResponse.json", 200) + + if _, err := client.VerifyCallerId.GetVerifiedCallerID("917708772010"); err != nil { + panic(err) + } + + cl := client.httpClient + client.httpClient = nil + _, err := client.VerifyCallerId.GetVerifiedCallerID("917708772010") + if err == nil { + client.httpClient = cl + panic(errors.New("error expected")) + } + client.httpClient = cl + + assertRequest(t, "GET", "VerifiedCallerId/917708772010") +} + +func TestVerifyCallerIdService_UpdateVerifiedCallerID(t *testing.T) { + expectResponse("updateVerifiedCallerIDResponse.json", 204) + + if _, err := client.VerifyCallerId.UpdateVerifiedCallerID("917708772010", UpdateVerifiedCallerIDParams{}); err != nil { + panic(err) + } + + cl := client.httpClient + client.httpClient = nil + _, err := client.VerifyCallerId.UpdateVerifiedCallerID("917708772010", UpdateVerifiedCallerIDParams{}) + if err == nil { + client.httpClient = cl + panic(errors.New("error expected")) + } + client.httpClient = cl + + assertRequest(t, "POST", "VerifiedCallerId/917708772010") +} + +func TestVerifyCallerIdService_ListVerifiedCallerID(t *testing.T) { + expectResponse("listVerifiedCallerIDResponse.json", 204) + + if _, err := client.VerifyCallerId.ListVerifiedCallerID(ListVerifiedCallerIdParams{}); err != nil { + panic(err) + } + + cl := client.httpClient + client.httpClient = nil + _, err := client.VerifyCallerId.ListVerifiedCallerID(ListVerifiedCallerIdParams{}) + if err == nil { + client.httpClient = cl + panic(errors.New("error expected")) + } + client.httpClient = cl + + assertRequest(t, "GET", "VerifiedCallerId") +}