-
Notifications
You must be signed in to change notification settings - Fork 57
/
response_util.go
49 lines (44 loc) · 956 Bytes
/
response_util.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
package resource
import (
"encoding/json"
"net/http"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)
// SendPlainText sends a plain text response.
func SendPlainText(sender backend.CallResourceResponseSender, text string) error {
return sendResourceResponse(
sender,
http.StatusOK,
map[string][]string{
"content-type": {"text/plain"},
},
[]byte(text),
)
}
// SendJSON sends a JSON response.
func SendJSON(sender backend.CallResourceResponseSender, obj interface{}) error {
body, err := json.Marshal(obj)
if err != nil {
return err
}
return sendResourceResponse(
sender,
http.StatusOK,
map[string][]string{
"content-type": {"application/json"},
},
body,
)
}
func sendResourceResponse(
sender backend.CallResourceResponseSender,
status int,
headers map[string][]string,
body []byte,
) error {
return sender.Send(&backend.CallResourceResponse{
Status: status,
Headers: headers,
Body: body,
})
}