forked from grafana-cold-storage/metrictank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.go
More file actions
46 lines (39 loc) · 769 Bytes
/
Copy pathjson.go
File metadata and controls
46 lines (39 loc) · 769 Bytes
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
package response
import (
"encoding/json"
)
type Json struct {
code int
body interface{}
jsonp string
}
func NewJson(code int, body interface{}, jsonp string) *Json {
return &Json{
code: code,
body: body,
jsonp: jsonp,
}
}
func (r *Json) Code() int {
return r.code
}
func (r *Json) Close() {
//NOOP
return
}
func (r *Json) Body() (buf []byte, err error) {
buf, err = json.Marshal(r.body)
if r.jsonp == "" || err != nil {
return buf, err
}
buf = append([]byte(r.jsonp+"("), buf...)
buf = append(buf, byte(')'))
return buf, err
}
func (r *Json) Headers() (headers map[string]string) {
headers = map[string]string{"content-type": "application/json"}
if r.jsonp != "" {
headers["content-type"] = "text/javascript"
}
return headers
}