Skip to content

Commit 7fd2300

Browse files
author
Dirk Hoekstra
authored
Merge pull request #16 from messagebird/resource-listing
Allows the client to list all resources
2 parents f7108e2 + 9a7c47d commit 7fd2300

File tree

7 files changed

+523
-130
lines changed

7 files changed

+523
-130
lines changed

client.go

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,27 @@ import (
2222

2323
const (
2424
// ClientVersion is used in User-Agent request header to provide server with API level.
25-
ClientVersion = "4.1.0"
25+
ClientVersion = "4.2.0"
2626

2727
// Endpoint points you to MessageBird REST API.
2828
Endpoint = "https://rest.messagebird.com"
2929
)
3030

31+
const (
32+
// HLRPath represents the path to the HLR resource.
33+
HLRPath = "hlr"
34+
// MessagePath represents the path to the Message resource.
35+
MessagePath = "messages"
36+
// MMSPath represents the path to the MMS resource.
37+
MMSPath = "mms"
38+
// VoiceMessagePath represents the path to the VoiceMessage resource.
39+
VoiceMessagePath = "voicemessages"
40+
// VerifyPath represents the path to the Verify resource.
41+
VerifyPath = "verify"
42+
// LookupPath represents the path to the Lookup resource.
43+
LookupPath = "lookup"
44+
)
45+
3146
var (
3247
// ErrResponse is returned when we were able to contact API but request was not successful and contains error details.
3348
ErrResponse = errors.New("The MessageBird API returned an error")
@@ -140,7 +155,7 @@ func (c *Client) Balance() (*Balance, error) {
140155
// created by the NewHLR function.
141156
func (c *Client) HLR(id string) (*HLR, error) {
142157
hlr := &HLR{}
143-
if err := c.request(hlr, "hlr/"+id, nil); err != nil {
158+
if err := c.request(hlr, HLRPath+"/"+id, nil); err != nil {
144159
if err == ErrResponse {
145160
return hlr, err
146161
}
@@ -151,6 +166,21 @@ func (c *Client) HLR(id string) (*HLR, error) {
151166
return hlr, nil
152167
}
153168

169+
// HLRs lists all HLR objects that were previously created by the NewHLR
170+
// function.
171+
func (c *Client) HLRs() (*HLRList, error) {
172+
hlrList := &HLRList{}
173+
if err := c.request(hlrList, HLRPath, nil); err != nil {
174+
if err == ErrResponse {
175+
return hlrList, err
176+
}
177+
178+
return nil, err
179+
}
180+
181+
return hlrList, nil
182+
}
183+
154184
// NewHLR retrieves the information of an existing HLR.
155185
func (c *Client) NewHLR(msisdn, reference string) (*HLR, error) {
156186
params := &url.Values{
@@ -159,7 +189,7 @@ func (c *Client) NewHLR(msisdn, reference string) (*HLR, error) {
159189
}
160190

161191
hlr := &HLR{}
162-
if err := c.request(hlr, "hlr", params); err != nil {
192+
if err := c.request(hlr, HLRPath, params); err != nil {
163193
if err == ErrResponse {
164194
return hlr, err
165195
}
@@ -173,7 +203,7 @@ func (c *Client) NewHLR(msisdn, reference string) (*HLR, error) {
173203
// Message retrieves the information of an existing Message.
174204
func (c *Client) Message(id string) (*Message, error) {
175205
message := &Message{}
176-
if err := c.request(message, "messages/"+id, nil); err != nil {
206+
if err := c.request(message, MessagePath+"/"+id, nil); err != nil {
177207
if err == ErrResponse {
178208
return message, err
179209
}
@@ -184,6 +214,25 @@ func (c *Client) Message(id string) (*Message, error) {
184214
return message, nil
185215
}
186216

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+
187236
// NewMessage creates a new message for one or more recipients.
188237
func (c *Client) NewMessage(originator string, recipients []string, body string, msgParams *MessageParams) (*Message, error) {
189238
params, err := paramsForMessage(msgParams)
@@ -196,7 +245,7 @@ func (c *Client) NewMessage(originator string, recipients []string, body string,
196245
params.Set("recipients", strings.Join(recipients, ","))
197246

198247
message := &Message{}
199-
if err := c.request(message, "messages", params); err != nil {
248+
if err := c.request(message, MessagePath, params); err != nil {
200249
if err == ErrResponse {
201250
return message, err
202251
}
@@ -210,7 +259,7 @@ func (c *Client) NewMessage(originator string, recipients []string, body string,
210259
// MMSMessage retrieves the information of an existing MmsMessage.
211260
func (c *Client) MMSMessage(id string) (*MMSMessage, error) {
212261
mmsMessage := &MMSMessage{}
213-
if err := c.request(mmsMessage, "mms/"+id, nil); err != nil {
262+
if err := c.request(mmsMessage, MMSPath+"/"+id, nil); err != nil {
214263
if err == ErrResponse {
215264
return mmsMessage, err
216265
}
@@ -232,7 +281,7 @@ func (c *Client) NewMMSMessage(originator string, recipients []string, msgParams
232281
params.Set("recipients", strings.Join(recipients, ","))
233282

234283
mmsMessage := &MMSMessage{}
235-
if err := c.request(mmsMessage, "mms", params); err != nil {
284+
if err := c.request(mmsMessage, MMSPath, params); err != nil {
236285
if err == ErrResponse {
237286
return mmsMessage, err
238287
}
@@ -246,7 +295,7 @@ func (c *Client) NewMMSMessage(originator string, recipients []string, msgParams
246295
// VoiceMessage retrieves the information of an existing VoiceMessage.
247296
func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
248297
message := &VoiceMessage{}
249-
if err := c.request(message, "voicemessages/"+id, nil); err != nil {
298+
if err := c.request(message, VoiceMessagePath+"/"+id, nil); err != nil {
250299
if err == ErrResponse {
251300
return message, err
252301
}
@@ -257,14 +306,28 @@ func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
257306
return message, nil
258307
}
259308

309+
// VoiceMessages retrieves all VoiceMessages of the user.
310+
func (c *Client) VoiceMessages() (*VoiceMessageList, error) {
311+
messageList := &VoiceMessageList{}
312+
if err := c.request(messageList, VoiceMessagePath, nil); err != nil {
313+
if err == ErrResponse {
314+
return messageList, err
315+
}
316+
317+
return nil, err
318+
}
319+
320+
return messageList, nil
321+
}
322+
260323
// NewVoiceMessage creates a new voice message for one or more recipients.
261324
func (c *Client) NewVoiceMessage(recipients []string, body string, params *VoiceMessageParams) (*VoiceMessage, error) {
262325
urlParams := paramsForVoiceMessage(params)
263326
urlParams.Set("body", body)
264327
urlParams.Set("recipients", strings.Join(recipients, ","))
265328

266329
message := &VoiceMessage{}
267-
if err := c.request(message, "voicemessages", urlParams); err != nil {
330+
if err := c.request(message, VoiceMessagePath, urlParams); err != nil {
268331
if err == ErrResponse {
269332
return message, err
270333
}
@@ -281,7 +344,7 @@ func (c *Client) NewVerify(recipient string, params *VerifyParams) (*Verify, err
281344
urlParams.Set("recipient", recipient)
282345

283346
verify := &Verify{}
284-
if err := c.request(verify, "verify", urlParams); err != nil {
347+
if err := c.request(verify, VerifyPath, urlParams); err != nil {
285348
if err == ErrResponse {
286349
return verify, err
287350
}
@@ -297,7 +360,7 @@ func (c *Client) VerifyToken(id, token string) (*Verify, error) {
297360
params := &url.Values{}
298361
params.Set("token", token)
299362

300-
path := "verify/" + id + "?" + params.Encode()
363+
path := VerifyPath + "/" + id + "?" + params.Encode()
301364

302365
verify := &Verify{}
303366
if err := c.request(verify, path, nil); err != nil {
@@ -314,7 +377,7 @@ func (c *Client) VerifyToken(id, token string) (*Verify, error) {
314377
// Lookup performs a new lookup for the specified number.
315378
func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, error) {
316379
urlParams := paramsForLookup(params)
317-
path := "lookup/" + phoneNumber + "?" + urlParams.Encode()
380+
path := LookupPath + "/" + phoneNumber + "?" + urlParams.Encode()
318381

319382
lookup := &Lookup{}
320383
if err := c.request(lookup, path, nil); err != nil {
@@ -331,7 +394,7 @@ func (c *Client) Lookup(phoneNumber string, params *LookupParams) (*Lookup, erro
331394
// NewLookupHLR creates a new HLR lookup for the specified number.
332395
func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
333396
urlParams := paramsForLookup(params)
334-
path := "lookup/" + phoneNumber + "/hlr"
397+
path := LookupPath + "/" + phoneNumber + "/" + HLRPath
335398

336399
hlr := &HLR{}
337400
if err := c.request(hlr, path, urlParams); err != nil {
@@ -348,7 +411,7 @@ func (c *Client) NewLookupHLR(phoneNumber string, params *LookupParams) (*HLR, e
348411
// LookupHLR performs a HLR lookup for the specified number.
349412
func (c *Client) LookupHLR(phoneNumber string, params *LookupParams) (*HLR, error) {
350413
urlParams := paramsForLookup(params)
351-
path := "lookup/" + phoneNumber + "/hlr?" + urlParams.Encode()
414+
path := LookupPath + "/" + phoneNumber + "/" + HLRPath + "?" + urlParams.Encode()
352415

353416
hlr := &HLR{}
354417
if err := c.request(hlr, path, nil); err != nil {

hlr.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@ type HLR struct {
1616
StatusDatetime *time.Time
1717
Errors []Error
1818
}
19+
20+
// HLRList represents a list of HLR requests.
21+
type HLRList struct {
22+
Offset int
23+
Limit int
24+
Count int
25+
TotalCount int
26+
Links map[string]*string
27+
Items []HLR
28+
}

hlr_test.go

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package messagebird
22

33
import (
4+
"net/http"
45
"testing"
56
"time"
67
)
78

89
var hlrObject = []byte(`{
910
"id":"27978c50354a93ca0ca8de6h54340177",
10-
"href":"https:\/\/rest.messagebird.com\/hlr\/27978c50354a93ca0ca8de6h54340177",
11+
"href":"https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177",
1112
"msisdn":31612345678,
1213
"network":20406,
1314
"reference":"MyReference",
@@ -16,42 +17,77 @@ var hlrObject = []byte(`{
1617
"statusDatetime":"2015-01-04T13:14:09+00:00"
1718
}`)
1819

20+
var hlrListObject = []byte(`{
21+
"offset":0,
22+
"limit":20,
23+
"count":2,
24+
"totalCount":2,
25+
"links":{
26+
"first":"https://rest.messagebird.com/hlr/?offset=0",
27+
"previous":null,
28+
"next":null,
29+
"last":"https://rest.messagebird.com/hlr/?offset=0"
30+
},
31+
"items":[
32+
{
33+
"id":"27978c50354a93ca0ca8de6h54340177",
34+
"href":"https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177",
35+
"msisdn":31612345678,
36+
"network":20406,
37+
"reference":"MyReference",
38+
"status":"sent",
39+
"createdDatetime":"2015-01-04T13:14:08+00:00",
40+
"statusDatetime":"2015-01-04T13:14:09+00:00"
41+
},
42+
{
43+
"id":"27978c50354a93ca0ca8de6h54340177",
44+
"href":"https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177",
45+
"msisdn":31612345678,
46+
"network":20406,
47+
"reference":"MyReference",
48+
"status":"sent",
49+
"createdDatetime":"2015-01-04T13:14:08+00:00",
50+
"statusDatetime":"2015-01-04T13:14:09+00:00"
51+
}
52+
]
53+
}`)
54+
1955
func assertHLRObject(t *testing.T, hlr *HLR) {
2056
if hlr.ID != "27978c50354a93ca0ca8de6h54340177" {
21-
t.Errorf("Unexpected result for HLR Id: %s", hlr.ID)
57+
t.Errorf("Unexpected result for HLR Id: %s, expected: 27978c50354a93ca0ca8de6h54340177", hlr.ID)
2258
}
2359

2460
if hlr.HRef != "https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177" {
25-
t.Errorf("Unexpected HLR href: %s", hlr.HRef)
61+
t.Errorf("Unexpected HLR href: %s, expected: https://rest.messagebird.com/hlr/27978c50354a93ca0ca8de6h54340177", hlr.HRef)
2662
}
2763

2864
if hlr.MSISDN != 31612345678 {
29-
t.Errorf("Unexpected HLR msisdn: %d", hlr.MSISDN)
65+
t.Errorf("Unexpected HLR msisdn: %d, expected: 31612345678", hlr.MSISDN)
3066
}
3167

3268
if hlr.Network != 20406 {
33-
t.Errorf("Unexpected HLR network: %d", hlr.Network)
69+
t.Errorf("Unexpected HLR network: %d, expected: 20406", hlr.Network)
3470
}
3571

3672
if hlr.Reference != "MyReference" {
37-
t.Errorf("Unexpected HLR reference: %s", hlr.Reference)
73+
t.Errorf("Unexpected HLR reference: %s, expected: MyReference", hlr.Reference)
3874
}
3975

4076
if hlr.Status != "sent" {
41-
t.Errorf("Unexpected HLR status: %s", hlr.Status)
77+
t.Errorf("Unexpected HLR status: %s, expected: sent", hlr.Status)
4278
}
4379

4480
if hlr.CreatedDatetime == nil || hlr.CreatedDatetime.Format(time.RFC3339) != "2015-01-04T13:14:08Z" {
45-
t.Errorf("Unexpected HLR created datetime: %s", hlr.CreatedDatetime.Format(time.RFC3339))
81+
t.Errorf("Unexpected HLR created datetime: %s, expected: 2015-01-04T13:14:08Z", hlr.CreatedDatetime.Format(time.RFC3339))
4682
}
4783

4884
if hlr.StatusDatetime == nil || hlr.StatusDatetime.Format(time.RFC3339) != "2015-01-04T13:14:09Z" {
49-
t.Errorf("Unexpected HLR status datetime: %s", hlr.StatusDatetime.Format(time.RFC3339))
85+
t.Errorf("Unexpected HLR status datetime: %s, expected: 2015-01-04T13:14:09Z", hlr.StatusDatetime.Format(time.RFC3339))
5086
}
5187
}
5288

5389
func TestHLR(t *testing.T) {
54-
SetServerResponse(200, hlrObject)
90+
SetServerResponse(http.StatusOK, hlrObject)
5591

5692
hlr, err := mbClient.HLR("27978c50354a93ca0ca8de6h54340177")
5793
if err != nil {
@@ -62,7 +98,7 @@ func TestHLR(t *testing.T) {
6298
}
6399

64100
func TestNewHLR(t *testing.T) {
65-
SetServerResponse(200, hlrObject)
101+
SetServerResponse(http.StatusOK, hlrObject)
66102

67103
hlr, err := mbClient.NewHLR("31612345678", "MyReference")
68104
if err != nil {
@@ -73,22 +109,48 @@ func TestNewHLR(t *testing.T) {
73109
}
74110

75111
func TestHLRError(t *testing.T) {
76-
SetServerResponse(405, accessKeyErrorObject)
112+
SetServerResponse(http.StatusMethodNotAllowed, accessKeyErrorObject)
77113

78114
hlr, err := mbClient.HLR("dummy_hlr_id")
79115
if err != ErrResponse {
80116
t.Fatalf("Expected ErrResponse to be returned, instead I got %s", err)
81117
}
82118

83119
if len(hlr.Errors) != 1 {
84-
t.Fatalf("Unexpected number of errors: %d", len(hlr.Errors))
120+
t.Fatalf("Unexpected number of errors: %d, expected: 1", len(hlr.Errors))
85121
}
86122

87123
if hlr.Errors[0].Code != 2 {
88-
t.Errorf("Unexpected error code: %d", hlr.Errors[0].Code)
124+
t.Errorf("Unexpected error code: %d, expected: 2", hlr.Errors[0].Code)
89125
}
90126

91127
if hlr.Errors[0].Parameter != "access_key" {
92-
t.Errorf("Unexpected error parameter: %s", hlr.Errors[0].Parameter)
128+
t.Errorf("Unexpected error parameter: %s, expected: access_key", hlr.Errors[0].Parameter)
129+
}
130+
}
131+
132+
func TestHLRList(t *testing.T) {
133+
SetServerResponse(http.StatusOK, hlrListObject)
134+
135+
hlrList, err := mbClient.HLRs()
136+
if err != nil {
137+
t.Fatalf("Didn't expect an error while requesting HLRs: %s", err)
138+
}
139+
140+
if hlrList.Offset != 0 {
141+
t.Errorf("Unexpected result for the HLRList offset: %d, expected: 0", hlrList.Offset)
142+
}
143+
if hlrList.Limit != 20 {
144+
t.Errorf("Unexpected result for the HLRList limit: %d, expected: 20", hlrList.Limit)
145+
}
146+
if hlrList.Count != 2 {
147+
t.Errorf("Unexpected result for the HLRList count: %d, expected: 2", hlrList.Count)
148+
}
149+
if hlrList.TotalCount != 2 {
150+
t.Errorf("Unexpected result for the HLRList total count: %d, expected: 2", hlrList.TotalCount)
151+
}
152+
153+
for _, hlr := range hlrList.Items {
154+
assertHLRObject(t, &hlr)
93155
}
94156
}

0 commit comments

Comments
 (0)