Skip to content

Commit a90dd71

Browse files
author
Dirk Hoekstra
committed
Merge remote-tracking branch 'timchunght/master' into resource-listing
2 parents 7b880ae + 7d45807 commit a90dd71

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,14 @@ func (c *Client) Message(id string) (*Message, error) {
213213
}
214214

215215
// Messages retrieves all messages of the user represented as a MessageList object.
216-
func (c *Client) Messages() (*MessageList, error) {
216+
func (c *Client) Messages(msgListParams *MessageListParams) (*MessageList, error) {
217217
messageList := &MessageList{}
218-
if err := c.request(messageList, MessagePath, nil); err != nil {
218+
params, err := paramsForMessageList(msgListParams)
219+
if err != nil {
220+
return messageList, err
221+
}
222+
223+
if err := c.request(messageList, "messages?"+params.Encode(), nil); err != nil {
219224
if err == ErrResponse {
220225
return messageList, err
221226
}

message.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ type MessageParams struct {
5252
ScheduledDatetime time.Time
5353
}
5454

55+
type MessageListParams struct {
56+
Originator string
57+
Direction string
58+
Limit int
59+
Offset int
60+
}
61+
5562
// paramsForMessage converts the specified MessageParams struct to a
5663
// url.Values pointer and returns it.
5764
func paramsForMessage(params *MessageParams) (*url.Values, error) {
@@ -96,3 +103,26 @@ func paramsForMessage(params *MessageParams) (*url.Values, error) {
96103

97104
return urlParams, nil
98105
}
106+
107+
// paramsForMessageList converts the specified MessageListParams struct to a
108+
// url.Values pointer and returns it.
109+
func paramsForMessageList(params *MessageListParams) (*url.Values, error) {
110+
urlParams := &url.Values{}
111+
112+
if params == nil {
113+
return urlParams, nil
114+
}
115+
116+
if params.Direction != "" {
117+
urlParams.Set("direction", params.Direction)
118+
}
119+
if params.Originator != "" {
120+
urlParams.Set("originator", params.Originator)
121+
}
122+
if params.Limit != 0 {
123+
urlParams.Set("limit", strconv.Itoa(params.Limit))
124+
}
125+
urlParams.Set("offset", strconv.Itoa(params.Offset))
126+
127+
return urlParams, nil
128+
}

message_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ var messageListObject = []byte(`{
542542
func TestMessageList(t *testing.T) {
543543
SetServerResponse(http.StatusOK, messageListObject)
544544

545-
messageList, err := mbClient.Messages()
545+
messageList, err := mbClient.Messages(nil)
546546
if err != nil {
547547
t.Fatalf("Didn't expect an error while requesting Messages: %s", err)
548548
}

0 commit comments

Comments
 (0)