Skip to content

Commit

Permalink
add sync method
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Jan 14, 2017
1 parent 2406c9d commit af4d4c1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
41 changes: 39 additions & 2 deletions todoist/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ type Client struct {
URL *url.URL
HTTPClient *http.Client
Token string
SyncToken string
Logger *log.Logger
}

func NewClient(endpoint, token string, logger *log.Logger) (*Client, error) {
func NewClient(endpoint, token, sync_token string, logger *log.Logger) (*Client, error) {
if len(endpoint) == 0 {
endpoint = "https://todoist.com/API/v7"
}
Expand All @@ -34,11 +35,15 @@ func NewClient(endpoint, token string, logger *log.Logger) (*Client, error) {
return nil, errors.New("Missing API Token")
}

if len(sync_token) == 0 {
sync_token = "*"
}

if logger == nil {
logger = log.New(ioutil.Discard, "", log.LstdFlags)
}

return &Client{parsed_endpoint, client, token, logger}, nil
return &Client{parsed_endpoint, client, token, sync_token, logger}, nil
}

func (c *Client) NewRequest(ctx context.Context, method, spath string, values url.Values) (*http.Request, error) {
Expand Down Expand Up @@ -78,3 +83,35 @@ func decodeBody(resp *http.Response, out interface{}) error {
decoder := json.NewDecoder(resp.Body)
return decoder.Decode(out)
}

func (c *Client) Sync(ctx context.Context, commands []Command) (*SyncResponse, error) {
b, err := json.Marshal(commands)
if err != nil {
return nil, err
}
values := url.Values{
"sync_token": {c.SyncToken},
"day_orders_timestamp": {""},
"resource_types": {"[\"all\"]"},
"commands": {string(b)},
}
req, err := c.NewSyncRequest(ctx, values)
if err != nil {
return nil, err
}

res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}

var out SyncResponse
err = decodeBody(res, &out)
if err != nil {
return nil, err
}
// TODO: replace temp_id mapping
// TODO: update state
// TODO: write cache
return &out, nil
}
29 changes: 29 additions & 0 deletions todoist/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package todoist

type SyncResponse struct {
SyncToken string `json:"sync_token"`
FullSync bool `json:"full_sync"`
// User User `json:"user"`
Projects []Project `json:"projects"`
// ProjectNotes []interface{} `json:"project_notes"`
Items []Item `json:"items"`
Notes []Note `json:"notes"`
Labels []Label `json:"labels"`
Filters []Filter `json:"filters"`
// DayOrders struct {} `json:"day_orders"`
// DayOrdersTimestamp string `json:"day_orders_timestamp"`
Reminders []Reminder `json:"reminders"`
// Collaborators []interface{} `json:"collaborators"`
// CollaboratorStates []CollaboratorState `json:"collaborator_states"`
// LiveNotifications []LiveNotification `json:"live_notifications"`
// LiveNotificationsLastReadID int `json:"live_notifications_last_read_id"`
// Locations []interface{} `json:"locations"`
// TempIDMapping struct {} `json:"temp_id_mapping"`
}

type Command struct {
Type string `json:"type"`
Args interface{} `json:"args"`
UUID string `json:"uuid"`
TempID string `json:"temp_id"`
}

0 comments on commit af4d4c1

Please sign in to comment.