[Section] Pagination support

This commit is contained in:
Ian Gulliver
2021-09-24 04:43:44 +00:00
parent 1a86bcce4d
commit ff27652618
2 changed files with 29 additions and 7 deletions

View File

@@ -26,6 +26,12 @@ type emptyResponse struct {
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
type nextPage struct {
Offset string `json:"offset"`
Path string `json:"path"`
URI string `json:"uri"`
}
func NewClient(token string) *Client { func NewClient(token string) *Client {
c := &Client{ c := &Client{
client: &http.Client{}, client: &http.Client{},

View File

@@ -1,6 +1,7 @@
package client package client
import "fmt" import "fmt"
import "net/url"
type Section struct { type Section struct {
GID string `json:"gid"` GID string `json:"gid"`
@@ -8,7 +9,8 @@ type Section struct {
} }
type sectionsResponse struct { type sectionsResponse struct {
Data []*Section `json:"data"` Data []*Section `json:"data"`
NextPage *nextPage `json:"next_page"`
} }
type sectionAddTaskData struct { type sectionAddTaskData struct {
@@ -20,14 +22,28 @@ type sectionAddTaskRequest struct {
} }
func (wc *WorkspaceClient) GetSections(project *Project) ([]*Section, error) { func (wc *WorkspaceClient) GetSections(project *Project) ([]*Section, error) {
// TODO: Handle pagination
path := fmt.Sprintf("projects/%s/sections", project.GID) path := fmt.Sprintf("projects/%s/sections", project.GID)
resp := &sectionsResponse{} values := &url.Values{}
err := wc.client.get(path, nil, resp)
if err != nil { ret := []*Section{}
return nil, err
for {
resp := &sectionsResponse{}
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) { func (wc *WorkspaceClient) GetSectionsByName(project *Project) (map[string]*Section, error) {