Skip to content

Commit f414c29

Browse files
author
Dirk Hoekstra
committed
Add the listing of resources
1 parent a505e01 commit f414c29

File tree

7 files changed

+355
-22
lines changed

7 files changed

+355
-22
lines changed

client.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ func (c *Client) HLR(id string) (*HLR, error) {
140140
return hlr, nil
141141
}
142142

143+
// HLRs lists all HLR objects that were previously created by the NewHLR
144+
// function.
145+
func (c *Client) HLRs() (*HLRList, error) {
146+
hlrList := &HLRList{}
147+
if err := c.request(hlrList, "hlr", nil); err != nil {
148+
if err == ErrResponse {
149+
return hlrList, err
150+
}
151+
152+
return nil, err
153+
}
154+
155+
return hlrList, nil
156+
}
157+
143158
// NewHLR retrieves the information of an existing HLR.
144159
func (c *Client) NewHLR(msisdn, reference string) (*HLR, error) {
145160
params := &url.Values{
@@ -173,6 +188,20 @@ func (c *Client) Message(id string) (*Message, error) {
173188
return message, nil
174189
}
175190

191+
// Messages retrieves all messages of the user represented as a MessageList object.
192+
func (c *Client) Messages() (*MessageList, error) {
193+
messageList := &MessageList{}
194+
if err := c.request(messageList, "messages", nil); err != nil {
195+
if err == ErrResponse {
196+
return messageList, err
197+
}
198+
199+
return nil, err
200+
}
201+
202+
return messageList, nil
203+
}
204+
176205
// NewMessage creates a new message for one or more recipients.
177206
func (c *Client) NewMessage(originator string, recipients []string, body string, msgParams *MessageParams) (*Message, error) {
178207
params, err := paramsForMessage(msgParams)
@@ -210,6 +239,20 @@ func (c *Client) VoiceMessage(id string) (*VoiceMessage, error) {
210239
return message, nil
211240
}
212241

242+
// VoiceMessages retrieves all VoiceMessages of the user.
243+
func (c *Client) VoiceMessages() (*VoiceMessageList, error) {
244+
messageList := &VoiceMessageList{}
245+
if err := c.request(messageList, "voicemessages", nil); err != nil {
246+
if err == ErrResponse {
247+
return messageList, err
248+
}
249+
250+
return nil, err
251+
}
252+
253+
return messageList, nil
254+
}
255+
213256
// NewVoiceMessage creates a new voice message for one or more recipients.
214257
func (c *Client) NewVoiceMessage(recipients []string, body string, params *VoiceMessageParams) (*VoiceMessage, error) {
215258
urlParams := paramsForVoiceMessage(params)

hlr.go

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

hlr_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,40 @@ var hlrObject []byte = []byte(`{
1616
"statusDatetime":"2015-01-04T13:14:09+00:00"
1717
}`)
1818

19+
var hlrListObject = []byte(`{
20+
"offset": 0,
21+
"limit": 20,
22+
"count": 2,
23+
"totalCount": 2,
24+
"links": {
25+
"first": "https://rest.messagebird.com/hlr/?offset=0",
26+
"previous": null,
27+
"next": null,
28+
"last": "https://rest.messagebird.com/hlr/?offset=0"
29+
},
30+
"items": [
31+
{
32+
"id":"27978c50354a93ca0ca8de6h54340177",
33+
"href":"https:\/\/rest.messagebird.com\/hlr\/27978c50354a93ca0ca8de6h54340177",
34+
"msisdn":31612345678,
35+
"network":20406,
36+
"reference":"MyReference",
37+
"status":"sent",
38+
"createdDatetime":"2015-01-04T13:14:08+00:00",
39+
"statusDatetime":"2015-01-04T13:14:09+00:00"
40+
},
41+
{
42+
"id":"27978c50354a93ca0ca8de6h54340177",
43+
"href":"https:\/\/rest.messagebird.com\/hlr\/27978c50354a93ca0ca8de6h54340177",
44+
"msisdn":31612345678,
45+
"network":20406,
46+
"reference":"MyReference",
47+
"status":"sent",
48+
"createdDatetime":"2015-01-04T13:14:08+00:00",
49+
"statusDatetime":"2015-01-04T13:14:09+00:00"
50+
}]
51+
}`)
52+
1953
func assertHLRObject(t *testing.T, hlr *HLR) {
2054
if hlr.Id != "27978c50354a93ca0ca8de6h54340177" {
2155
t.Errorf("Unexpected result for HLR Id: %s", hlr.Id)
@@ -92,3 +126,29 @@ func TestHLRError(t *testing.T) {
92126
t.Errorf("Unexpected error parameter: %s", hlr.Errors[0].Parameter)
93127
}
94128
}
129+
130+
func TestHLRList(t *testing.T) {
131+
SetServerResponse(200, hlrListObject)
132+
133+
hlrList, err := mbClient.HLRs()
134+
if err != nil {
135+
t.Fatalf("Didn't expect an error while requesting HLRs: %s", err)
136+
}
137+
138+
if hlrList.Offset != 0 {
139+
t.Errorf("Unexpected result for the HLRList Offset: %d\n", hlrList.Offset)
140+
}
141+
if hlrList.Limit != 20 {
142+
t.Errorf("Unexpected result for the HLRList Limit: %d\n", hlrList.Limit)
143+
}
144+
if hlrList.Count != 2 {
145+
t.Errorf("Unexpected result for the HLRList Count: %d\n", hlrList.Count)
146+
}
147+
if hlrList.TotalCount != 2 {
148+
t.Errorf("Unexpected result for the HLRList TotalCount: %d\n", hlrList.TotalCount)
149+
}
150+
151+
for _, hlr := range hlrList.Items {
152+
assertHLRObject(t, &hlr)
153+
}
154+
}

message.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ type Message struct {
2828
Errors []Error
2929
}
3030

31+
// MessageList represents a list of Messages.
32+
type MessageList struct {
33+
Offset int
34+
Limit int
35+
Count int
36+
TotalCount int
37+
Links map[string]*string
38+
Items []Message
39+
}
40+
3141
type MessageParams struct {
3242
Type string
3343
Reference string

message_test.go

Lines changed: 113 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var messageObject []byte = []byte(`{
1616
"validity":null,
1717
"gateway":239,
1818
"typeDetails":{
19-
19+
2020
},
2121
"datacoding":"plain",
2222
"mclass":1,
@@ -37,14 +37,7 @@ var messageObject []byte = []byte(`{
3737
}
3838
}`)
3939

40-
func TestNewMessage(t *testing.T) {
41-
SetServerResponse(200, messageObject)
42-
43-
message, err := mbClient.NewMessage("TestName", []string{"31612345678"}, "Hello World", nil)
44-
if err != nil {
45-
t.Fatalf("Didn't expect error while creating a new message: %s", err)
46-
}
47-
40+
func assertMessageObject(t *testing.T, message *Message) {
4841
if message.Id != "6fe65f90454aa61536e6a88b88972670" {
4942
t.Errorf("Unexpected message id: %s", message.Id)
5043
}
@@ -78,7 +71,7 @@ func TestNewMessage(t *testing.T) {
7871
}
7972

8073
if message.Gateway != 239 {
81-
t.Errorf("Unexpected message gateway: %s", message.Gateway)
74+
t.Errorf("Unexpected message gateway: %d", message.Gateway)
8275
}
8376

8477
if len(message.TypeDetails) != 0 {
@@ -90,7 +83,7 @@ func TestNewMessage(t *testing.T) {
9083
}
9184

9285
if message.MClass != 1 {
93-
t.Errorf("Unexpected message mclass: %s", message.MClass)
86+
t.Errorf("Unexpected message mclass: %d", message.MClass)
9487
}
9588

9689
if message.ScheduledDatetime != nil {
@@ -126,6 +119,17 @@ func TestNewMessage(t *testing.T) {
126119
}
127120
}
128121

122+
func TestNewMessage(t *testing.T) {
123+
SetServerResponse(200, messageObject)
124+
125+
message, err := mbClient.NewMessage("TestName", []string{"31612345678"}, "Hello World", nil)
126+
if err != nil {
127+
t.Fatalf("Didn't expect error while creating a new message: %s", err)
128+
}
129+
130+
assertMessageObject(t, message)
131+
}
132+
129133
func TestNewMessageError(t *testing.T) {
130134
SetServerResponse(405, accessKeyErrorObject)
131135

@@ -158,7 +162,7 @@ var messageWithParamsObject []byte = []byte(`{
158162
"validity":13,
159163
"gateway":10,
160164
"typeDetails":{
161-
165+
162166
},
163167
"datacoding":"unicode",
164168
"mclass":1,
@@ -208,7 +212,7 @@ func TestNewMessageWithParams(t *testing.T) {
208212
}
209213

210214
if message.Gateway != 10 {
211-
t.Errorf("Unexpected message gateway: %s", message.Gateway)
215+
t.Errorf("Unexpected message gateway: %d", message.Gateway)
212216
}
213217

214218
if message.DataCoding != "unicode" {
@@ -400,7 +404,7 @@ var messageObjectWithCreatedDatetime []byte = []byte(`{
400404
"validity":null,
401405
"gateway":239,
402406
"typeDetails":{
403-
407+
404408
},
405409
"datacoding":"plain",
406410
"mclass":1,
@@ -456,3 +460,98 @@ func TestNewMessageWithScheduledDatetime(t *testing.T) {
456460
t.Errorf("Unexpected datetime status for message recipient: %s", message.Recipients.Items[0].StatusDatetime.Format(time.RFC3339))
457461
}
458462
}
463+
464+
var messageListObject = []byte(`{
465+
"offset": 0,
466+
"limit": 20,
467+
"count": 2,
468+
"totalCount": 2,
469+
"links": {
470+
"first": "https://rest.messagebird.com/messages/?offset=0",
471+
"previous": null,
472+
"next": null,
473+
"last": "https://rest.messagebird.com/messages/?offset=0"
474+
},
475+
"items": [
476+
{
477+
"id":"6fe65f90454aa61536e6a88b88972670",
478+
"href":"https:\/\/rest.messagebird.com\/messages\/6fe65f90454aa61536e6a88b88972670",
479+
"direction":"mt",
480+
"type":"sms",
481+
"originator":"TestName",
482+
"body":"Hello World",
483+
"reference":null,
484+
"validity":null,
485+
"gateway":239,
486+
"typeDetails":{ },
487+
"datacoding":"plain",
488+
"mclass":1,
489+
"scheduledDatetime":null,
490+
"createdDatetime":"2015-01-05T10:02:59+00:00",
491+
"recipients":{
492+
"totalCount":1,
493+
"totalSentCount":1,
494+
"totalDeliveredCount":0,
495+
"totalDeliveryFailedCount":0,
496+
"items":[{
497+
"recipient":31612345678,
498+
"status":"sent",
499+
"statusDatetime":"2015-01-05T10:02:59+00:00"
500+
}]
501+
}
502+
},
503+
{
504+
"id":"6fe65f90454aa61536e6a88b88972670",
505+
"href":"https:\/\/rest.messagebird.com\/messages\/6fe65f90454aa61536e6a88b88972670",
506+
"direction":"mt",
507+
"type":"sms",
508+
"originator":"TestName",
509+
"body":"Hello World",
510+
"reference":null,
511+
"validity":null,
512+
"gateway":239,
513+
"typeDetails":{ },
514+
"datacoding":"plain",
515+
"mclass":1,
516+
"scheduledDatetime":null,
517+
"createdDatetime":"2015-01-05T10:02:59+00:00",
518+
"recipients":{
519+
"totalCount":1,
520+
"totalSentCount":1,
521+
"totalDeliveredCount":0,
522+
"totalDeliveryFailedCount":0,
523+
"items":[{
524+
"recipient":31612345678,
525+
"status":"sent",
526+
"statusDatetime":"2015-01-05T10:02:59+00:00"
527+
}]
528+
}
529+
}
530+
]
531+
}`)
532+
533+
func TestMessageList(t *testing.T) {
534+
SetServerResponse(200, messageListObject)
535+
536+
messageList, err := mbClient.Messages()
537+
if err != nil {
538+
t.Fatalf("Didn't expect an error while requesting Messages: %s", err)
539+
}
540+
541+
if messageList.Offset != 0 {
542+
t.Errorf("Unexpected result for the MessageList Offset: %d\n", messageList.Offset)
543+
}
544+
if messageList.Limit != 20 {
545+
t.Errorf("Unexpected result for the MessageList Limit: %d\n", messageList.Limit)
546+
}
547+
if messageList.Count != 2 {
548+
t.Errorf("Unexpected result for the MessageList Count: %d\n", messageList.Count)
549+
}
550+
if messageList.TotalCount != 2 {
551+
t.Errorf("Unexpected result for the MessageList TotalCount: %d\n", messageList.TotalCount)
552+
}
553+
554+
for _, message := range messageList.Items {
555+
assertMessageObject(t, &message)
556+
}
557+
}

voice_message.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ type VoiceMessage struct {
2222
Errors []Error
2323
}
2424

25+
// VoiceMessageList represents a list of VoiceMessages.
26+
type VoiceMessageList struct {
27+
Offset int
28+
Limit int
29+
Count int
30+
TotalCount int
31+
Links map[string]*string
32+
Items []VoiceMessage
33+
}
34+
2535
type VoiceMessageParams struct {
2636
Originator string
2737
Reference string

0 commit comments

Comments
 (0)