-
Notifications
You must be signed in to change notification settings - Fork 4
/
hermes.go
86 lines (70 loc) · 1.7 KB
/
hermes.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
package hermes
import (
"context"
"log"
"github.com/rugwirobaker/hermes/observ"
"github.com/rugwirobaker/hermes/pindo"
)
// SMS ...
type SMS struct {
Sender string `json:"sender"`
Payload string `json:"payload"`
Recipient string `json:"recipient"`
}
// Report message queueing status
type Report struct {
Count int64 `json:"count"`
ID int `json:"id"`
Cost float64 `json:"cost"`
Status string `json:"status"`
}
type Callback struct {
MsgRef string `json:"msgRef"`
Recipient string `json:"recipient"`
GatewayRef string `json:"gatewayRef"`
Status int `json:"status"`
}
// SendService is a front to the sending service
type SendService interface {
// Send an sms message and return it's
Send(context.Context, *SMS) (*Report, error)
}
type service struct {
sender string
client *pindo.Client
}
// NewSendService instance of service
func NewSendService(cli *pindo.Client, sender string) (SendService, error) {
return &service{
sender: sender,
client: cli,
}, nil
}
func (s *service) Send(ctx context.Context, message *SMS) (*Report, error) {
const op = "service.Send"
ctx, span := observ.StartSpan(ctx, op)
defer span.End()
sendRequest := &pindo.SendRequest{
Sender: message.Sender,
To: message.Recipient,
Text: message.Payload,
}
if sendRequest.Sender == "" {
sendRequest.Sender = s.sender
}
log.Println("sending message", sendRequest)
res, err := s.client.Send(ctx, sendRequest)
if err != nil {
span.RecordError(err)
return nil, err
}
return convertResponse(res), nil
}
func convertResponse(res *pindo.SendResponse) *Report {
return &Report{
Count: res.ItemCount,
ID: res.SmsID,
Status: res.Status,
Cost: res.TotalCost,
}
}