forked from darkSasori/todoist
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtask.go
159 lines (133 loc) · 3.08 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package todoist
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// Task is a model of todoist project entity
type Task struct {
ID uint `json:"id"`
CommentCount int `json:"comment_count"`
Completed bool `json:"completed"`
Content string `json:"content"`
Indent int `json:"indent"`
LabelIDs []int `json:"label_ids"`
Order int `json:"order"`
Priority int `json:"priority"`
ProjectID int `json:"project_id"`
Due Due `json:"due"`
}
// Due is a model of todoist project entity
type Due struct {
String string `json:"string"`
Date string `json:"date"`
Datetime time.Time `json:"datetime,omitempty"`
Timezone string `json:"timezone"`
}
func (t Task) taskSave() taskSave {
return taskSave{
t.Content,
t.ProjectID,
t.Order,
t.LabelIDs,
t.Priority,
t.Due.String,
t.Due.Datetime,
"en",
}
}
func decodeTask(body io.ReadCloser) (Task, error) {
defer body.Close()
decoder := json.NewDecoder(body)
var task Task
if err := decoder.Decode(&task); err != nil {
return Task{}, err
}
return task, nil
}
// QueryParam is a map[string]string to build http query
type QueryParam map[string]string
func (qp QueryParam) String() string {
if len(qp) == 0 {
return ""
}
ret := "?"
for key, value := range qp {
if ret != "?" {
ret = ret + "&"
}
ret = ret + key + "=" + value
}
return ret
}
// ListTask return all task, you can filter using QueryParam
// See documentation: https://developer.todoist.com/rest/v8/#get-tasks
func ListTask(qp QueryParam) ([]Task, error) {
path := fmt.Sprintf("tasks%s", qp)
res, err := makeRequest(http.MethodGet, path, nil)
if err != nil {
return []Task{}, err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var tasks []Task
if err := decoder.Decode(&tasks); err != nil {
return []Task{}, err
}
return tasks, nil
}
// GetTask return a task by id
func GetTask(id uint) (Task, error) {
path := fmt.Sprintf("tasks/%d", id)
res, err := makeRequest(http.MethodGet, path, nil)
if err != nil {
return Task{}, err
}
return decodeTask(res.Body)
}
// CreateTask create a new task
func CreateTask(task Task) (Task, error) {
res, err := makeRequest(http.MethodPost, "tasks", task.taskSave())
if err != nil {
return Task{}, err
}
return decodeTask(res.Body)
}
// Delete remove a task
func (t Task) Delete() error {
path := fmt.Sprintf("tasks/%d", t.ID)
_, err := makeRequest(http.MethodDelete, path, nil)
if err != nil {
return err
}
return nil
}
// Update a task
func (t Task) Update() error {
path := fmt.Sprintf("tasks/%d", t.ID)
_, err := makeRequest(http.MethodPost, path, t.taskSave())
if err != nil {
return err
}
return nil
}
// Close mask task as done
func (t Task) Close() error {
path := fmt.Sprintf("tasks/%d/close", t.ID)
_, err := makeRequest(http.MethodPost, path, nil)
if err != nil {
return err
}
return nil
}
// Reopen a task
func (t Task) Reopen() error {
path := fmt.Sprintf("tasks/%d/reopen", t.ID)
_, err := makeRequest(http.MethodPost, path, nil)
if err != nil {
return err
}
return nil
}