From f9f6b58d3598a74fbe1f3a938de1d65f290b5af1 Mon Sep 17 00:00:00 2001 From: kobtea Date: Thu, 6 Jul 2017 22:14:27 +0900 Subject: [PATCH] implement completed api --- todoist/client.go | 2 + todoist/completed.go | 123 +++++++++++++++++++++++++++++++++++++++++++ todoist/item.go | 5 ++ 3 files changed, 130 insertions(+) create mode 100644 todoist/completed.go diff --git a/todoist/client.go b/todoist/client.go index 05d1e2a..3ee5081 100644 --- a/todoist/client.go +++ b/todoist/client.go @@ -21,6 +21,7 @@ type Client struct { CacheDir string syncState *SyncState Logger *log.Logger + Completed *CompletedClient Filter *FilterClient Item *ItemClient Label *LabelClient @@ -74,6 +75,7 @@ func NewClient(endpoint, token, sync_token, cache_dir string, logger *log.Logger if err = c.readCache(); err != nil { c.resetState() } + c.Completed = &CompletedClient{c} c.Filter = &FilterClient{c, &filterCache{&c.syncState.Filters}} c.Item = &ItemClient{c, &itemCache{&c.syncState.Items}} c.Label = &LabelClient{c, &labelCache{&c.syncState.Labels}} diff --git a/todoist/completed.go b/todoist/completed.go new file mode 100644 index 0000000..dee565a --- /dev/null +++ b/todoist/completed.go @@ -0,0 +1,123 @@ +package todoist + +import ( + "context" + "net/url" +) + +type Stats struct { + KarmaLastUpdate float64 `json:"karma_last_update"` + KarmaTrend string `json:"karma_trend"` + DaysItems []struct { + Date string `json:"date"` + Items []struct { + Completed int `json:"completed"` + ID int `json:"id"` + } `json:"items"` + TotalCompleted int `json:"total_completed"` + } `json:"days_items"` + CompletedCount int `json:"completed_count"` + KarmaUpdateReasons []struct { + PositiveKarmaReasons []int `json:"positive_karma_reasons"` + NewKarma float64 `json:"new_karma"` + NegativeKarma float64 `json:"negative_karma"` + PositiveKarma float64 `json:"positive_karma"` + NegativeKarmaReasons []int `json:"negative_karma_reasons"` + Time string `json:"time"` + } `json:"karma_update_reasons"` + Karma float64 `json:"karma"` + WeekItems []struct { + Date string `json:"date"` + Items []struct { + Completed int `json:"completed"` + ID int `json:"id"` + } `json:"items"` + TotalCompleted int `json:"total_completed"` + } `json:"week_items"` + KarmaGraph string `json:"karma_graph"` + Goals struct { + KarmaDisabled int `json:"karma_disabled"` + UserID int `json:"user_id"` + LastDailyStreak struct { + Count int `json:"count"` + Start string `json:"start"` + End string `json:"end"` + } `json:"last_daily_streak"` + VacationMode int `json:"vacation_mode"` + IgnoreDays []int `json:"ignore_days"` + MaxWeeklyStreak struct { + Count int `json:"count"` + Start string `json:"start"` + End string `json:"end"` + } `json:"max_weekly_streak"` + CurrentWeeklyStreak struct { + Count int `json:"count"` + Start string `json:"start"` + End string `json:"end"` + } `json:"current_weekly_streak"` + CurrentDailyStreak struct { + Count int `json:"count"` + Start string `json:"start"` + End string `json:"end"` + } `json:"current_daily_streak"` + LastWeeklyStreak struct { + Count int `json:"count"` + Start string `json:"start"` + End string `json:"end"` + } `json:"last_weekly_streak"` + WeeklyGoal int `json:"weekly_goal"` + MaxDailyStreak struct { + Count int `json:"count"` + Start string `json:"start"` + End string `json:"end"` + } `json:"max_daily_streak"` + DailyGoal int `json:"daily_goal"` + } `json:"goals"` +} + +type CompletedItems struct { + Items []CompletedItem `json:"items"` + Projects map[ID]Project `json:"projects"` +} + +func (c *CompletedItems) GroupByCompletedDate() map[string][]CompletedItem { + const layout = "2006-01-02" + res := map[string][]CompletedItem{} + for _, item := range c.Items { + date := item.CompletedDate.Local().Format(layout) + res[date] = append(res[date], item) + } + return res +} + +type CompletedClient struct { + *Client +} + +func (c *CompletedClient) GetStats() (*Stats, error) { + req, err := c.newRequest(context.Background(), "POST", "completed/get_stats", url.Values{}) + if err != nil { + return nil, err + } + res, err := c.HTTPClient.Do(req) + if err != nil { + return nil, err + } + var out Stats + decodeBody(res, &out) + return &out, nil +} + +func (c *CompletedClient) GetAll() (*CompletedItems, error) { + req, err := c.newRequest(context.Background(), "POST", "completed/get_all", url.Values{}) + if err != nil { + return nil, err + } + res, err := c.HTTPClient.Do(req) + if err != nil { + return nil, err + } + var out CompletedItems + decodeBody(res, &out) + return &out, nil +} diff --git a/todoist/item.go b/todoist/item.go index 007eff2..e313513 100644 --- a/todoist/item.go +++ b/todoist/item.go @@ -33,6 +33,11 @@ type Item struct { DateAdded Time `json:"date_added,omitempty"` } +type CompletedItem struct { + Item + CompletedDate Time `json:"completed_date"` +} + func (i Item) IsOverDueDate() bool { return i.DueDateUtc.Before(Time{time.Now().UTC()}) }