Skip to content

Commit fadc42e

Browse files
improvement(conversation) allow pass option as nil to list all
1 parent 80dc21f commit fadc42e

File tree

3 files changed

+97
-18
lines changed

3 files changed

+97
-18
lines changed

conversation/conversation.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ 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)
32+
query := "?"
33+
if options != nil {
34+
query += paginationQuery(options)
35+
}
3336

3437
convList := &ConversationList{}
35-
if err := request(c, convList, http.MethodGet, path+"?"+query, nil); err != nil {
38+
if err := request(c, convList, http.MethodGet, path+query, nil); err != nil {
3639
return nil, err
3740
}
3841

conversation/conversation_test.go

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,57 @@ func TestMain(m *testing.M) {
1313
}
1414

1515
func TestList(t *testing.T) {
16-
mbtest.WillReturnTestdata(t, "conversationListObject.json", http.StatusOK)
17-
client := mbtest.Client(t)
16+
t.Run("limit_offset", func(t *testing.T) {
17+
mbtest.WillReturnTestdata(t, "conversationListObject.json", http.StatusOK)
18+
client := mbtest.Client(t)
1819

19-
convList, err := List(client, &ListOptions{10, 20})
20-
if err != nil {
21-
t.Fatalf("unexpected error listing Conversations: %s", err)
22-
}
20+
convList, err := List(client, &ListOptions{10, 20})
21+
if err != nil {
22+
t.Fatalf("unexpected error listing Conversations: %s", err)
23+
}
2324

24-
if convList.Offset != 20 {
25-
t.Fatalf("got %d, expected 20", convList.Offset)
26-
}
25+
if convList.Offset != 20 {
26+
t.Fatalf("got %d, expected 20", convList.Offset)
27+
}
2728

28-
if convList.Items[0].ID != "convid" {
29-
t.Fatalf("got %s, expected convid", convList.Items[0].ID)
30-
}
29+
if convList.Items[0].ID != "convid" {
30+
t.Fatalf("got %s, expected convid", convList.Items[0].ID)
31+
}
3132

32-
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/conversations")
33+
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/conversations")
3334

34-
if query := mbtest.Request.URL.RawQuery; query != "limit=10&offset=20" {
35-
t.Fatalf("got %s, expected limit=10&offset=20", query)
36-
}
35+
if query := mbtest.Request.URL.RawQuery; query != "limit=10&offset=20" {
36+
t.Fatalf("got %s, expected limit=10&offset=20", query)
37+
}
38+
})
39+
40+
t.Run("all", func(t *testing.T) {
41+
mbtest.WillReturnTestdata(t, "allConversationListObject.json", http.StatusOK)
42+
client := mbtest.Client(t)
43+
44+
convList, err := List(client, nil)
45+
if err != nil {
46+
t.Fatalf("unexpected error listing Conversations: %s", err)
47+
}
48+
49+
if convList.Offset != 0 {
50+
t.Fatalf("got offset=%d, expected 0", convList.Offset)
51+
}
52+
53+
if convList.Limit != 10 {
54+
t.Fatalf("got limit=%d, expected 10", convList.Limit)
55+
}
56+
57+
if convList.Items[0].ID != "convid" {
58+
t.Fatalf("got %s, expected convid", convList.Items[0].ID)
59+
}
60+
61+
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/conversations")
62+
63+
if query := mbtest.Request.URL.RawQuery; query != "" {
64+
t.Fatalf("got %s, expected no limit and offset", query)
65+
}
66+
})
3767
}
3868

3969
func TestRead(t *testing.T) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"offset": 0,
3+
"limit": 10,
4+
"count": 1,
5+
"totalCount": 1,
6+
"items": [
7+
{
8+
"id": "convid",
9+
"contactId": "contid",
10+
"contact": {
11+
"id": "contid",
12+
"href": "https://chat.messagebird.com/1/contacts/contid",
13+
"msisdn": 31612345678,
14+
"firstName": "Foo",
15+
"lastName": "Bar",
16+
"customDetails": {
17+
"avatar": "https://s3-eu-west-1.amazonaws.com/messagebird-chat/telegram/0d1dae7t5b7d7eb4531c14n04328336/6de655at5b7d859309f821n60607065/d0b705dt5b7d859309f987n05372263.jpg",
18+
"firstName": "Foo",
19+
"lastName": "Bar",
20+
"userId": 12345678
21+
},
22+
"createdDatetime": "2018-08-22T15:47:32Z",
23+
"updatedDatetime": null
24+
},
25+
"channels": [
26+
{
27+
"id": "chid",
28+
"name": "TestChannel",
29+
"platformId": "telegram",
30+
"status": "active",
31+
"createdDatetime": "2018-08-22T15:18:11Z",
32+
"updatedDatetime": "2018-08-22T15:18:13Z"
33+
}
34+
],
35+
"status": "active",
36+
"createdDatetime": "2018-08-22T15:47:34Z",
37+
"updatedDatetime": "2018-08-24T09:49:01Z",
38+
"lastReceivedDatetime": "2018-08-24T09:49:01Z",
39+
"lastUsedChannelId": "chid",
40+
"messages": {
41+
"totalCount": 3,
42+
"href": "https://conversations.messagebird.com/v1/conversations/convid/messages"
43+
}
44+
}
45+
]
46+
}

0 commit comments

Comments
 (0)