Skip to content

Commit

Permalink
This commit adds support to use Email as a channel on Verify (#105)
Browse files Browse the repository at this point in the history
* This commit adds support to use Email as a channel on the Verify product.

The changes included in this changes are:
(1) "email" is now a supported verify type
(2) The property Recipient in the Verify object changed type from int to string to support email addresses. The Create methos signature was already expecting a string as the recipient so that was handy for this change to not be a bit breaking change to current users. If for some reasons the Recipient property was accssed directly, it might break current usage.
(3) The property Subject was added to enable SDK users to specify the Subkect of the OTP email
(4)The method GetVerifyEmailMessage was added to retrieve a email message object
(5) Tests for new method were added and previous tests were fix as change (1) broke them.
  • Loading branch information
leandroshp authored Jun 25, 2021
1 parent 012bc19 commit 82e6ea4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
4 changes: 4 additions & 0 deletions verify/testdata/verifyEmailMessageObject.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "8e515072e7f14b7d8c71ee13025c600d",
"status": "sent"
}
2 changes: 1 addition & 1 deletion verify/testdata/verifyObject.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "15498233759288aaf929661v21936686",
"href": "https://rest.messagebird.com/verify/15498233759288aaf929661v21936686",
"recipient": 31612345678,
"recipient": "31612345678",
"reference": "MyReference",
"messages": {
"href": "https://rest.messagebird.com/messages/c2bbd563759288aaf962910b56023756"
Expand Down
2 changes: 1 addition & 1 deletion verify/testdata/verifyTokenObject.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "a3f2edb23592d68163f9694v13904556",
"href": "https://rest.messagebird.com/verify/a3f2edb23592d68163f9694v13904556",
"recipient": 31612345678,
"recipient": "31612345678",
"reference": "MyReference",
"messages": {
"href": "https://rest.messagebird.com/messages/63b168423592d681641eb07b76226648"
Expand Down
64 changes: 63 additions & 1 deletion verify/verify.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package verify

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

messagebird "github.com/messagebird/go-rest-api/v6"
Expand All @@ -18,7 +21,12 @@ type Verify struct {
Messages map[string]string
CreatedDatetime *time.Time
ValidUntilDatetime *time.Time
Recipient int
Recipient string
}

type VerifyMessage struct {
ID string `json:"id"`
Status string `json:"status"`
}

// Params handles optional verification parameters.
Expand All @@ -33,6 +41,7 @@ type Params struct {
Language string
Timeout int
TokenLength int
Subject string
}

type verifyRequest struct {
Expand All @@ -47,10 +56,12 @@ type verifyRequest struct {
Language string `json:"language,omitempty"`
Timeout int `json:"timeout,omitempty"`
TokenLength int `json:"tokenLength,omitempty"`
Subject string `json:"subject,omitempty"`
}

// path represents the path to the Verify resource.
const path = "verify"
const emailMessagesPath = path + "/messages/email"

// Create generates a new One-Time-Password for one recipient.
func Create(c *messagebird.Client, recipient string, params *Params) (*Verify, error) {
Expand Down Expand Up @@ -98,6 +109,18 @@ func VerifyToken(c *messagebird.Client, id, token string) (*Verify, error) {
return verify, nil
}

func ReadVerifyEmailMessage(c *messagebird.Client, id string) (*VerifyMessage, error) {

messagePath := emailMessagesPath + "/" + id

verifyMessage := &VerifyMessage{}
if err := c.Request(verifyMessage, http.MethodGet, messagePath, nil); err != nil {
return nil, err
}

return verifyMessage, nil
}

func requestDataForVerify(recipient string, params *Params) (*verifyRequest, error) {
if recipient == "" {
return nil, errors.New("recipient is required")
Expand All @@ -121,6 +144,45 @@ func requestDataForVerify(recipient string, params *Params) (*verifyRequest, err
request.Language = params.Language
request.Timeout = params.Timeout
request.TokenLength = params.TokenLength
request.Subject = params.Subject

return request, nil
}

/**
The type of the Verify.Recipient object changed from int to string but the api still returns a recipent numeric value whne sms type is used.
This was the best way to ensure backward compatibility with the previous versions
*/
func (v *Verify) UnmarshalJSON(b []byte) error {
if v == nil {
return errors.New("cannot unmarshal to nil pointer")
}

// Need a type alias so we get a type the same memory layout, but without Verify's method set.
// Otherwise encoding/json will recursively invoke this UnmarshalJSON() implementation.
type Alias Verify
var wrapper struct {
Alias
Recipient interface{}
}
if err := json.Unmarshal(b, &wrapper); err != nil {
return err
}

switch wrapper.Recipient.(type) {
case float64:
const noExponent = 'f'
const precision = -1
const bitSize = 64
asFloat := wrapper.Recipient.(float64)

wrapper.Alias.Recipient = strconv.FormatFloat(asFloat, noExponent, precision, bitSize)
case string:
wrapper.Alias.Recipient = wrapper.Recipient.(string)
default:
return fmt.Errorf("recipient is unknown type %T", wrapper.Recipient)
}

*v = Verify(wrapper.Alias)
return nil
}
17 changes: 15 additions & 2 deletions verify/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func assertVerifyObject(t *testing.T, v *Verify) {
assert.NotNil(t, v)
assert.Equal(t, "15498233759288aaf929661v21936686", v.ID)
assert.Equal(t, "https://rest.messagebird.com/verify/15498233759288aaf929661v21936686", v.HRef)
assert.Equal(t, 31612345678, v.Recipient)
assert.Equal(t, "31612345678", v.Recipient)
assert.Equal(t, "MyReference", v.Reference)
assert.Len(t, v.Messages, 1)
assert.Equal(t, "https://rest.messagebird.com/messages/c2bbd563759288aaf962910b56023756", v.Messages["href"])
Expand Down Expand Up @@ -69,11 +69,24 @@ func TestVerifyToken(t *testing.T) {
assertVerifyTokenObject(t, v)
}

func TestReadVerifyEmailMessage(t *testing.T) {

mbtest.WillReturnTestdata(t, "verifyEmailMessageObject.json", http.StatusOK)
client := mbtest.Client(t)

v, err := ReadVerifyEmailMessage(client, "8e515072e7f14b7d8c71ee13025c600d")
assert.NoError(t, err)
assert.Equal(t, "8e515072e7f14b7d8c71ee13025c600d", v.ID)
assert.Equal(t, "sent", v.Status)

mbtest.AssertEndpointCalled(t, http.MethodGet, "/verify/messages/email/8e515072e7f14b7d8c71ee13025c600d")
}

func assertVerifyTokenObject(t *testing.T, v *Verify) {
assert.NotNil(t, v)
assert.Equal(t, "a3f2edb23592d68163f9694v13904556", v.ID)
assert.Equal(t, "https://rest.messagebird.com/verify/a3f2edb23592d68163f9694v13904556", v.HRef)
assert.Equal(t, 31612345678, v.Recipient)
assert.Equal(t, "31612345678", v.Recipient)
assert.Equal(t, "MyReference", v.Reference)
assert.Len(t, v.Messages, 1)
assert.Equal(t, "https://rest.messagebird.com/messages/63b168423592d681641eb07b76226648", v.Messages["href"])
Expand Down

0 comments on commit 82e6ea4

Please sign in to comment.