Skip to content

Commit 424732d

Browse files
authored
feat: Add Limit Query Parameter to Get Follower Ids (#363)
* feat: Add limit to query follower ids * refactor: Extract query bind to bindToQuery func
1 parent 62e2928 commit 424732d

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

linebot/get_follower_ids.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package linebot
1717
import (
1818
"context"
1919
"net/url"
20+
"strconv"
2021
)
2122

2223
// GetFollowerIDs method
@@ -34,6 +35,7 @@ type GetFollowerIDsCall struct {
3435
ctx context.Context
3536

3637
continuationToken string
38+
limit uint16
3739
}
3840

3941
// WithContext method
@@ -42,12 +44,16 @@ func (call *GetFollowerIDsCall) WithContext(ctx context.Context) *GetFollowerIDs
4244
return call
4345
}
4446

47+
// WithLimit will set limit parmeter on query.
48+
// The limit can be a maximum of 1000 for a single request.
49+
func (call *GetFollowerIDsCall) WithLimit(limit uint16) *GetFollowerIDsCall {
50+
call.bindLimit(limit)
51+
return call
52+
}
53+
4554
// Do method
4655
func (call *GetFollowerIDsCall) Do() (*UserIDsResponse, error) {
47-
var q url.Values
48-
if call.continuationToken != "" {
49-
q = url.Values{"start": []string{call.continuationToken}}
50-
}
56+
q := call.bindToQuery()
5157
res, err := call.c.get(call.ctx, call.c.endpointBase, APIEndpointGetFollowerIDs, q)
5258
if err != nil {
5359
return nil, err
@@ -75,6 +81,27 @@ func (call *GetFollowerIDsCall) NewScanner() *UserIDsScanner {
7581
}
7682
}
7783

84+
func (call *GetFollowerIDsCall) bindLimit(limit uint16) {
85+
if limit > 1000 {
86+
limit = 1000
87+
}
88+
if limit == 0 {
89+
limit = 300
90+
}
91+
call.limit = limit
92+
}
93+
94+
func (call *GetFollowerIDsCall) bindToQuery() url.Values {
95+
q := make(url.Values)
96+
if call.continuationToken != "" {
97+
q.Set("start", call.continuationToken)
98+
}
99+
if call.limit != 0 {
100+
q.Set("limit", strconv.FormatUint(uint64(call.limit), 10))
101+
}
102+
return q
103+
}
104+
78105
func (call *GetFollowerIDsCall) setContinuationToken(token string) {
79106
call.continuationToken = token
80107
}

linebot/get_follower_ids_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestGetFollowerIDs(t *testing.T) {
3131
type want struct {
3232
URLPath string
3333
ContinuationToken string
34+
Limit string
3435
RequestBody []byte
3536
Response *UserIDsResponse
3637
Error error
@@ -39,6 +40,7 @@ func TestGetFollowerIDs(t *testing.T) {
3940
Label string
4041
GroupID string
4142
ContinuationToken string
43+
Limit uint16
4244
ResponseCode int
4345
Response []byte
4446
Want want
@@ -77,6 +79,24 @@ func TestGetFollowerIDs(t *testing.T) {
7779
},
7880
},
7981
},
82+
{
83+
Label: "With Limit",
84+
GroupID: "cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
85+
Limit: 1,
86+
ResponseCode: 200,
87+
Response: []byte(`{"userIds": ["U0047556f2e40dba2456887320ba7c76d"], "next": "xxxxx"}`),
88+
Want: want{
89+
URLPath: APIEndpointGetFollowerIDs,
90+
Limit: "1",
91+
RequestBody: []byte(""),
92+
Response: &UserIDsResponse{
93+
UserIDs: []string{
94+
"U0047556f2e40dba2456887320ba7c76d",
95+
},
96+
Next: "xxxxx",
97+
},
98+
},
99+
},
80100
{
81101
Label: "Internal server error",
82102
GroupID: "cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
@@ -108,6 +128,9 @@ func TestGetFollowerIDs(t *testing.T) {
108128
if start, want := q.Get("start"), tc.Want.ContinuationToken; start != want {
109129
t.Errorf("ContinuationToken: %s; want %s", start, want)
110130
}
131+
if limit, want := q.Get("limit"), tc.Want.Limit; limit != want {
132+
t.Errorf("Limit: %s; want %s", limit, want)
133+
}
111134
body, err := io.ReadAll(r.Body)
112135
if err != nil {
113136
t.Fatal(err)
@@ -135,7 +158,11 @@ func TestGetFollowerIDs(t *testing.T) {
135158
for i, tc := range testCases {
136159
currentTestIdx = i
137160
t.Run(strconv.Itoa(i)+"/"+tc.Label, func(t *testing.T) {
138-
res, err := client.GetFollowerIDs(tc.ContinuationToken).Do()
161+
call := client.GetFollowerIDs(tc.ContinuationToken)
162+
if tc.Limit != 0 {
163+
call.WithLimit(tc.Limit)
164+
}
165+
res, err := call.Do()
139166
if tc.Want.Error != nil {
140167
if !reflect.DeepEqual(err, tc.Want.Error) {
141168
t.Errorf("Error %v; want %v", err, tc.Want.Error)

0 commit comments

Comments
 (0)