From 639bc56ea72e3ec38d6b1ebcd58681c492ec5f49 Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 27 Apr 2021 01:34:41 +0900 Subject: [PATCH] issue #3; query params in task.List(); added fields to Task struct --- integration/integration_test.go | 4 +++- tasks.go | 30 ++++++++++++++++++++++++++++-- tasks_test.go | 4 +++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index 5fc551a..e067156 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -2,6 +2,7 @@ package integration_test import ( "context" + "net/url" "os" "testing" @@ -73,7 +74,8 @@ func TestIntegration_UsersTasks(t *testing.T) { if err != nil { t.Errorf("error creating client: %s", err) } - tasks, err := client.Tasks.List(context.Background()) + v := url.Values{} + tasks, err := client.Tasks.List(context.Background(), v) if err != nil { t.Errorf("error retrieving list of tasks: %s", err) } diff --git a/tasks.go b/tasks.go index eda7547..1ccacf5 100644 --- a/tasks.go +++ b/tasks.go @@ -5,6 +5,9 @@ import ( "encoding/json" "fmt" "net/http" + "net/url" + "strings" + "time" ) type Task struct { @@ -16,6 +19,29 @@ type Task struct { Tags []string `json:"tags"` Completed bool `json:"completed"` Checklist []ChecklistItem `json:"checklist"` + NextDue []NextDueType `json:"nextDue"` + IsDue bool `json:"isDue"` +} + +type NextDueType time.Time + +func (n *NextDueType) UnmarshalJSON(b []byte) error { + s := strings.Trim(string(b), "\"") + //the habitica api returns one of two date formats randomly + t, err := time.Parse("2006-01-02T15:04:05.000Z", s) + if err != nil { + t, err = time.Parse("Mon Jan 02 2006 15:04:05 MST-0700", s) + } + if err != nil { + return err + } + *n = NextDueType(t) + return nil +} + +func (n NextDueType) Format(s string) string { + t := time.Time(n) + return t.Format(s) } type TaskResponse struct { @@ -75,8 +101,8 @@ func (t *TaskService) Get(ctx context.Context, id string) (*TaskResponse, error) return t.getTaskResponse(ctx, req) } -func (t *TaskService) List(ctx context.Context) (*TasksResponse, error) { - req, err := t.client.NewRequest(http.MethodGet, "tasks/user", nil) +func (t *TaskService) List(ctx context.Context, vals url.Values) (*TasksResponse, error) { + req, err := t.client.NewRequest(http.MethodGet, fmt.Sprintf("tasks/user?%s", vals.Encode()), nil) if err != nil { return nil, fmt.Errorf("unable to create request: %s", err) } diff --git a/tasks_test.go b/tasks_test.go index fee8ae2..b65b82c 100644 --- a/tasks_test.go +++ b/tasks_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "net/url" "testing" "github.com/wfernandes/go-habitica" @@ -98,7 +99,8 @@ func TestGet_UserTasks(t *testing.T) { w.Write(userTasksResponse) }) - resp, err := client.Tasks.List(ctx) + v := url.Values{} + resp, err := client.Tasks.List(ctx, v) Expect(err).ToNot(HaveOccurred()) Expect(resp).ToNot(BeNil()) Expect(resp.Data).To(HaveLen(1))