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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
|
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
|
+
+
+
+
+
+
+
+
-
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
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()))
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)
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)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, bytes.NewBuffer(data))
if err != nil {
return api.InvalidZID, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return api.InvalidZID, statusToError(resp)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return api.InvalidZID, err
}
if zid := api.ZettelID(b); zid.IsValid() {
return zid, nil
}
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) {
var buf bytes.Buffer
if _, err := sx.Print(&buf, sexp.EncodeZettel(data)); err != nil {
return api.InvalidZID, err
}
ub := c.NewURLBuilder('z').AppendKVQuery(api.QueryKeyEncoding, api.EncodingData)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, &buf, nil)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, &buf)
if err != nil {
return api.InvalidZID, err
}
defer resp.Body.Close()
rdr := sxreader.MakeReader(resp.Body)
obj, err := rdr.Read()
if resp.StatusCode != http.StatusCreated {
return api.InvalidZID, statusToError(resp)
}
if err != nil {
return api.InvalidZID, 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 sz.GoValue(qVals[1]), sz.GoValue(hVals[1]), 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) {
val, isInt64 := obj.(sx.Int64)
if !isInt64 || val <= 0 {
return api.InvalidZID, 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)
}
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 or of type 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.
// TagZettel returns the tag zettel of a given tag.
//
// This method only works if c.AllowRedirect(true) was called.
func (c *Client) TagZettel(ctx context.Context, tag string) (api.ZettelID, error) {
return c.fetchTagOrRoleZettel(ctx, api.QueryKeyTag, tag)
}
// data contains the zettel metadata and content, as it is stored in a file in a zettel box,
// RoleZettel returns the tag zettel of a given tag.
//
// This method only works if c.AllowRedirect(true) was called.
func (c *Client) RoleZettel(ctx context.Context, role string) (api.ZettelID, error) {
return c.fetchTagOrRoleZettel(ctx, api.QueryKeyRole, role)
}
// or as returned by [Client.GetZettel].
func (c *Client) fetchTagOrRoleZettel(ctx context.Context, key, val string) (api.ZettelID, error) {
if c.client.CheckRedirect == nil {
panic("client does not allow to track redirect")
}
ub := c.NewURLBuilder('z').AppendKVQuery(key, val)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodGet, ub, nil, nil)
if err != nil {
return api.InvalidZID, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return api.InvalidZID, err
}
// Metadata is separated from zettel content by an empty line.
switch resp.StatusCode {
case http.StatusNotFound:
return "", nil
case http.StatusFound:
zid := api.ZettelID(data)
if zid.IsValid() {
return zid, nil
}
return api.InvalidZID, nil
default:
return api.InvalidZID, statusToError(resp)
}
}
// 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
}
// 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)
}
// 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)
}
// GetParsedSz returns an parsed zettel as a Sexpr-decoded data structure.
func (c *Client) GetParsedSz(ctx context.Context, zid api.ZettelID, part string) (sx.Object, error) {
return c.getSz(ctx, zid, part, true)
}
// GetEvaluatedSz returns an evaluated zettel as a Sexpr-decoded data structure.
func (c *Client) GetEvaluatedSz(ctx context.Context, zid api.ZettelID, part string) (sx.Object, error) {
return c.getSz(ctx, zid, part, false)
}
func (c *Client) getSz(ctx context.Context, zid api.ZettelID, part string, parseOnly bool) (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)).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)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, bytes.NewBuffer(data))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return statusToError(resp)
}
return nil
}
// UpdateZettelData updates an existing zettel.
// UpdateZettelData updates an existing zettel, specified by its zettel identifier.
func (c *Client) UpdateZettelData(ctx context.Context, zid api.ZettelID, 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)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPut, ub, &buf)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return statusToError(resp)
}
return nil
}
// RenameZettel renames a zettel.
//
// This function is deprecated and will be removed in v0.19 (or later).
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)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodDelete, ub, nil)
if err != nil {
return err
}
defer 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)
resp, err := c.buildAndExecuteRequest(ctx, http.MethodPost, ub, nil)
if err != nil {
return err
}
defer 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).GetValue(),
Hash: vals[4].(sx.String).GetValue(),
}, nil
}
}
return VersionInfo{}, err
}
// VersionInfo contains version information.
type VersionInfo struct {
Major int
Minor int
Patch int
Info string
Hash string
}
// GetApplicationZid returns the zettel identifier used to configure client
// application with the given name.
func (c *Client) GetApplicationZid(ctx context.Context, appname string) (api.ZettelID, error) {
mr, err := c.GetMetaData(ctx, api.ZidAppDirectory)
if err != nil {
return api.InvalidZID, err
}
key := appname + "-zid"
val, found := mr.Meta[key]
if !found {
return api.InvalidZID, fmt.Errorf("no application registered: %v", appname)
}
if zid := api.ZettelID(val); zid.IsValid() {
return zid, nil
}
return api.InvalidZID, fmt.Errorf("invalid identifier for application %v: %v", appname, val)
}
// Get executes a GET request to the given URL and returns the read data.
func (c *Client) Get(ctx context.Context, ub *api.URLBuilder) ([]byte, error) {
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:
return nil, nil
default:
return nil, statusToError(resp)
}
data, err := io.ReadAll(resp.Body)
return data, err
}
|