This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_v1.go
275 lines (249 loc) · 5.81 KB
/
api_v1.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
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
// Package dynalist implements dynalist.io API for Go
//
// Set an env before using:
// export DYNALIST_TOKEN=your_secret_token
package dynalist
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"os"
"time"
)
const (
baseurl = `https://dynalist.io/api/v1/`
)
type API struct {
Token string `json:"token"`
RateLimit time.Time `json:"-"`
BurstLimit int `json:"-"`
client *http.Client
}
func New() (*API, error) {
token := os.Getenv("DYNALIST_TOKEN")
if token == "" {
return nil, errors.New("failed to get $DYNALIST_TOKEN")
}
return &API{
Token: token,
client: &http.Client{},
}, nil
}
func (api *API) FileList() (*Response, error) {
var res Response
err := api.post(baseurl+"file/list", api, &res)
if err != nil {
return nil, err
}
return &res, nil
}
func (api *API) FileEdit(changes []*Change) (*Response, error) {
var res Response
param := struct {
Token string `json:"token"`
Changes []*Change `json:"changes"`
}{
Token: api.Token,
Changes: changes,
}
err := api.post(baseurl+"file/edit", ¶m, &res)
if err != nil {
return nil, err
}
return &res, nil
}
func (api *API) DocRead(fileID string) (*Response, error) {
var res Response
param := struct {
Token string `json:"token"`
FileID string `Json:"file_id"`
}{
Token: api.Token,
FileID: fileID,
}
err := api.post(baseurl+"doc/read", ¶m, &res)
if err != nil {
return nil, err
}
return &res, nil
}
func (api *API) DocEdit(fileID string, changes []*Change) (*Response, error) {
var res Response
param := struct {
Token string `json:"token"`
FileID string `Json:"file_id"`
Changes []*Change `json:"changes"`
}{
Token: api.Token,
FileID: fileID,
Changes: changes,
}
err := api.post(baseurl+"doc/edit", ¶m, &res)
if err != nil {
return nil, err
}
return &res, nil
}
func (api *API) InboxAdd(change *Change) (*Response, error) {
var res Response
param := struct {
Change
Token string `json:"token"`
}{
Token: api.Token,
Change: *change,
}
err := api.post(baseurl+"inbox/add", ¶m, &res)
if err != nil {
return nil, err
}
return &res, nil
}
func (api *API) post(url string, in, out interface{}) error {
b := &bytes.Buffer{}
err := json.NewEncoder(b).Encode(in)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, b)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
res, err := api.client.Do(req)
if err != nil {
return err
}
if res.Body == nil {
return errors.New("no body in the response")
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(out)
if err != nil {
return err
}
return nil
}
type Limit struct {
Rate time.Duration
Burst int
}
func (api *API) LimitFileList() *Limit {
return &Limit{
Rate: time.Minute / 6,
Burst: 10,
}
}
func (api *API) LimitFileEdit() *Limit {
return &Limit{
Rate: time.Minute / 60,
Burst: 50,
}
}
func (api *API) LimitDocRead() *Limit {
return &Limit{
Rate: time.Minute / 60,
Burst: 50,
}
}
func (api *API) LimitDocEdit() *Limit {
return &Limit{
Rate: time.Minute / 60,
Burst: 20,
}
}
func (api *API) LimitChange() *Limit {
return &Limit{
Rate: time.Minute / 240,
Burst: 500,
}
}
func (api *API) LimitInboxAdd() *Limit {
return &Limit{
Rate: time.Minute / 6,
Burst: 10,
}
}
type Change struct {
Action Action `json:"action"`
Index int `json:"index,omitempty"`
NodeID string `json:"node_id,omitempty"`
ParentID string `json:"parent_id,omitempty"`
Content string `json:"content,omitempty"`
Type Type `json:"type,omitempty"`
FileID string `json:"file_id,omitempty"`
Title string `json:"title,omitempty"`
Note string `json:"note,omitempty"`
Checked bool `json:"checked,omitempty"`
}
func NewChange(action Action) *Change {
return &Change{Action: action}
}
type Action string
const (
ActionInsert Action = "action"
ActionEdit Action = "edit"
ActionMove Action = "move"
ActionDelete Action = "delete"
)
type Response struct {
Code Code `json:"_code"`
Msg string `json:"_msg"`
RootFileID string `json:"root_file_id,omitempty"`
Files []File `json:"files,omitempty"`
Results []bool `json:"results,omitempty"`
Title string `json:"title,omitempty"`
Nodes []Node `json:"nodes,omitempty"`
}
type Code string
const (
CodeOK Code = "Ok"
//Your request is not valid JSON.
CodeInvalid Code = "Invalid"
//You've hit the limit on how many requests you can send.
CodeTooManyRequests Code = "TooManyRequests"
//Your secret token is invalid.
CodeInvalidToken Code = "InvalidToken"
//Server unable to handle the request.
CodeLockFail Code = "LockFail"
//You don't have permission to access this document.
CodeUnauthorized Code = "Unauthorized"
//The document you're requesting is not found.
CodeNotFound Code = "NotFound"
//The node (item) you're requesting is not found.
CodeNodeNotFound Code = "NodeNotFound"
//Inbox location is not configured, or invalid.
CodeNoInbox Code = "NoInbox"
)
type File struct {
ID string `json:"id"`
Title string `json:"title"`
Type Type `json:"type"`
Permission Permission `json:"permission"`
Collapsed bool `json:"collapsed,omitempty"`
Children []string `json:"children,omitempty"`
}
type Type string
const (
TypeDocument Type = "document"
TypeFolder Type = "folder"
)
type Permission int
const (
PermissionNoAccess Permission = iota
PermissionReadOnly
PermissionEditRights
PermissionManage
PermissionOwner
)
type Node struct {
ID string `json:"id"`
Content string `json:"content"`
Note string `json:"note,omitempty"`
Checked bool `json:"checked,omitempty"`
Collapsed bool `json:"collapsed,omitempty"`
Parent string `json:"parent,omitempty"`
Children []string `json:"children,omitempty"`
}