This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.go
218 lines (181 loc) · 4.6 KB
/
list.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
216
217
218
package oauth
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/robdimsdale/wundergo"
)
// Lists returns all lists the client has permission to access.
func (c oauthClient) Lists() ([]wundergo.List, error) {
url := fmt.Sprintf(
"%s/lists",
c.apiURL,
)
req, err := c.newGetRequest(url)
if err != nil {
return nil, err
}
resp, err := c.do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Unexpected response code %d - expected %d", resp.StatusCode, http.StatusOK)
}
lists := []wundergo.List{}
err = json.NewDecoder(resp.Body).Decode(&lists)
if err != nil {
return lists, err
}
return lists, nil
}
// List returns the list for the corresponding listID.
func (c oauthClient) List(listID uint) (wundergo.List, error) {
url := fmt.Sprintf(
"%s/lists/%d",
c.apiURL,
listID,
)
req, err := c.newGetRequest(url)
if err != nil {
return wundergo.List{}, err
}
resp, err := c.do(req)
if err != nil {
return wundergo.List{}, err
}
if resp.StatusCode != http.StatusOK {
return wundergo.List{}, fmt.Errorf("Unexpected response code %d - expected %d", resp.StatusCode, http.StatusOK)
}
list := wundergo.List{}
err = json.NewDecoder(resp.Body).Decode(&list)
if err != nil {
return wundergo.List{}, err
}
return list, nil
}
// CreateList creates a list with the provided title.
func (c oauthClient) CreateList(title string) (wundergo.List, error) {
if title == "" {
return wundergo.List{}, fmt.Errorf("title must be non-empty")
}
url := fmt.Sprintf("%s/lists", c.apiURL)
body := []byte(fmt.Sprintf(`{"title":"%s"}`, title))
req, err := c.newPostRequest(url, body)
if err != nil {
return wundergo.List{}, err
}
resp, err := c.do(req)
if err != nil {
return wundergo.List{}, err
}
if resp.StatusCode != http.StatusCreated {
return wundergo.List{}, fmt.Errorf("Unexpected response code %d - expected %d", resp.StatusCode, http.StatusCreated)
}
list := wundergo.List{}
err = json.NewDecoder(resp.Body).Decode(&list)
if err != nil {
return wundergo.List{}, err
}
return list, nil
}
// UpdateList updates the provided List.
func (c oauthClient) UpdateList(list wundergo.List) (wundergo.List, error) {
body, err := json.Marshal(list)
if err != nil {
return wundergo.List{}, err
}
url := fmt.Sprintf(
"%s/lists/%d",
c.apiURL,
list.ID,
)
req, err := c.newPatchRequest(url, body)
if err != nil {
return wundergo.List{}, err
}
resp, err := c.do(req)
if err != nil {
return wundergo.List{}, err
}
if resp.StatusCode != http.StatusOK {
return wundergo.List{}, fmt.Errorf("Unexpected response code %d - expected %d", resp.StatusCode, http.StatusOK)
}
returnedList := wundergo.List{}
err = json.NewDecoder(resp.Body).Decode(&returnedList)
if err != nil {
return wundergo.List{}, err
}
return returnedList, nil
}
// DeleteList deletes the provided list.
func (c oauthClient) DeleteList(list wundergo.List) error {
url := fmt.Sprintf(
"%s/lists/%d?revision=%d",
c.apiURL,
list.ID,
list.Revision,
)
req, err := c.newDeleteRequest(url)
if err != nil {
return err
}
resp, err := c.do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("Unexpected response code %d - expected %d", resp.StatusCode, http.StatusNoContent)
}
return nil
}
// DeleteAllLists gets a list of all lists via Lists() and deletes them
// via DeleteList(listID)
// It will not attempt to delete the inbox
func (c oauthClient) DeleteAllLists() error {
lists, err := c.Lists()
if err != nil {
return err
}
listCount := len(lists)
c.logger.Debug("delete-all-lists", map[string]interface{}{"listCount": listCount})
idErrChan := make(chan idErr, listCount)
for _, l := range lists {
go func(list wundergo.List) {
c.logger.Debug("delete-all-lists - deleting list", map[string]interface{}{"listID": list.ID})
var err error
if list.ListType == "inbox" {
err = nil
} else {
err = c.DeleteList(list)
}
idErrChan <- idErr{idType: "list", id: list.ID, err: err}
}(l)
}
e := multiIDErr{}
for i := 0; i < len(lists); i++ {
idErr := <-idErrChan
if idErr.err != nil {
c.logger.Debug("delete-all-lists - error received", map[string]interface{}{"id": idErr.id, "err": err})
e.addError(idErr)
}
}
if len(e.errors()) > 0 {
return e
}
return nil
}
// Inbox returns the inbox list.
func (c oauthClient) Inbox() (wundergo.List, error) {
lists, err := c.Lists()
if err != nil {
return wundergo.List{}, err
}
for _, l := range lists {
if l.Title == "inbox" {
return l, nil
}
}
return wundergo.List{}, errors.New("Inbox not found")
}