-
Notifications
You must be signed in to change notification settings - Fork 1
/
mediana.go
95 lines (80 loc) · 2.29 KB
/
mediana.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package sendo
import (
"bytes"
"context"
"text/template"
"github.com/kamva/hexa"
"github.com/kamva/hexa/hexahttp"
"github.com/kamva/tracer"
)
// medianaService implements the SMSService.
type medianaService struct {
client *hexahttp.Client
defaultSender string
token string
tpl *template.Template
}
type MedianaOptions struct {
ApiClient *hexahttp.Client // APIClient must have base api url as its base API url. (e.g., abc.com/api/v1).
Token string
DefaultSender string
Templates map[string]string
}
func NewMedianaService(o MedianaOptions) (SMSService, error) {
t, err := parseTextTemplates("mediana_root", o.Templates)
if err != nil {
return nil, tracer.Trace(err)
}
return &medianaService{
client: o.ApiClient,
defaultSender: o.DefaultSender,
token: o.Token,
tpl: t,
}, tracer.Trace(err)
}
type MedianaExtra struct {
Token string
}
func (s medianaService) Send(_ context.Context, o SMSOptions) error {
msg, err := s.renderTemplate(o.TemplateName, o.Data)
if err != nil {
return tracer.Trace(err)
}
token := s.token
if extraOptToken := extraOption[*ExtraOptionToken](o.Extra); extraOptToken != nil {
token = extraOptToken.Tokens
}
var authorizationHeader = hexahttp.AuthenticateHeader("apikey", "", token)
sender := o.Sender
if sender == "" {
sender = s.defaultSender
}
payload := hexa.Map{
"recipient": []string{"+" + o.PhoneNumber},
"sender": sender,
"message": msg,
}
r, err := s.client.PostJsonForm("sms/send/webservice/single", payload, authorizationHeader)
if err != nil {
return tracer.Trace(err)
}
return tracer.Trace(hexahttp.ResponseErr(r))
}
func (s *medianaService) renderTemplate(tplName string, data interface{}) (string, error) {
var buf bytes.Buffer
if err := s.tpl.ExecuteTemplate(&buf, tplName, data); err != nil {
return "", tracer.Trace(err)
}
return buf.String(), nil
}
// SendVerificationCode ignores the sender param in mediana driver.
func (s medianaService) SendVerificationCode(ctx context.Context, o VerificationOptions) error {
return s.Send(ctx, SMSOptions{
TemplateName: o.TemplateName,
Sender: o.Sender,
PhoneNumber: o.PhoneNumber,
Data: hexa.Map{"Code": o.Code},
Extra: o.Extra,
})
}
var _ SMSService = &medianaService{}