Skip to content

Commit

Permalink
implement completed api
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Jul 6, 2017
1 parent e0d25d0 commit f9f6b58
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions todoist/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Client struct {
CacheDir string
syncState *SyncState
Logger *log.Logger
Completed *CompletedClient
Filter *FilterClient
Item *ItemClient
Label *LabelClient
Expand Down Expand Up @@ -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}}
Expand Down
123 changes: 123 additions & 0 deletions todoist/completed.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions todoist/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
}
Expand Down

0 comments on commit f9f6b58

Please sign in to comment.