Skip to content

Commit 584c8d0

Browse files
authored
limits the number of pages for a request for a pagination (ktrysmt#199)
* limits the number of pages for a request for a pagination * for easier to read Co-authored-by: Alexey Voronkov <[email protected]>
1 parent 23a3800 commit 584c8d0

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

client.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
)
2222

2323
const DEFAULT_PAGE_LENGTH = 10
24+
const DEFAULT_LIMIT_PAGES = 0
2425
const DEFAULT_MAX_DEPTH = 1
2526
const DEFAULT_BITBUCKET_API_BASE_URL = "https://api.bitbucket.org/2.0"
2627

@@ -42,6 +43,9 @@ type Client struct {
4243
Workspaces *Workspace
4344
Pagelen int
4445
MaxDepth int
46+
// LimitPages limits the number of pages for a request
47+
// default value as 0 -- disable limits
48+
LimitPages int
4549
// DisableAutoPaging allows you to disable the default behavior of automatically requesting
4650
// all the pages for a paginated response.
4751
DisableAutoPaging bool
@@ -150,7 +154,8 @@ func injectClient(a *auth) *Client {
150154
if err != nil {
151155
log.Fatalf("invalid bitbucket url")
152156
}
153-
c := &Client{Auth: a, Pagelen: DEFAULT_PAGE_LENGTH, MaxDepth: DEFAULT_MAX_DEPTH, apiBaseURL: bitbucketUrl}
157+
c := &Client{Auth: a, Pagelen: DEFAULT_PAGE_LENGTH, MaxDepth: DEFAULT_MAX_DEPTH,
158+
apiBaseURL: bitbucketUrl, LimitPages: DEFAULT_LIMIT_PAGES}
154159
c.Repositories = &Repositories{
155160
c: c,
156161
PullRequests: &PullRequests{c: c},
@@ -349,12 +354,16 @@ func (c *Client) doPaginatedRequest(req *http.Request, emptyResponse bool) (inte
349354
}
350355

351356
responsePaginated := &Response{}
357+
var curPage int
358+
352359
err = json.Unmarshal(responseBytes, responsePaginated)
353360
if err == nil && len(responsePaginated.Values) > 0 {
354361
var values []interface{}
355362
for {
363+
curPage++
356364
values = append(values, responsePaginated.Values...)
357-
if c.DisableAutoPaging || len(responsePaginated.Next) == 0 {
365+
if c.DisableAutoPaging || len(responsePaginated.Next) == 0 ||
366+
(curPage >= c.LimitPages && c.LimitPages != 0) {
358367
break
359368
}
360369
newReq, err := http.NewRequest(req.Method, responsePaginated.Next, nil)

0 commit comments

Comments
 (0)