Skip to content

Commit 82e6ea4

Browse files
authored
This commit adds support to use Email as a channel on Verify (#105)
* 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.
1 parent 012bc19 commit 82e6ea4

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": "8e515072e7f14b7d8c71ee13025c600d",
3+
"status": "sent"
4+
}

verify/testdata/verifyObject.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "15498233759288aaf929661v21936686",
33
"href": "https://rest.messagebird.com/verify/15498233759288aaf929661v21936686",
4-
"recipient": 31612345678,
4+
"recipient": "31612345678",
55
"reference": "MyReference",
66
"messages": {
77
"href": "https://rest.messagebird.com/messages/c2bbd563759288aaf962910b56023756"

verify/testdata/verifyTokenObject.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "a3f2edb23592d68163f9694v13904556",
33
"href": "https://rest.messagebird.com/verify/a3f2edb23592d68163f9694v13904556",
4-
"recipient": 31612345678,
4+
"recipient": "31612345678",
55
"reference": "MyReference",
66
"messages": {
77
"href": "https://rest.messagebird.com/messages/63b168423592d681641eb07b76226648"

verify/verify.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package verify
22

33
import (
4+
"encoding/json"
45
"errors"
6+
"fmt"
57
"net/http"
68
"net/url"
9+
"strconv"
710
"time"
811

912
messagebird "github.com/messagebird/go-rest-api/v6"
@@ -18,7 +21,12 @@ type Verify struct {
1821
Messages map[string]string
1922
CreatedDatetime *time.Time
2023
ValidUntilDatetime *time.Time
21-
Recipient int
24+
Recipient string
25+
}
26+
27+
type VerifyMessage struct {
28+
ID string `json:"id"`
29+
Status string `json:"status"`
2230
}
2331

2432
// Params handles optional verification parameters.
@@ -33,6 +41,7 @@ type Params struct {
3341
Language string
3442
Timeout int
3543
TokenLength int
44+
Subject string
3645
}
3746

3847
type verifyRequest struct {
@@ -47,10 +56,12 @@ type verifyRequest struct {
4756
Language string `json:"language,omitempty"`
4857
Timeout int `json:"timeout,omitempty"`
4958
TokenLength int `json:"tokenLength,omitempty"`
59+
Subject string `json:"subject,omitempty"`
5060
}
5161

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

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

112+
func ReadVerifyEmailMessage(c *messagebird.Client, id string) (*VerifyMessage, error) {
113+
114+
messagePath := emailMessagesPath + "/" + id
115+
116+
verifyMessage := &VerifyMessage{}
117+
if err := c.Request(verifyMessage, http.MethodGet, messagePath, nil); err != nil {
118+
return nil, err
119+
}
120+
121+
return verifyMessage, nil
122+
}
123+
101124
func requestDataForVerify(recipient string, params *Params) (*verifyRequest, error) {
102125
if recipient == "" {
103126
return nil, errors.New("recipient is required")
@@ -121,6 +144,45 @@ func requestDataForVerify(recipient string, params *Params) (*verifyRequest, err
121144
request.Language = params.Language
122145
request.Timeout = params.Timeout
123146
request.TokenLength = params.TokenLength
147+
request.Subject = params.Subject
124148

125149
return request, nil
126150
}
151+
152+
/**
153+
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.
154+
This was the best way to ensure backward compatibility with the previous versions
155+
*/
156+
func (v *Verify) UnmarshalJSON(b []byte) error {
157+
if v == nil {
158+
return errors.New("cannot unmarshal to nil pointer")
159+
}
160+
161+
// Need a type alias so we get a type the same memory layout, but without Verify's method set.
162+
// Otherwise encoding/json will recursively invoke this UnmarshalJSON() implementation.
163+
type Alias Verify
164+
var wrapper struct {
165+
Alias
166+
Recipient interface{}
167+
}
168+
if err := json.Unmarshal(b, &wrapper); err != nil {
169+
return err
170+
}
171+
172+
switch wrapper.Recipient.(type) {
173+
case float64:
174+
const noExponent = 'f'
175+
const precision = -1
176+
const bitSize = 64
177+
asFloat := wrapper.Recipient.(float64)
178+
179+
wrapper.Alias.Recipient = strconv.FormatFloat(asFloat, noExponent, precision, bitSize)
180+
case string:
181+
wrapper.Alias.Recipient = wrapper.Recipient.(string)
182+
default:
183+
return fmt.Errorf("recipient is unknown type %T", wrapper.Recipient)
184+
}
185+
186+
*v = Verify(wrapper.Alias)
187+
return nil
188+
}

verify/verify_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func assertVerifyObject(t *testing.T, v *Verify) {
1717
assert.NotNil(t, v)
1818
assert.Equal(t, "15498233759288aaf929661v21936686", v.ID)
1919
assert.Equal(t, "https://rest.messagebird.com/verify/15498233759288aaf929661v21936686", v.HRef)
20-
assert.Equal(t, 31612345678, v.Recipient)
20+
assert.Equal(t, "31612345678", v.Recipient)
2121
assert.Equal(t, "MyReference", v.Reference)
2222
assert.Len(t, v.Messages, 1)
2323
assert.Equal(t, "https://rest.messagebird.com/messages/c2bbd563759288aaf962910b56023756", v.Messages["href"])
@@ -69,11 +69,24 @@ func TestVerifyToken(t *testing.T) {
6969
assertVerifyTokenObject(t, v)
7070
}
7171

72+
func TestReadVerifyEmailMessage(t *testing.T) {
73+
74+
mbtest.WillReturnTestdata(t, "verifyEmailMessageObject.json", http.StatusOK)
75+
client := mbtest.Client(t)
76+
77+
v, err := ReadVerifyEmailMessage(client, "8e515072e7f14b7d8c71ee13025c600d")
78+
assert.NoError(t, err)
79+
assert.Equal(t, "8e515072e7f14b7d8c71ee13025c600d", v.ID)
80+
assert.Equal(t, "sent", v.Status)
81+
82+
mbtest.AssertEndpointCalled(t, http.MethodGet, "/verify/messages/email/8e515072e7f14b7d8c71ee13025c600d")
83+
}
84+
7285
func assertVerifyTokenObject(t *testing.T, v *Verify) {
7386
assert.NotNil(t, v)
7487
assert.Equal(t, "a3f2edb23592d68163f9694v13904556", v.ID)
7588
assert.Equal(t, "https://rest.messagebird.com/verify/a3f2edb23592d68163f9694v13904556", v.HRef)
76-
assert.Equal(t, 31612345678, v.Recipient)
89+
assert.Equal(t, "31612345678", v.Recipient)
7790
assert.Equal(t, "MyReference", v.Reference)
7891
assert.Len(t, v.Messages, 1)
7992
assert.Equal(t, "https://rest.messagebird.com/messages/63b168423592d681641eb07b76226648", v.Messages["href"])

0 commit comments

Comments
 (0)