-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote.go
More file actions
282 lines (223 loc) · 6.14 KB
/
note.go
File metadata and controls
282 lines (223 loc) · 6.14 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package sdk
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"
"golang.org/x/text/unicode/norm"
)
type NoteText struct {
// NoteTextID int64 `json:"-"`
// NoteID int64 `json:"-"`
NoteTextValue string `json:"note_text"`
Created time.Time `json:"created"`
}
// NoteTag is a string that includes methods for canonicalizing the tag as used
// by the server itself
type NoteTag string
// NoteMeta is a map of string keys to string values that can be used to store
// arbitrary metadata about a note that is not part of the note itself
// and is useful for implementation specific values.
//
// The server enforces a 255 character limit on both keys and values.
// The key is expected to be ascii only and the value is expected to be utf-8
type NoteMeta map[string]string
// c14nReg matches Combining Diacritical Marks
// see: https://en.wikipedia.org/wiki/Combining_Diacritical_Marks
var c14nReg = regexp.MustCompile("[\u0300-\u036f]")
// C14n provides the standard method by which tags are canonicalized.
//
// The process involves converting the string to NFD normalization and
// removing all Combining Diacritical Marks
//
// There should be little need to call this directly other than perhaps
// comparing strings that have not yet been canonicalized by the server,
// it is provided here as a helper for such comparison but shall not
// be needed before a roundtrip to the server.
func (nt NoteTag) C14n() string {
s := strings.ToLower(string(nt))
s = norm.NFD.String(s)
s = c14nReg.ReplaceAllString(s, "")
return s
}
type Note struct {
PublicID string `json:"public_id"`
// UserID int `json:"-"`
// NoteTextID int64 `json:"-"` // maybe get rid of?
Archived bool `json:"archived"`
Starred bool `json:"starred"`
Created time.Time `json:"created"`
Tags []NoteTag `json:"tags"`
Meta NoteMeta `json:"meta"`
CurrentText *NoteText `json:"current"`
TextHistory []*NoteText `json:"history,omitempty"`
}
func (a *AuthenticatedAPI) authRequest(r *http.Request) {
r.Header.Add("Authorization", "Token "+a.token)
}
func (a *AuthenticatedAPI) GetNotes() ([]*Note, error) {
client := &http.Client{}
// Create request
req, err := http.NewRequest("GET", a.GetEndpoint()+"/notes", nil)
if err != nil {
return nil, err
}
a.authRequest(req)
// Fetch Request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusForbidden {
return nil, ErrInvalidCredentials
} else if resp.StatusCode != http.StatusOK {
text, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %s", resp.StatusCode, text)
}
tresp := []*Note{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&tresp)
if err != nil {
return nil, err
}
return tresp, nil
}
func (a *AuthenticatedAPI) GetNote(publicID string) (*Note, error) {
client := &http.Client{}
// Create request
req, err := http.NewRequest("GET", fmt.Sprintf("%s/notes/%s", a.GetEndpoint(), publicID), nil)
if err != nil {
return nil, err
}
a.authRequest(req)
// Fetch Request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusForbidden {
return nil, ErrInvalidCredentials
} else if resp.StatusCode == http.StatusNotFound {
return nil, ErrNotFound
} else if resp.StatusCode != http.StatusOK {
text, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %s", resp.StatusCode, text)
}
tresp := &Note{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&tresp)
if err != nil {
return nil, err
}
return tresp, nil
}
func (a *AuthenticatedAPI) PostNewNote(n *Note) (*Note, error) {
client := &http.Client{}
j, err := json.Marshal(n)
if err != nil {
return nil, err
}
// Create request
req, err := http.NewRequest("POST", a.GetEndpoint()+"/notes", bytes.NewBuffer(j))
if err != nil {
return nil, err
}
a.authRequest(req)
// Fetch Request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusForbidden {
return nil, ErrInvalidCredentials
} else if resp.StatusCode != http.StatusCreated {
text, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %s", resp.StatusCode, text)
}
tresp := &Note{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&tresp)
if err != nil {
return nil, err
}
return tresp, nil
}
func (a *AuthenticatedAPI) PutUpdateNote(n *Note) (*Note, error) {
client := &http.Client{}
j, err := json.Marshal(n)
if err != nil {
return nil, err
}
// Create request
req, err := http.NewRequest("PUT", fmt.Sprintf("%s/notes/%s", a.GetEndpoint(), n.PublicID), bytes.NewBuffer(j))
if err != nil {
return nil, err
}
a.authRequest(req)
// Fetch Request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusForbidden {
return nil, ErrInvalidCredentials
} else if resp.StatusCode != http.StatusOK {
text, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %s", resp.StatusCode, text)
}
tresp := &Note{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&tresp)
if err != nil {
return nil, err
}
return tresp, nil
}
func (a *AuthenticatedAPI) DeleteNote(publicID string) (bool, error) {
client := &http.Client{}
// Create request
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/notes/%s", a.GetEndpoint(), publicID), nil)
if err != nil {
return false, err
}
a.authRequest(req)
// Fetch Request
resp, err := client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusForbidden {
return false, ErrInvalidCredentials
} else if resp.StatusCode == http.StatusNotFound {
// already deleted
return false, ErrNotFound
} else if resp.StatusCode != http.StatusNoContent {
text, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
return false, fmt.Errorf("%d: %s", resp.StatusCode, text)
}
return true, nil
}