forked from ramvasanth/wavecell
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
154 lines (131 loc) · 4.13 KB
/
Copy pathclient.go
File metadata and controls
154 lines (131 loc) · 4.13 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package wavecell
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/fairyhunter13/iso8601/v2"
"github.com/fairyhunter13/phone"
"github.com/fairyhunter13/pool"
"github.com/gofiber/fiber/v2"
"net/http"
)
// Client is the contract for the Wavecell client.
type Client interface {
// SendSMSV1 sends one message to one recipient.
// The resp here can be either *ResponseError, *ResponseSendSMS, or nil.
// This method is based on the documentation at: https://developer.8x8.com/connect/reference/send-sms-single.
SendSMSV1(ctx context.Context, req *RequestSendSMS) (resp *ResponseSendSMS, err error)
}
type client struct {
opt *Option
}
// Assign assigns the opt to the client.
func (c *client) Assign(opt *Option) *client {
if opt == nil {
return c
}
c.opt = opt.Clone()
return c
}
// New returns a new Sender struct.
func New(opts ...FnOption) (c Client, err error) {
o := new(Option).Assign(opts...).Default()
err = o.Validate()
if err != nil {
return
}
c = (new(client)).Assign(o)
return
}
// ResponseSendSMS is the response struct for SendSMSV1.
type ResponseSendSMS struct {
UmID string `json:"umid"`
Destination string `json:"destination"`
Status ResponseSendSMSStatus `json:"status"`
Encoding string `json:"encoding"`
ClientMessageID string `json:"clientMessageId,omitempty"`
}
// ResponseSendSMSStatus is the response struct for SendSMSV1.
type ResponseSendSMSStatus struct {
Code string `json:"code"`
Description string `json:"description"`
}
// RequestSendSMS is the request struct for SendSMSV1.
type RequestSendSMS struct {
Destination string `json:"destination,omitempty" validate:"required"`
Country string `json:"country,omitempty"`
Source string `json:"source,omitempty"`
ClientMessageID string `json:"clientMessageId,omitempty"`
Text string `json:"text,omitempty" validate:"required"`
Encoding string `json:"encoding,omitempty"`
Scheduled *iso8601.Time `json:"scheduled,omitempty"`
Expiry *iso8601.Time `json:"expiry,omitempty"`
DlrCallbackURL string `json:"dlrCallbackUrl,omitempty"`
ClientIP string `json:"clientIp,omitempty"`
Track string `json:"track,omitempty"`
}
// Normalize normalizes the request.
func (r *RequestSendSMS) Normalize() *RequestSendSMS {
r.Destination = phone.NormalizeID(r.Destination, 0)
return r
}
func (s *client) getReqBuffer(req interface{}) (buf *bytes.Buffer, err error) {
buf = pool.GetBuffer()
err = json.NewEncoder(buf).Encode(req)
return
}
func (s *client) clean(buff *bytes.Buffer) {
pool.Put(buff)
}
func (s *client) getFullURL(action string) string {
return s.opt.BaseURL + action
}
// SendSMSV1 sends one message to one recipient.
// The resp here can be either *ResponseError, *ResponseSendSMS, or nil.
func (s *client) SendSMSV1(ctx context.Context, req *RequestSendSMS) (resp *ResponseSendSMS, err error) {
buff, err := s.getReqBuffer(req.Normalize())
if err != nil {
return
}
defer s.clean(buff)
endpoint := fmt.Sprintf(URLSendSMS, s.opt.SubAccountID)
r, err := http.NewRequest(http.MethodPost, s.getFullURL(endpoint), buff)
if err != nil {
return
}
res, err := s.doRequest(ctx, r)
if err != nil {
return
}
defer func() {
if res.Body != nil {
_ = res.Body.Close()
}
}()
decoder := json.NewDecoder(res.Body)
if s.isResponseError(res) {
var respErr ResponseError
err = decoder.Decode(&respErr)
if err != nil {
return
}
err = &respErr
return
}
err = decoder.Decode(&resp)
return
}
func (s *client) isResponseError(resp *http.Response) bool {
return resp.StatusCode >= http.StatusBadRequest
}
func (s *client) doRequest(ctx context.Context, req *http.Request) (resp *http.Response, err error) {
if s.opt.APIKey != "" {
req.Header.Add(fiber.HeaderAuthorization, HeaderAuthBearerValue+s.opt.APIKey)
}
req.Header.Add(fiber.HeaderContentType, fiber.MIMEApplicationJSON)
req.Header.Add(fiber.HeaderAccept, fiber.MIMEApplicationJSON)
req = req.WithContext(ctx)
resp, err = s.opt.client.Do(req)
return
}