Skip to content

Commit 430d19f

Browse files
committed
Add proper query string for listing endpoints
1 parent dd7ec65 commit 430d19f

File tree

7 files changed

+32
-4
lines changed

7 files changed

+32
-4
lines changed

conversation/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package conversation
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/url"
7+
"strconv"
68
"time"
79

810
messagebird "github.com/messagebird/go-rest-api"
@@ -206,6 +208,15 @@ func request(c *messagebird.Client, v interface{}, method, path string, data int
206208
return c.Request(v, method, fmt.Sprintf("%s/%s", apiRoot, path), data)
207209
}
208210

211+
// paginationQuery builds the query string for paginated endpoints.
212+
func paginationQuery(options *ListOptions) string {
213+
query := url.Values{}
214+
query.Set("limit", strconv.Itoa(options.Limit))
215+
query.Set("offset", strconv.Itoa(options.Offset))
216+
217+
return query.Encode()
218+
}
219+
209220
// UnmarshalJSON is used to unmarshal the MSISDN to a string rather than an
210221
// int64. The API returns integers, but this client always uses strings.
211222
// Exposing a json.Number doesn't seem nice.

conversation/conversation.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ var DefaultListOptions = &ListOptions{10, 0}
2929

3030
// List gets a collection of Conversations. Pagination can be set in options.
3131
func List(c *messagebird.Client, options *ListOptions) (*ConversationList, error) {
32+
query := paginationQuery(options)
33+
3234
convList := &ConversationList{}
33-
if err := request(c, convList, http.MethodGet, path, options); err != nil {
35+
if err := request(c, convList, http.MethodGet, path+"?"+query, nil); err != nil {
3436
return nil, err
3537
}
3638

conversation/conversation_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func TestList(t *testing.T) {
3030
}
3131

3232
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/conversations")
33+
34+
if query := mbtest.Request.URL.RawQuery; query != "limit=10&offset=20" {
35+
t.Fatalf("got %s, expected limit=10&offset=20", query)
36+
}
3337
}
3438

3539
func TestRead(t *testing.T) {

conversation/message.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ func CreateMessage(c *messagebird.Client, conversationID string, req *MessageCre
2929
// ListMessages gets a collection of messages from a conversation. Pagination
3030
// can be set in the options.
3131
func ListMessages(c *messagebird.Client, conversationID string, options *ListOptions) (*MessageList, error) {
32-
uri := fmt.Sprintf("%s/%s/%s", path, conversationID, messagesPath)
32+
query := paginationQuery(options)
33+
uri := fmt.Sprintf("%s/%s/%s?%s", path, conversationID, messagesPath, query)
3334

3435
messageList := &MessageList{}
35-
if err := request(c, messageList, http.MethodGet, uri, options); err != nil {
36+
if err := request(c, messageList, http.MethodGet, uri, nil); err != nil {
3637
return nil, err
3738
}
3839

conversation/message_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func TestListMessages(t *testing.T) {
4848
}
4949

5050
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/conversations/convid/messages")
51+
52+
if query := mbtest.Request.URL.RawQuery; query != "limit=10&offset=0" {
53+
t.Fatalf("got %s, expected limit=10&offset=0", query)
54+
}
5155
}
5256

5357
func TestReadMessage(t *testing.T) {

conversation/webhook.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ func DeleteWebhook(c *messagebird.Client, id string) error {
3131

3232
// ListWebhooks gets a collection of webhooks. Pagination can be set in options.
3333
func ListWebhooks(c *messagebird.Client, options *ListOptions) (*WebhookList, error) {
34+
query := paginationQuery(options)
35+
3436
webhookList := &WebhookList{}
35-
if err := request(c, webhookList, http.MethodGet, webhooksPath, options); err != nil {
37+
if err := request(c, webhookList, http.MethodGet, webhooksPath+"?"+query, nil); err != nil {
3638
return nil, err
3739
}
3840

conversation/webhook_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ func TestListWebhooks(t *testing.T) {
6060
}
6161

6262
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/webhooks")
63+
64+
if query := mbtest.Request.URL.RawQuery; query != "limit=10&offset=0" {
65+
t.Fatalf("got %s, expected limit=10&offset=0", query)
66+
}
6367
}
6468

6569
func TestReadWebhook(t *testing.T) {

0 commit comments

Comments
 (0)