-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks.go
215 lines (193 loc) · 6.57 KB
/
tasks.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package todoist
import (
"context"
"encoding/json"
"net/url"
"strings"
)
type TasksResponse struct {
Tasks []Task
TodoistResponse
}
type TaskResponse struct {
Task Task
TodoistResponse
}
type Due struct {
Date string `json:"date"`
IsRecurring bool `json:"is_recurring"`
Datetime string `json:"datetime"`
String string `json:"string"`
Timezone string `json:"timezone"`
}
type Task struct {
Id string `json:"id"`
AssignerId any `json:"assigner_id"`
AssigneeId any `json:"assignee_id"`
ProjectId string `json:"project_id"`
SectionId string `json:"section_id"`
ParentId any `json:"parent_id"`
Order int `json:"order"`
Content string `json:"content"`
Description string `json:"description"`
IsCompleted bool `json:"is_completed"`
Labels []any `json:"labels"`
Priority int `json:"priority"`
CommentCount int `json:"comment_count"`
CreatorId string `json:"creator_id"`
CreatedAt string `json:"created_at"`
Due *Due `json:"due"`
Url string `json:"url"`
}
type Task1 struct {
CreatorId string `json:"creator_id"`
CreatedAt string `json:"created_at"`
AssigneeId *string `json:"assignee_id"`
AssignerId *string `json:"assigner_id"`
CommentCount int `json:"comment_count"`
IsCompleted bool `json:"is_completed"`
Content string `json:"content"`
Description string `json:"description"`
Due *Due `json:"due"`
Id string `json:"id"`
Labels []string `json:"labels"`
Order int `json:"order"`
Priority int `json:"priority"`
ProjectId string `json:"project_id"`
SectionId string `json:"section_id"`
ParentId string `json:"parent_id"`
Url string `json:"url"`
}
type AddTaskRequest struct {
Content string `json:"content"`
Description string `json:"description"`
ProjectId string `json:"project_id"`
SectionId *string `json:"section_id"`
ParentId *string `json:"parent_id"`
Order int `json:"order"`
Labels []string `json:"labels"`
Priority int `json:"priority"`
DueString string `json:"due_string"`
DueDate string `json:"due_date"`
DueDatetime string `json:"due_datetime"`
DueLang string `json:"due_lang"`
AssigneeId *string `json:"assignee_id"`
}
type GetActiveTasksRequest struct {
ProjectId string `json:"project_id"` // Optional
SectionId string `json:"section_id"` // Optional
Label string `json:"label"` // Optional
Filter string `json:"filter"` // Optional
Lang string `json:"lang"` // Optional
Ids []string `json:"ids"` // Optional
}
type UpdateTaskRequest struct {
Content string `json:"content"` // Optional
Description string `json:"description"` // Optional
Labels []string `json:"labels"` // Optional
Priority int `json:"priority"` // Optional
DueString string `json:"due_string"` // Optional
DueDate string `json:"due_date"` // Optional
DueDatetime string `json:"due_datetime"` // Optional
DueLang string `json:"due_lang"` // Optional
AssigneeId string `json:"assignee_id"` // Optional
}
func (api *Client) GetActiveTasks(getActiveTasksRequest GetActiveTasksRequest) (*[]Task, error) {
return api.GetActiveTasksContext(getActiveTasksRequest, context.Background())
}
func (api *Client) AddTask(request AddTaskRequest) (*Task, error) {
return api.AddTaskContext(request, context.Background())
}
func (api *Client) GetActiveTaskById(id string) (*Task, error) {
return api.GetActiveTaskByIdContext(id, context.Background())
}
func (api *Client) UpdateTask(id string, updateTaskRequest UpdateTaskRequest) (*Task, error) {
return api.UpdateTaskContext(id, updateTaskRequest, context.Background())
}
func (api *Client) CloseTask(id string) (*TodoistResponse, error) {
return api.CloseTaskContext(id, context.Background())
}
func (api *Client) ReopenTask(id string) (*TodoistResponse, error) {
return api.ReopenTaskContext(id, context.Background())
}
func (api *Client) DeleteTaskById(id string) (*TodoistResponse, error) {
return api.DeleteTaskByIdContext(id, context.Background())
}
func (api *Client) GetActiveTasksContext(request GetActiveTasksRequest, context context.Context) (*[]Task, error) {
response := &TasksResponse{}
values := url.Values{
"project_id": {request.ProjectId},
"section_id": {request.SectionId},
"label": {request.Label},
"filter": {request.Filter},
"lang": {request.Lang},
"ids": {strings.Join(request.Ids, ",")},
}
err := api.get(context,
"tasks",
api.token,
values,
&response.Tasks)
return &response.Tasks, err
}
func (api *Client) AddTaskContext(addTaskRequest AddTaskRequest, context context.Context) (*Task, error) {
response := &TaskResponse{}
request, err := json.Marshal(addTaskRequest)
err = api.post(context, "tasks", api.token, request, &response.Task)
if err != nil {
return nil, err
} else {
return &response.Task, nil
}
}
func (api *Client) GetActiveTaskByIdContext(id string, context context.Context) (*Task, error) {
response := &TaskResponse{}
err := api.get(context,
"tasks/"+id,
api.token,
url.Values{},
&response.Task)
return &response.Task, err
}
func (api *Client) UpdateTaskContext(id string, updateTaskRequest UpdateTaskRequest, context context.Context) (*Task, error) {
response := &TaskResponse{}
request, _ := json.Marshal(updateTaskRequest)
err := api.post(context, "tasks/"+id, api.token, request, &response.Task)
if err != nil {
return nil, err
} else {
return &response.Task, nil
}
}
func (api *Client) CloseTaskContext(id string, context context.Context) (*TodoistResponse, error) {
response := &TodoistResponse{}
err := performPostWithoutResponse(context, api.httpclient, api.endpoint+"tasks/"+id+"/close", api.token, &response, api)
if err != nil {
return nil, err
}
if !response.Ok {
return nil, response.Err()
}
return response, err
}
func (api *Client) ReopenTaskContext(id string, context context.Context) (*TodoistResponse, error) {
response := &TodoistResponse{}
err := performPostWithoutResponse(context, api.httpclient, api.endpoint+"tasks/"+id+"/reopen", api.token, &response, api)
if err != nil {
return nil, err
}
if !response.Ok {
return nil, response.Err()
}
return response, err
}
func (api *Client) DeleteTaskByIdContext(id string, context context.Context) (*TodoistResponse, error) {
response := &TodoistResponse{}
err := performDelete(context,
api.httpclient,
api.endpoint+"tasks/"+id,
api.token,
response,
api)
return response, err
}