Skip to content

Commit

Permalink
implement add and get methods for item
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Jan 21, 2017
1 parent c5adc8c commit acf617a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 26 deletions.
22 changes: 21 additions & 1 deletion todoist/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Client struct {
CacheDir string
SyncState *SyncState
Logger *log.Logger
Item *ItemManager
queue []Command
}

func NewClient(endpoint, token, sync_token, cache_dir string, logger *log.Logger) (*Client, error) {
Expand Down Expand Up @@ -56,7 +58,16 @@ func NewClient(endpoint, token, sync_token, cache_dir string, logger *log.Logger
logger = log.New(ioutil.Discard, "", log.LstdFlags)
}

c := &Client{parsed_endpoint, client, token, sync_token, cache_dir, &SyncState{}, logger}
c := &Client{
URL: parsed_endpoint,
HTTPClient: client,
Token: token,
SyncToken: sync_token,
CacheDir: cache_dir,
SyncState: &SyncState{},
Logger: logger,
}
c.Item = &ItemManager{c}
if err = c.readCache(); err != nil {
c.resetState()
}
Expand Down Expand Up @@ -138,6 +149,15 @@ func (c *Client) FullSync(ctx context.Context, commands []Command) error {
return c.Sync(ctx, commands)
}

func (c *Client) Commit(ctx context.Context) error {
if len(c.queue) == 0 {
return nil
}
err := c.Sync(ctx, c.queue)
c.queue = []Command{}
return err
}

func (c *Client) resetState() {
c.SyncToken = "*"
c.SyncState = &SyncState{}
Expand Down
67 changes: 44 additions & 23 deletions todoist/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@ import (
"context"
"net/url"
"net/http"
"errors"
)

type Item struct {
ID int `json:"id"`
UserID int `json:"user_id"`
ProjectID int `json:"project_id"`
ID ID `json:"id,omitempty"`
UserID int `json:"user_id,omitempty"`
ProjectID ID `json:"project_id,omitempty"`
Content string `json:"content"`
DateString string `json:"date_string"`
DateLang string `json:"date_lang"`
DueDateUtc string `json:"due_date_utc"`
Priority int `json:"priority"`
Indent int `json:"indent"`
ItemOrder int `json:"item_order"`
DayOrder int `json:"day_order"`
Collapsed int `json:"collapsed"`
Labels []int `json:"labels"`
AssignedByUID int `json:"assigned_by_uid"`
ResponsibleUID int `json:"responsible_uid"`
Checked int `json:"checked"`
InHistory int `json:"in_history"`
IsDeleted int `json:"is_deleted"`
IsArchived int `json:"is_archived"`
SyncID int `json:"sync_id"`
DateAdded string `json:"date_added"`
DateString string `json:"date_string,omitempty"`
DateLang string `json:"date_lang,omitempty"`
DueDateUtc string `json:"due_date_utc,omitempty"`
Priority int `json:"priority,omitempty"`
Indent int `json:"indent,omitempty"`
ItemOrder int `json:"item_order,omitempty"`
DayOrder int `json:"day_order,omitempty"`
Collapsed int `json:"collapsed,omitempty"`
Labels []int `json:"labels,omitempty"`
AssignedByUID int `json:"assigned_by_uid,omitempty"`
ResponsibleUID int `json:"responsible_uid,omitempty"`
Checked int `json:"checked,omitempty"`
InHistory int `json:"in_history,omitempty"`
IsDeleted int `json:"is_deleted,omitempty"`
IsArchived int `json:"is_archived,omitempty"`
SyncID int `json:"sync_id,omitempty"`
DateAdded string `json:"date_added,omitempty"`
}

type ItemResponse struct {
Expand All @@ -36,12 +37,32 @@ type ItemResponse struct {
Notes []Note
}

func (c *Client) GetItem(ctx context.Context, id string) (*ItemResponse, error) {
req, err := c.NewRequest(ctx, http.MethodGet, "items/get", url.Values{"item_id": {id}})
type ItemManager struct {
*Client
}

func (m *ItemManager) Add(ctx context.Context, item Item) (*Item, error) {
if len(item.Content) == 0 {
return nil, errors.New("New item requires a content")
}
item.ID = GenerateTempID()
m.SyncState.Items = append(m.SyncState.Items, item)
command := Command{
Type: "item_add",
Args: item,
UUID: GenerateUUID(),
TempID: item.ID,
}
m.queue = append(m.queue, command)
return &item, nil
}

func (m *ItemManager) Get(ctx context.Context, id string) (*ItemResponse, error) {
req, err := m.NewRequest(ctx, http.MethodGet, "items/get", url.Values{"item_id": {id}})
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
res, err := m.HTTPClient.Do(req)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions todoist/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ type SyncState struct {
type Command struct {
Type string `json:"type"`
Args interface{} `json:"args"`
UUID string `json:"uuid"`
TempID string `json:"temp_id"`
UUID UUID `json:"uuid"`
TempID ID `json:"temp_id"`
}

0 comments on commit acf617a

Please sign in to comment.