[Workspace] Pagination support

This commit is contained in:
Ian Gulliver
2021-09-24 04:46:38 +00:00
parent ff27652618
commit bdec09a2b8

View File

@@ -1,6 +1,7 @@
package client package client
import "fmt" import "fmt"
import "net/url"
type Workspace struct { type Workspace struct {
GID string `json:"gid"` GID string `json:"gid"`
@@ -8,7 +9,8 @@ type Workspace struct {
} }
type workspacesResponse struct { type workspacesResponse struct {
Data []*Workspace `json:"data"` Data []*Workspace `json:"data"`
NextPage *nextPage `json:"next_page"`
} }
func (c *Client) InWorkspace(name string) (*WorkspaceClient, error) { func (c *Client) InWorkspace(name string) (*WorkspaceClient, error) {
@@ -24,13 +26,26 @@ func (c *Client) InWorkspace(name string) (*WorkspaceClient, error) {
} }
func (c *Client) GetWorkspaces() ([]*Workspace, error) { func (c *Client) GetWorkspaces() ([]*Workspace, error) {
// TODO: Handle pagination values := &url.Values{}
resp := &workspacesResponse{} ret := []*Workspace{}
err := c.get("workspaces", nil, resp)
if err != nil { for {
return nil, err resp := &workspacesResponse{}
err := c.get("workspaces", 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 (c *Client) GetWorkspaceByName(name string) (*Workspace, error) { func (c *Client) GetWorkspaceByName(name string) (*Workspace, error) {