Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #3; query params in task.List(); added fields to Task struct #4

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration_test

import (
"context"
"net/url"
"os"
"testing"

Expand Down Expand Up @@ -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)
}
Expand Down
30 changes: 28 additions & 2 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)

type Task struct {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/wfernandes/go-habitica"
Expand Down Expand Up @@ -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))
Expand Down