@@ -18,16 +18,32 @@ import (
18
18
"net/http"
19
19
"net/url"
20
20
"runtime"
21
+ "strings"
21
22
)
22
23
23
24
const (
24
25
// ClientVersion is used in User-Agent request header to provide server with API level.
25
- ClientVersion = "3.0 .0"
26
+ ClientVersion = "4.2 .0"
26
27
27
28
// Endpoint points you to MessageBird REST API.
28
29
Endpoint = "https://rest.messagebird.com"
29
30
)
30
31
32
+ const (
33
+ // HLRPath represents the path to the HLR resource.
34
+ HLRPath = "hlr"
35
+ // MessagePath represents the path to the Message resource.
36
+ MessagePath = "messages"
37
+ // MMSPath represents the path to the MMS resource.
38
+ MMSPath = "mms"
39
+ // VoiceMessagePath represents the path to the VoiceMessage resource.
40
+ VoiceMessagePath = "voicemessages"
41
+ // VerifyPath represents the path to the Verify resource.
42
+ VerifyPath = "verify"
43
+ // LookupPath represents the path to the Lookup resource.
44
+ LookupPath = "lookup"
45
+ )
46
+
31
47
var (
32
48
// ErrResponse is returned when we were able to contact API but request was not successful and contains error details.
33
49
ErrResponse = errors .New ("The MessageBird API returned an error" )
@@ -138,7 +154,7 @@ func (c *Client) Balance() (*Balance, error) {
138
154
// created by the NewHLR function.
139
155
func (c * Client ) HLR (id string ) (* HLR , error ) {
140
156
hlr := & HLR {}
141
- if err := c .request (hlr , "hlr /"+ id , nil ); err != nil {
157
+ if err := c .request (hlr , HLRPath + " /"+ id , nil ); err != nil {
142
158
if err == ErrResponse {
143
159
return hlr , err
144
160
}
@@ -149,6 +165,21 @@ func (c *Client) HLR(id string) (*HLR, error) {
149
165
return hlr , nil
150
166
}
151
167
168
+ // HLRs lists all HLR objects that were previously created by the NewHLR
169
+ // function.
170
+ func (c * Client ) HLRs () (* HLRList , error ) {
171
+ hlrList := & HLRList {}
172
+ if err := c .request (hlrList , HLRPath , nil ); err != nil {
173
+ if err == ErrResponse {
174
+ return hlrList , err
175
+ }
176
+
177
+ return nil , err
178
+ }
179
+
180
+ return hlrList , nil
181
+ }
182
+
152
183
// NewHLR retrieves the information of an existing HLR.
153
184
func (c * Client ) NewHLR (msisdn string , reference string ) (* HLR , error ) {
154
185
requestData , err := requestDataForHLR (msisdn , reference )
@@ -157,7 +188,8 @@ func (c *Client) NewHLR(msisdn string, reference string) (*HLR, error) {
157
188
}
158
189
159
190
hlr := & HLR {}
160
- if err := c .request (hlr , "hlr" , requestData ); err != nil {
191
+
192
+ if err := c .request (hlr , HLRPath , requestData ); err != nil {
161
193
if err == ErrResponse {
162
194
return hlr , err
163
195
}
@@ -171,7 +203,7 @@ func (c *Client) NewHLR(msisdn string, reference string) (*HLR, error) {
171
203
// Message retrieves the information of an existing Message.
172
204
func (c * Client ) Message (id string ) (* Message , error ) {
173
205
message := & Message {}
174
- if err := c .request (message , "messages /"+ id , nil ); err != nil {
206
+ if err := c .request (message , MessagePath + " /"+ id , nil ); err != nil {
175
207
if err == ErrResponse {
176
208
return message , err
177
209
}
@@ -182,6 +214,25 @@ func (c *Client) Message(id string) (*Message, error) {
182
214
return message , nil
183
215
}
184
216
217
+ // Messages retrieves all messages of the user represented as a MessageList object.
218
+ func (c * Client ) Messages (msgListParams * MessageListParams ) (* MessageList , error ) {
219
+ messageList := & MessageList {}
220
+ params , err := paramsForMessageList (msgListParams )
221
+ if err != nil {
222
+ return messageList , err
223
+ }
224
+
225
+ if err := c .request (messageList , MessagePath + "?" + params .Encode (), nil ); err != nil {
226
+ if err == ErrResponse {
227
+ return messageList , err
228
+ }
229
+
230
+ return nil , err
231
+ }
232
+
233
+ return messageList , nil
234
+ }
235
+
185
236
// NewMessage creates a new message for one or more recipients.
186
237
func (c * Client ) NewMessage (originator string , recipients []string , body string , msgParams * MessageParams ) (* Message , error ) {
187
238
requestData , err := requestDataForMessage (originator , recipients , body , msgParams )
@@ -190,7 +241,7 @@ func (c *Client) NewMessage(originator string, recipients []string, body string,
190
241
}
191
242
192
243
message := & Message {}
193
- if err := c .request (message , "messages" , requestData ); err != nil {
244
+ if err := c .request (message , MessagePath , requestData ); err != nil {
194
245
if err == ErrResponse {
195
246
return message , err
196
247
}
@@ -201,10 +252,46 @@ func (c *Client) NewMessage(originator string, recipients []string, body string,
201
252
return message , nil
202
253
}
203
254
255
+ // MMSMessage retrieves the information of an existing MmsMessage.
256
+ func (c * Client ) MMSMessage (id string ) (* MMSMessage , error ) {
257
+ mmsMessage := & MMSMessage {}
258
+ if err := c .request (mmsMessage , MMSPath + "/" + id , nil ); err != nil {
259
+ if err == ErrResponse {
260
+ return mmsMessage , err
261
+ }
262
+
263
+ return nil , err
264
+ }
265
+
266
+ return mmsMessage , nil
267
+ }
268
+
269
+ // NewMMSMessage creates a new MMS message for one or more recipients.
270
+ func (c * Client ) NewMMSMessage (originator string , recipients []string , msgParams * MMSMessageParams ) (* MMSMessage , error ) {
271
+ params , err := paramsForMMSMessage (msgParams )
272
+ if err != nil {
273
+ return nil , err
274
+ }
275
+
276
+ params .Set ("originator" , originator )
277
+ params .Set ("recipients" , strings .Join (recipients , "," ))
278
+
279
+ mmsMessage := & MMSMessage {}
280
+ if err := c .request (mmsMessage , MMSPath , params ); err != nil {
281
+ if err == ErrResponse {
282
+ return mmsMessage , err
283
+ }
284
+
285
+ return nil , err
286
+ }
287
+
288
+ return mmsMessage , nil
289
+ }
290
+
204
291
// VoiceMessage retrieves the information of an existing VoiceMessage.
205
292
func (c * Client ) VoiceMessage (id string ) (* VoiceMessage , error ) {
206
293
message := & VoiceMessage {}
207
- if err := c .request (message , "voicemessages /"+ id , nil ); err != nil {
294
+ if err := c .request (message , VoiceMessagePath + " /"+ id , nil ); err != nil {
208
295
if err == ErrResponse {
209
296
return message , err
210
297
}
@@ -215,6 +302,20 @@ func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
215
302
return message , nil
216
303
}
217
304
305
+ // VoiceMessages retrieves all VoiceMessages of the user.
306
+ func (c * Client ) VoiceMessages () (* VoiceMessageList , error ) {
307
+ messageList := & VoiceMessageList {}
308
+ if err := c .request (messageList , VoiceMessagePath , nil ); err != nil {
309
+ if err == ErrResponse {
310
+ return messageList , err
311
+ }
312
+
313
+ return nil , err
314
+ }
315
+
316
+ return messageList , nil
317
+ }
318
+
218
319
// NewVoiceMessage creates a new voice message for one or more recipients.
219
320
func (c * Client ) NewVoiceMessage (recipients []string , body string , params * VoiceMessageParams ) (* VoiceMessage , error ) {
220
321
requestData , err := requestDataForVoiceMessage (recipients , body , params )
@@ -223,7 +324,7 @@ func (c *Client) NewVoiceMessage(recipients []string, body string, params *Voice
223
324
}
224
325
225
326
message := & VoiceMessage {}
226
- if err := c .request (message , "voicemessages" , requestData ); err != nil {
327
+ if err := c .request (message , VoiceMessagePath , requestData ); err != nil {
227
328
if err == ErrResponse {
228
329
return message , err
229
330
}
@@ -242,7 +343,7 @@ func (c *Client) NewVerify(recipient string, params *VerifyParams) (*Verify, err
242
343
}
243
344
244
345
verify := & Verify {}
245
- if err := c .request (verify , "verify" , requestData ); err != nil {
346
+ if err := c .request (verify , VerifyPath , requestData ); err != nil {
246
347
if err == ErrResponse {
247
348
return verify , err
248
349
}
@@ -258,7 +359,7 @@ func (c *Client) VerifyToken(id, token string) (*Verify, error) {
258
359
params := & url.Values {}
259
360
params .Set ("token" , token )
260
361
261
- path := "verify /" + id + "?" + params .Encode ()
362
+ path := VerifyPath + " /" + id + "?" + params .Encode ()
262
363
263
364
verify := & Verify {}
264
365
if err := c .request (verify , path , nil ); err != nil {
@@ -275,7 +376,7 @@ func (c *Client) VerifyToken(id, token string) (*Verify, error) {
275
376
// Lookup performs a new lookup for the specified number.
276
377
func (c * Client ) Lookup (phoneNumber string , params * LookupParams ) (* Lookup , error ) {
277
378
urlParams := paramsForLookup (params )
278
- path := "lookup /" + phoneNumber + "?" + urlParams .Encode ()
379
+ path := LookupPath + " /" + phoneNumber + "?" + urlParams .Encode ()
279
380
280
381
lookup := & Lookup {}
281
382
if err := c .request (lookup , path , nil ); err != nil {
@@ -292,7 +393,7 @@ func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, erro
292
393
// NewLookupHLR creates a new HLR lookup for the specified number.
293
394
func (c * Client ) NewLookupHLR (phoneNumber string , params * LookupParams ) (* HLR , error ) {
294
395
requestData := requestDataForLookup (params )
295
- path := "lookup /" + phoneNumber + "/hlr"
396
+ path := LookupPath + " /" + phoneNumber + "/" + HLRPath
296
397
297
398
hlr := & HLR {}
298
399
if err := c .request (hlr , path , requestData ); err != nil {
@@ -309,7 +410,7 @@ func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, e
309
410
// LookupHLR performs a HLR lookup for the specified number.
310
411
func (c * Client ) LookupHLR (phoneNumber string , params * LookupParams ) (* HLR , error ) {
311
412
urlParams := paramsForLookup (params )
312
- path := "lookup /" + phoneNumber + "/hlr ?" + urlParams .Encode ()
413
+ path := LookupPath + " /" + phoneNumber + "/" + HLRPath + " ?" + urlParams .Encode ()
313
414
314
415
hlr := & HLR {}
315
416
if err := c .request (hlr , path , nil ); err != nil {
0 commit comments