/
response.go
37 lines (31 loc) · 1018 Bytes
/
response.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
package kit
import (
"encoding/json"
"net/http"
)
// NewJSONStatusResponse allows users to respond with a specific HTTP status code and
// a JSON serialized response.
func NewJSONStatusResponse(res interface{}, code int) *JSONStatusResponse {
return &JSONStatusResponse{res: res, code: code}
}
// JSONStatusResponse implements:
// `httptransport.StatusCoder` to allow users to respond with the given
// response with a non-200 status code.
// `json.Marshaler` so it can wrap JSON Endpoint responses.
// `error` so it can be used to respond as an error within the go-kit stack.
type JSONStatusResponse struct {
code int
res interface{}
}
// StatusCode is to implement httptransport.StatusCoder
func (c *JSONStatusResponse) StatusCode() int {
return c.code
}
// MarshalJSON is to implement json.Marshaler
func (c *JSONStatusResponse) MarshalJSON() ([]byte, error) {
return json.Marshal(c.res)
}
// Error is to implement error
func (c *JSONStatusResponse) Error() string {
return http.StatusText(c.code)
}