From 44c27869a5975bb7f2e6ebd000f9000a995629d5 Mon Sep 17 00:00:00 2001 From: Federico Fapitalle Date: Mon, 28 Oct 2024 20:11:27 -0300 Subject: [PATCH] feat: adds annotation creation capability --- annotations.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 annotations.go diff --git a/annotations.go b/annotations.go new file mode 100644 index 00000000..641a6a23 --- /dev/null +++ b/annotations.go @@ -0,0 +1,48 @@ +package grabana + +import ( + "context" + "encoding/json" + "net/http" + "time" +) + +type AnnotationResponse struct { + ID uint `json:"id"` + Message string `json:"message"` +} + +func (client *Client) AddAnnotation(ctx context.Context, text string, tags []string) (*AnnotationResponse, error) { + buf, err := json.Marshal(struct { + Time int64 `json:"title"` + Updated int64 `json:"updated"` + Text string `json:"text"` + Tags []string `json:"tags"` + }{ + Time: time.Now().UnixMilli(), + Updated: time.Now().UnixMilli(), + Text: text, + Tags: tags, + }) + if err != nil { + return nil, err + } + + resp, err := client.sendJSON(ctx, http.MethodPost, "/api/annotations", buf) + if err != nil { + return nil, err + } + + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + return nil, client.httpError(resp) + } + + var respann AnnotationResponse + if err := decodeJSON(resp.Body, &respann); err != nil { + return nil, err + } + + return &respann, nil +}