-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoreapi.go
98 lines (82 loc) · 2.34 KB
/
coreapi.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
package coreapi
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
type apiResponse struct {
Status string `json:"status"`
Error string `json:"error,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
}
type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}
// Balerter is the main struct for the coreapi package.
type Balerter struct {
// Alert provides access to the alert module
Alert ModuleAlert
// Datasource provides access to the datasource module
Datasource ModuleDatasource
// KV provides access to the kv module
KV ModuleKV
// Log provides access to the log module
Log ModuleLog
// TLS provides access to the tls module
TLS ModuleTLS
// Runtime provides access to the runtime module
Runtime ModuleRuntime
// Chart provides access to the chart module
Chart ModuleChart
address string
authToken string
client httpClient
}
type requestFunc func(path, contentType string, body []byte) ([]byte, error)
// New creates a new Balerter instance.
// address is the address of the balerter server.
// authToken is the authentication token for the balerter server. Pass empty token, if auth is not use.
func New(address, authToken string) *Balerter {
c := &Balerter{
authToken: authToken,
address: strings.Trim(address, "/"),
client: &http.Client{},
}
c.Alert = ModuleAlert{rf: c.request}
c.Datasource = ModuleDatasource{rf: c.request}
c.KV = ModuleKV{rf: c.request}
c.Log = ModuleLog{rf: c.request}
c.TLS = ModuleTLS{rf: c.request}
c.Runtime = ModuleRuntime{rf: c.request}
c.Chart = ModuleChart{rf: c.request}
return c
}
func (b *Balerter) request(path, contentType string, body []byte) ([]byte, error) {
u := fmt.Sprintf("%s/%s", b.address, path)
req, errReq := http.NewRequest(http.MethodPost, u, bytes.NewReader(body))
if errReq != nil {
return nil, errReq
}
if contentType != "" {
req.Header.Add("Content-Type", contentType)
}
if b.authToken != "" {
req.Header.Add("Authorization", b.authToken)
}
resp, errDo := b.client.Do(req)
if errDo != nil {
return nil, errDo
}
defer resp.Body.Close()
r := apiResponse{}
errRead := json.NewDecoder(resp.Body).Decode(&r)
if errRead != nil {
return nil, fmt.Errorf("error decode response, %w", errRead)
}
if r.Status == "error" {
return nil, fmt.Errorf("%s", r.Error)
}
return r.Result, nil
}