forked from levpay/infobip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
270 lines (216 loc) · 6.17 KB
/
client.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package infobip
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
)
const (
// BasePath ...
BasePath = "https://api.infobip.com/"
//SingleMessagePath for sending a single message
SingleMessagePath = "sms/1/text/single"
//AdvancedMessagePath for sending advanced messages
AdvancedMessagePath = "sms/1/text/advanced"
// AvailableNumberPath for searching number
AvailableNumberPath = "numbers/1/numbers/available"
// RentNumberPath for purchasing number
RentNumberPath = "numbers/1/numbers"
// UnrentNumberPath for purchasing number
UnrentNumberPath = "numbers/1/numbers"
// PurchasedNumberPath to get purchased number
PurchasedNumberPath = "numbers/1/numbers"
// SMSStatusPath for getting status
SMSStatusPath = "sms/1/reports"
)
// HTTPInterface helps Infobip tests
type HTTPInterface interface {
Do(req *http.Request) (*http.Response, error)
}
// Client manages requests to Infobip
type Client struct {
BaseURL string
Username string
Password string
ApiKey string
HTTPClient HTTPInterface
}
// ClientWithBasicAuth returns a pointer to infobip.Client with Infobip funcs
func ClientWithBasicAuth(username, password string) *Client {
return &Client{
BaseURL: BasePath,
Username: username,
Password: password,
HTTPClient: &http.Client{},
}
}
// ClientWithApiKey returns a pointer to infobip.Client with Infobip funcs
func ClientWithApiKey(apiKey string) *Client {
return &Client{
BaseURL: BasePath,
ApiKey: apiKey,
HTTPClient: &http.Client{},
}
}
// CreateNumberConfig create a config for a specific number key
func (c Client) CreateNumberConfig(numberKey string) (*ConfigResponse, error) {
configPath := fmt.Sprintf("numbers/1/numbers/%s/configurations", numberKey)
resp := &ConfigResponse{}
if err := c.defaultPostRequest(nil, configPath, resp); err != nil {
return nil, err
}
return resp, nil
}
// CreateNumberAction create a action for a specific number key
func (c Client) CreateNumberAction(actionData *Action, numberKey, configKey string) (*Action, error) {
actionPath := fmt.Sprintf("numbers/1/numbers/%s/configurations/%s/actions", numberKey, configKey)
b, err := json.Marshal(actionData)
if err != nil {
return nil, err
}
resp := &Action{}
if err := c.defaultPostRequest(b, actionPath, resp); err != nil {
return nil, err
}
return resp, nil
}
// Rent return a newly purchased number
func (c Client) Rent(numberKey string) (*Number, error) {
data := map[string]string{
"numberKey": numberKey,
}
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
num := &Number{}
if err := c.defaultPostRequest(b, RentNumberPath, num); err != nil {
return nil, err
}
return num, nil
}
// Unrent rent a number using numberKey
func (c Client) Unrent(numberKey string) error {
path := UnrentNumberPath + "/" + numberKey
return c.defaultDeleteRequest(path)
}
// SearchNumber return a list of available number
func (c Client) SearchNumber(parmas NumberParmas) (*NumberResponse, error) {
v, err := query.Values(parmas)
if err != nil {
return nil, err
}
path := AvailableNumberPath + "?" + v.Encode()
resp := &NumberResponse{}
if err := c.defaultGetRequest(path, resp); err != nil {
return nil, err
}
return resp, nil
}
// ListPurchasedNumber return a purchased number
func (c Client) ListPurchasedNumber(parmas NumberParmas) (*NumberResponse, error) {
v, err := query.Values(parmas)
if err != nil {
return nil, err
}
path := PurchasedNumberPath + "?" + v.Encode()
resp := &NumberResponse{}
if err := c.defaultGetRequest(path, resp); err != nil {
return nil, err
}
return resp, nil
}
// SingleMessage sends one message to one recipient
func (c Client) SingleMessage(m Message) (*MessageResponse, error) {
if err := m.Validate(); err != nil {
return nil, err
}
b, err := json.Marshal(m)
if err != nil {
return nil, err
}
resp := &MessageResponse{}
if err := c.defaultPostRequest(b, SingleMessagePath, resp); err != nil {
return nil, err
}
return resp, nil
}
// AdvancedMessage sends messages to the recipients
func (c Client) AdvancedMessage(m BulkMessage) (*MessageResponse, error) {
if err := m.Validate(); err != nil {
return nil, err
}
b, err := json.Marshal(m)
if err != nil {
return nil, err
}
resp := &MessageResponse{}
if err := c.defaultPostRequest(b, AdvancedMessagePath, resp); err != nil {
return nil, err
}
return resp, nil
}
// GetSMSStatus return sms status
func (c Client) GetSMSStatus(messageID string) (*MessageStatusWithID, error) {
params := url.Values{}
params.Add("messageId", messageID)
path := SMSStatusPath + "?" + params.Encode()
resp := &MessageStatusResponse{}
if err := c.defaultGetRequest(path, resp); err != nil {
return nil, err
}
if len(resp.Results) != 1 {
return nil, ErrSMSStatusNotFound
}
return &resp.Results[0], nil
}
func (c Client) defaultPostRequest(b []byte, path string, v interface{}) error {
req, err := http.NewRequest(http.MethodPost, c.BaseURL+path, bytes.NewBuffer(b))
if err != nil {
return err
}
c.setAuthentication(req)
req.Header.Add("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(v)
}
func (c Client) defaultGetRequest(path string, v interface{}) error {
req, err := http.NewRequest(http.MethodGet, c.BaseURL+path, nil)
if err != nil {
return err
}
c.setAuthentication(req)
req.Header.Add("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(v)
}
func (c Client) defaultDeleteRequest(path string) error {
req, err := http.NewRequest(http.MethodDelete, c.BaseURL+path, nil)
if err != nil {
return err
}
c.setAuthentication(req)
req.Header.Add("Content-Type", "application/json")
_, err = c.HTTPClient.Do(req)
return err
}
func (c Client) setAuthentication(req *http.Request) error {
if c.ApiKey != "" {
req.Header.Add("Authorization", fmt.Sprintf("App %s", c.ApiKey))
return nil
} else if c.Username != "" && c.Password != "" {
req.SetBasicAuth(c.Username, c.Password)
return nil
}
return ErrNoAuthentication
}