Support pagination
This commit is contained in:
@@ -23,6 +23,8 @@ type workspacesResponse struct {
|
|||||||
Data []*workspace `json:"data"`
|
Data []*workspace `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const perPage = 100
|
||||||
|
|
||||||
func NewClient(token string) *Client {
|
func NewClient(token string) *Client {
|
||||||
c := &Client{
|
c := &Client{
|
||||||
client: &http.Client{},
|
client: &http.Client{},
|
||||||
@@ -81,7 +83,7 @@ func (c *Client) get(path string, values *url.Values, out interface{}) error {
|
|||||||
if values == nil {
|
if values == nil {
|
||||||
values = &url.Values{}
|
values = &url.Values{}
|
||||||
}
|
}
|
||||||
values.Add("limit", "100")
|
values.Set("limit", fmt.Sprintf("%d", perPage))
|
||||||
|
|
||||||
url := fmt.Sprintf("%s%s?%s", baseURL, path, values.Encode())
|
url := fmt.Sprintf("%s%s?%s", baseURL, path, values.Encode())
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ type Tag struct {
|
|||||||
type Task struct {
|
type Task struct {
|
||||||
GID string `json:"gid,omitempty"`
|
GID string `json:"gid,omitempty"`
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
|
CreatedAt string `json:"created_at,omitempty"`
|
||||||
DueOn string `json:"due_on,omitempty"`
|
DueOn string `json:"due_on,omitempty"`
|
||||||
ParsedDueOn *civil.Date `json:"-"`
|
ParsedDueOn *civil.Date `json:"-"`
|
||||||
HTMLNotes string `json:"html_notes,omitempty"`
|
HTMLNotes string `json:"html_notes,omitempty"`
|
||||||
@@ -243,9 +244,12 @@ func (wc *WorkspaceClient) GetMyUserTaskList() (*Project, error) {
|
|||||||
func (wc *WorkspaceClient) Search(q *SearchQuery) ([]*Task, error) {
|
func (wc *WorkspaceClient) Search(q *SearchQuery) ([]*Task, error) {
|
||||||
path := fmt.Sprintf("workspaces/%s/tasks/search", wc.workspace.GID)
|
path := fmt.Sprintf("workspaces/%s/tasks/search", wc.workspace.GID)
|
||||||
|
|
||||||
values := &url.Values{}
|
values := &url.Values{
|
||||||
|
"sort_by": []string{"created_at"},
|
||||||
|
"sort_ascending": []string{"true"},
|
||||||
|
}
|
||||||
|
|
||||||
values.Add("opt_fields", "due_on,html_notes,name")
|
values.Add("opt_fields", "created_at,due_on,html_notes,name")
|
||||||
|
|
||||||
if len(q.SectionsAny) > 0 {
|
if len(q.SectionsAny) > 0 {
|
||||||
gids := []string{}
|
gids := []string{}
|
||||||
@@ -295,20 +299,42 @@ func (wc *WorkspaceClient) Search(q *SearchQuery) ([]*Task, error) {
|
|||||||
values.Add("tags.not", strings.Join(gids, ","))
|
values.Add("tags.not", strings.Join(gids, ","))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tasksByGID := map[string]*Task{}
|
||||||
|
|
||||||
|
for {
|
||||||
resp := &tasksResponse{}
|
resp := &tasksResponse{}
|
||||||
err := wc.client.get(path, values, resp)
|
err := wc.client.get(path, values, resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
maxCreatedAt := ""
|
||||||
|
|
||||||
for _, task := range resp.Data {
|
for _, task := range resp.Data {
|
||||||
err := task.parse()
|
err := task.parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
tasksByGID[task.GID] = task
|
||||||
|
|
||||||
|
if task.CreatedAt > maxCreatedAt {
|
||||||
|
maxCreatedAt = task.CreatedAt
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp.Data, nil
|
if len(resp.Data) < perPage {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
values.Set("created_at.after", maxCreatedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks := []*Task{}
|
||||||
|
for _, task := range tasksByGID {
|
||||||
|
tasks = append(tasks, task)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tasks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wc *WorkspaceClient) UpdateTask(task *Task) error {
|
func (wc *WorkspaceClient) UpdateTask(task *Task) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user