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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
|
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
+
+
+
+
+
+
-
+
-
+
-
+
+
+
+
+
-
-
-
+
+
-
+
-
+
-
+
+
+
-
+
+
+
-
+
+
+
+
+
-
-
-
+
+
+
-
+
-
+
-
+
-
+
-
-
+
-
-
+
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
+
-
-
+
+
-
-
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
+
+
+
+
+
-
-
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
return &Error{
StatusCode: resp.StatusCode,
Message: resp.Status[4:],
Body: body,
}
}
// NewURLBuilder creates a new URL builder for the client with the given key.
//
// key is one of the defined lower case letters to specify an endpoint.
// See [Endpoints used by the API] for details.
//
// [Endpoints used by the API]: https://zettelstore.de/manual/h/00001012920000
func (c *Client) newURLBuilder(key byte) *api.URLBuilder {
func (c *Client) NewURLBuilder(key byte) *api.URLBuilder {
return api.NewURLBuilder(c.base, key)
}
func (*Client) newRequest(ctx context.Context, method string, ub *api.URLBuilder, body io.Reader) (*http.Request, error) {
return http.NewRequestWithContext(ctx, method, ub.String(), body)
}
func (c *Client) executeRequest(req *http.Request) (*http.Response, error) {
if c.token != "" {
req.Header.Add("Authorization", c.tokenType+" "+c.token)
}
resp, err := c.client.Do(req)
if err != nil {
if resp != nil && resp.Body != nil {
resp.Body.Close()
_ = resp.Body.Close()
}
return nil, err
}
return resp, err
}
func (c *Client) buildAndExecuteRequest(
ctx context.Context, method string, ub *api.URLBuilder, body io.Reader, h http.Header) (*http.Response, error) {
ctx context.Context,
method string,
ub *api.URLBuilder,
body io.Reader,
) (*http.Response, error) {
req, err := c.newRequest(ctx, method, ub, body)
if err != nil {
return nil, err
}
err = c.updateToken(ctx)
if err != nil {
return nil, err
}
for key, val := range h {
req.Header[key] = append(req.Header[key], val...)
}
return c.executeRequest(req)
}
// SetAuth sets authentication data.
//
// username and password are the same values that are used to authenticate via the Web-UI.
func (c *Client) SetAuth(username, password string) {
c.username = username
c.password = password
c.token = ""
c.tokenType = ""
c.expires = time.Time{}
}
func (c *Client) executeAuthRequest(req *http.Request) error {
resp, err := c.executeRequest(req)
if err != nil {
return err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return statusToError(resp)
}
rd := sxreader.MakeReader(resp.Body)
obj, err := rd.Read()
if err != nil {
return err
}
vals, err := sexp.ParseList(obj, "ssi")
if err != nil {
return err
}
token := vals[1].(sx.String).String()
token := vals[1].(sx.String).GetValue()
if len(token) < 4 {
return fmt.Errorf("no valid token found: %q", token)
}
c.token = token
c.tokenType = vals[0].(sx.String).String()
c.tokenType = vals[0].(sx.String).GetValue()
c.expires = time.Now().Add(time.Duration(vals[2].(sx.Int64)*9/10) * time.Second)
return nil
}
func (c *Client) updateToken(ctx context.Context) error {
if c.username == "" {
return nil
}
if time.Now().After(c.expires) {
return c.Authenticate(ctx)
}
return c.RefreshToken(ctx)
}
// Authenticate sets a new token by sending user name and password.
//
// [Client.SetAuth] should be called before.
func (c *Client) Authenticate(ctx context.Context) error {
authData := url.Values{"username": {c.username}, "password": {c.password}}
req, err := c.newRequest(ctx, http.MethodPost, c.newURLBuilder('a'), strings.NewReader(authData.Encode()))
req, err := c.newRequest(ctx, http.MethodPost, c.NewURLBuilder('a'), strings.NewReader(authData.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return c.executeAuthRequest(req)
}
// RefreshToken updates the access token
//
// [Client.SetAuth] should be called before.
func (c *Client) RefreshToken(ctx context.Context) error {
req, err := c.newRequest(ctx, http.MethodPut, c.newURLBuilder('a'), nil)
req, err := c.newRequest(ctx, http.MethodPut, c.NewURLBuilder('a'), nil)
if err != nil {
return err
}
return c.executeAuthRequest(req)
}
// CreateZettel creates a new zettel and returns its URL.
//
// data contains the zettel metadata and content, as it is stored in a file in a zettel box,
// or as returned by [Client.GetZettel].
// Metadata is separated from zettel content by an empty line.
func (c *Client) CreateZettel(ctx context.Context, data []byte) (api.ZettelID, error) {
ub := c.newURLBuilder('z')
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, bytes.NewBuffer(data), nil)
func (c *Client) CreateZettel(ctx context.Context, data []byte) (id.Zid, error) {
ub := c.NewURLBuilder('z')
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, bytes.NewBuffer(data))
if err != nil {
return api.InvalidZID, err
return id.Invalid, err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusCreated {
return api.InvalidZID, statusToError(resp)
return id.Invalid, statusToError(resp)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return api.InvalidZID, err
return id.Invalid, err
}
if zid := api.ZettelID(b); zid.IsValid() {
return zid, nil
return id.Parse(string(b))
}
return api.InvalidZID, err
}
// CreateZettelData creates a new zettel and returns its URL.
//
// data contains the zettel date, encoded as explicit struct.
func (c *Client) CreateZettelData(ctx context.Context, data api.ZettelData) (api.ZettelID, error) {
func (c *Client) CreateZettelData(ctx context.Context, data api.ZettelData) (id.Zid, error) {
var buf bytes.Buffer
if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil {
return api.InvalidZID, err
return id.Invalid, err
}
ub := c.newURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, &buf, nil)
ub := c.NewURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, &buf)
if err != nil {
return api.InvalidZID, err
return id.Invalid, err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
rdr := sxreader.MakeReader(resp.Body)
obj, err := rdr.Read()
if resp.StatusCode != http.StatusCreated {
return api.InvalidZID, statusToError(resp)
return id.Invalid, statusToError(resp)
}
if err != nil {
return api.InvalidZID, err
return id.Invalid, err
}
return makeZettelID(obj)
}
var bsLF = []byte{'\n'}
// QueryZettel returns a list of all Zettel.
func (c *Client) QueryZettel(ctx context.Context, query string) ([][]byte, error) {
ub := c.newURLBuilder('z').AppendQuery(query)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
switch resp.StatusCode {
case http.StatusOK:
case http.StatusNoContent:
return nil, nil
default:
return nil, statusToError(resp)
}
if err != nil {
return nil, err
}
lines := bytes.Split(data, bsLF)
if len(lines[len(lines)-1]) == 0 {
lines = lines[:len(lines)-1]
}
return lines, nil
}
// QueryZettelData returns a list of zettel metadata.
func (c *Client) QueryZettelData(ctx context.Context, query string) (string, string, []api.ZidMetaRights, error) {
ub := c.newURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData).AppendQuery(query)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
if err != nil {
return "", "", nil, err
}
defer resp.Body.Close()
rdr := sxreader.MakeReader(resp.Body)
obj, err := rdr.Read()
switch resp.StatusCode {
case http.StatusOK:
case http.StatusNoContent:
return "", "", nil, nil
default:
return "", "", nil, statusToError(resp)
}
if err != nil {
return "", "", nil, err
}
vals, err := sexp.ParseList(obj, "yppp")
if err != nil {
return "", "", nil, err
}
qVals, err := sexp.ParseList(vals[1], "ys")
if err != nil {
return "", "", nil, err
}
hVals, err := sexp.ParseList(vals[2], "ys")
if err != nil {
return "", "", nil, err
}
metaList, err := parseMetaList(vals[3].(*sx.Pair))
return qVals[1].String(), hVals[1].String(), metaList, err
}
func parseMetaList(metaPair *sx.Pair) ([]api.ZidMetaRights, error) {
if metaPair == nil {
return nil, fmt.Errorf("no zettel list")
}
if errSym := sexp.CheckSymbol(metaPair.Car(), "list"); errSym != nil {
return nil, errSym
}
var result []api.ZidMetaRights
for node := metaPair.Cdr(); !sx.IsNil(node); {
elem, isPair := sx.GetPair(node)
if !isPair {
return nil, fmt.Errorf("meta-list not a proper list: %v", metaPair.String())
}
node = elem.Cdr()
vals, err := sexp.ParseList(elem.Car(), "yppp")
if err != nil {
return nil, err
}
if errSym := sexp.CheckSymbol(vals[0], "zettel"); errSym != nil {
return nil, errSym
}
idVals, err := sexp.ParseList(vals[1], "yi")
if err != nil {
return nil, err
}
if errSym := sexp.CheckSymbol(idVals[0], "id"); errSym != nil {
return nil, errSym
}
zid, err := makeZettelID(idVals[1])
if err != nil {
return nil, err
}
meta, err := sexp.ParseMeta(vals[2].(*sx.Pair))
if err != nil {
return nil, err
}
rights, err := sexp.ParseRights(vals[3])
if err != nil {
return nil, err
}
result = append(result, api.ZidMetaRights{
ID: zid,
Meta: meta,
Rights: rights,
})
}
return result, nil
}
func makeZettelID(obj sx.Object) (api.ZettelID, error) {
func makeZettelID(obj sx.Object) (id.Zid, error) {
val, isInt64 := obj.(sx.Int64)
if !isInt64 || val <= 0 {
return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val)
return id.Invalid, fmt.Errorf("invalid zettel ID: %v", val)
}
sVal := strconv.FormatInt(int64(val), 10)
if len(sVal) < 14 {
sVal = "00000000000000"[0:14-len(sVal)] + sVal
}
zid := api.ZettelID(sVal)
if !zid.IsValid() {
return api.InvalidZID, fmt.Errorf("invalid zettel ID: %v", val)
zid, err := id.Parse(sVal)
if err != nil {
return id.Invalid, fmt.Errorf("invalid zettel ID %v: %w", val, err)
}
return zid, nil
}
// QueryAggregate returns a aggregate as a result of a query.
// It is most often used in a query with an action, where the action is either
// a metadata key of type Word, WordSet, or TagSet.
func (c *Client) QueryAggregate(ctx context.Context, query string) (api.Aggregate, error) {
lines, err := c.QueryZettel(ctx, query)
if err != nil {
return nil, err
}
if len(lines) == 0 {
return nil, nil
}
agg := make(api.Aggregate, len(lines))
for _, line := range lines {
if fields := bytes.Fields(line); len(fields) > 1 {
key := string(fields[0])
for _, field := range fields[1:] {
if zid := api.ZettelID(string(field)); zid.IsValid() {
agg[key] = append(agg[key], zid)
}
}
}
}
return agg, nil
}
// UpdateZettel updates an existing zettel, specified by its zettel identifier.
// GetZettel returns a zettel as a string.
func (c *Client) GetZettel(ctx context.Context, zid api.ZettelID, part string) ([]byte, error) {
ub := c.newURLBuilder('z').SetZid(zid)
if part != "" && part != api.PartContent {
ub.AppendKVQuery(api.QueryKeyPart, part)
}
resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
switch resp.StatusCode {
case http.StatusOK:
case http.StatusNoContent:
return nil, nil
default:
return nil, statusToError(resp)
}
return data, err
}
//
// GetZettelData returns a zettel as a struct of its parts.
func (c *Client) GetZettelData(ctx context.Context, zid api.ZettelID) (api.ZettelData, error) {
ub := c.newURLBuilder('z').SetZid(zid)
ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
ub.AppendKVQuery(api.QueryKeyPart, api.PartZettel)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
if err == nil {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return api.ZettelData{}, statusToError(resp)
}
rdr := sxreader.MakeReader(resp.Body)
obj, err2 := rdr.Read()
if err2 == nil {
return sexp.ParseZettel(obj)
}
}
return api.ZettelData{}, err
}
// data contains the zettel metadata and content, as it is stored in a file in a zettel box,
// GetParsedZettel return a parsed zettel in a defined encoding.
func (c *Client) GetParsedZettel(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) {
return c.getZettelString(ctx, zid, enc, true)
}
// or as returned by [Client.GetZettel].
// GetEvaluatedZettel return an evaluated zettel in a defined encoding.
func (c *Client) GetEvaluatedZettel(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum) ([]byte, error) {
return c.getZettelString(ctx, zid, enc, false)
}
func (c *Client) getZettelString(ctx context.Context, zid api.ZettelID, enc api.EncodingEnum, parseOnly bool) ([]byte, error) {
ub := c.newURLBuilder('z').SetZid(zid)
ub.AppendKVQuery(api.QueryKeyEncoding, enc.String())
ub.AppendKVQuery(api.QueryKeyPart, api.PartContent)
if parseOnly {
ub.AppendKVQuery(api.QueryKeyParseOnly, "")
}
resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
case http.StatusNoContent:
default:
return nil, statusToError(resp)
}
return io.ReadAll(resp.Body)
}
// Metadata is separated from zettel content by an empty line.
// GetParsedSz returns an parsed zettel as a Sexpr-decoded data structure.
func (c *Client) GetParsedSz(ctx context.Context, zid api.ZettelID, part string, sf sx.SymbolFactory) (sx.Object, error) {
return c.getSz(ctx, zid, part, true, sf)
}
// GetEvaluatedSz returns an evaluated zettel as a Sexpr-decoded data structure.
func (c *Client) GetEvaluatedSz(ctx context.Context, zid api.ZettelID, part string, sf sx.SymbolFactory) (sx.Object, error) {
return c.getSz(ctx, zid, part, false, sf)
}
func (c *Client) getSz(ctx context.Context, zid api.ZettelID, part string, parseOnly bool, sf sx.SymbolFactory) (sx.Object, error) {
ub := c.newURLBuilder('z').SetZid(zid)
ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingSz)
if part != "" {
ub.AppendKVQuery(api.QueryKeyPart, part)
}
if parseOnly {
ub.AppendKVQuery(api.QueryKeyParseOnly, "")
}
resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, statusToError(resp)
}
return sxreader.MakeReader(bufio.NewReaderSize(resp.Body, 8), sxreader.WithSymbolFactory(sf)).Read()
}
// GetMetaData returns the metadata of a zettel.
func (c *Client) GetMetaData(ctx context.Context, zid api.ZettelID) (api.MetaRights, error) {
ub := c.newURLBuilder('z').SetZid(zid)
ub.AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
ub.AppendKVQuery(api.QueryKeyPart, api.PartMeta)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
if err != nil {
return api.MetaRights{}, err
}
defer resp.Body.Close()
rdr := sxreader.MakeReader(resp.Body)
obj, err := rdr.Read()
if resp.StatusCode != http.StatusOK {
return api.MetaRights{}, statusToError(resp)
}
if err != nil {
return api.MetaRights{}, err
}
vals, err := sexp.ParseList(obj, "ypp")
if err != nil {
return api.MetaRights{}, err
}
if errSym := sexp.CheckSymbol(vals[0], "list"); errSym != nil {
return api.MetaRights{}, err
}
meta, err := sexp.ParseMeta(vals[1].(*sx.Pair))
if err != nil {
return api.MetaRights{}, err
}
rights, err := sexp.ParseRights(vals[2])
if err != nil {
return api.MetaRights{}, err
}
return api.MetaRights{
Meta: meta,
Rights: rights,
}, nil
}
// UpdateZettel updates an existing zettel.
func (c *Client) UpdateZettel(ctx context.Context, zid api.ZettelID, data []byte) error {
ub := c.newURLBuilder('z').SetZid(zid)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, bytes.NewBuffer(data), nil)
func (c *Client) UpdateZettel(ctx context.Context, zid id.Zid, data []byte) error {
ub := c.NewURLBuilder('z').SetZid(zid)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, bytes.NewBuffer(data))
if err != nil {
return err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusNoContent {
return statusToError(resp)
}
return nil
}
// UpdateZettelData updates an existing zettel.
func (c *Client) UpdateZettelData(ctx context.Context, zid api.ZettelID, data api.ZettelData) error {
// UpdateZettelData updates an existing zettel, specified by its zettel identifier.
func (c *Client) UpdateZettelData(ctx context.Context, zid id.Zid, data api.ZettelData) error {
var buf bytes.Buffer
if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil {
return err
}
ub := c.newURLBuilder('z').SetZid(zid).AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, &buf, nil)
ub := c.NewURLBuilder('z').SetZid(zid).AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, &buf)
if err != nil {
return err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusNoContent {
return statusToError(resp)
}
return nil
}
// RenameZettel renames a zettel.
func (c *Client) RenameZettel(ctx context.Context, oldZid, newZid api.ZettelID) error {
ub := c.newURLBuilder('z').SetZid(oldZid)
h := http.Header{
api.HeaderDestination: {c.newURLBuilder('z').SetZid(newZid).String()},
}
resp, err := c.buildAndExecuteRequest(ctx, api.MethodMove, ub, nil, h)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return statusToError(resp)
}
return nil
}
// DeleteZettel deletes a zettel with the given identifier.
func (c *Client) DeleteZettel(ctx context.Context, zid api.ZettelID) error {
ub := c.newURLBuilder('z').SetZid(zid)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodDelete, ub, nil, nil)
func (c *Client) DeleteZettel(ctx context.Context, zid id.Zid) error {
ub := c.NewURLBuilder('z').SetZid(zid)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodDelete, ub, nil)
if err != nil {
return err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusNoContent {
return statusToError(resp)
}
return nil
}
// ExecuteCommand will execute a given command at the Zettelstore.
//
// See [API commands] for a list of valid commands.
//
// [API commands]: https://zettelstore.de/manual/h/00001012080100
func (c *Client) ExecuteCommand(ctx context.Context, command api.Command) error {
ub := c.newURLBuilder('x').AppendKVQuery(api.QueryKeyCommand, string(command))
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, nil, nil)
ub := c.NewURLBuilder('x').AppendKVQuery(api.QueryKeyCommand, string(command))
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, nil)
if err != nil {
return err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusNoContent {
return statusToError(resp)
}
return nil
}
// GetVersionInfo returns version information..
func (c *Client) GetVersionInfo(ctx context.Context) (VersionInfo, error) {
resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, c.newURLBuilder('x'), nil, nil)
if err != nil {
return VersionInfo{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return VersionInfo{}, statusToError(resp)
}
rdr := sxreader.MakeReader(resp.Body)
obj, err := rdr.Read()
if err == nil {
if vals, errVals := sexp.ParseList(obj, "iiiss"); errVals == nil {
return VersionInfo{
Major: int(vals[0].(sx.Int64)),
Minor: int(vals[1].(sx.Int64)),
Patch: int(vals[2].(sx.Int64)),
Info: vals[3].(sx.String).String(),
Hash: vals[4].(sx.String).String(),
}, nil
}
}
return VersionInfo{}, err
}
// VersionInfo contains version information.
type VersionInfo struct {
Major int
Minor int
Patch int
Info string
Hash string
}
|