From ff2765261879db39a90d2688916f3c4881958dd9 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 24 Sep 2021 04:43:44 +0000 Subject: [PATCH] [Section] Pagination support --- client/client.go | 6 ++++++ client/section.go | 30 +++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/client/client.go b/client/client.go index 819c6d7..d636a75 100644 --- a/client/client.go +++ b/client/client.go @@ -26,6 +26,12 @@ type emptyResponse struct { Data interface{} `json:"data"` } +type nextPage struct { + Offset string `json:"offset"` + Path string `json:"path"` + URI string `json:"uri"` +} + func NewClient(token string) *Client { c := &Client{ client: &http.Client{}, diff --git a/client/section.go b/client/section.go index b75c4a2..2068789 100644 --- a/client/section.go +++ b/client/section.go @@ -1,6 +1,7 @@ package client import "fmt" +import "net/url" type Section struct { GID string `json:"gid"` @@ -8,7 +9,8 @@ type Section struct { } type sectionsResponse struct { - Data []*Section `json:"data"` + Data []*Section `json:"data"` + NextPage *nextPage `json:"next_page"` } type sectionAddTaskData struct { @@ -20,14 +22,28 @@ type sectionAddTaskRequest struct { } func (wc *WorkspaceClient) GetSections(project *Project) ([]*Section, error) { - // TODO: Handle pagination path := fmt.Sprintf("projects/%s/sections", project.GID) - resp := §ionsResponse{} - err := wc.client.get(path, nil, resp) - if err != nil { - return nil, err + values := &url.Values{} + + ret := []*Section{} + + for { + resp := §ionsResponse{} + err := wc.client.get(path, values, resp) + if err != nil { + return nil, err + } + + ret = append(ret, resp.Data...) + + if resp.NextPage == nil { + break + } + + values.Set("offset", resp.NextPage.Offset) } - return resp.Data, nil + + return ret, nil } func (wc *WorkspaceClient) GetSectionsByName(project *Project) (map[string]*Section, error) {