/
response.go
93 lines (78 loc) · 2.6 KB
/
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
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
package httpencoder
import (
"context"
"encoding/json"
"net/http"
)
// Predefined http encoder content type
const (
ContentTypeHeader = "Content-Type"
ContentType = "application/json; charset=utf-8"
)
type (
// Response struct
Response struct {
Data interface{} `json:"data,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty"`
}
// ListResponse struct
ListResponse struct {
Data interface{} `json:"data,omitempty"`
Meta ListMeta `json:"meta,omitempty"`
}
// BoolResultResponse struct
BoolResultResponse struct {
Result bool `json:"result"`
Message string `json:"message,omitempty"`
}
ListMeta struct {
TotalItems int64 `json:"total_items,omitempty"`
ItemsPerPage int32 `json:"items_per_page,omitempty"`
Page int32 `json:"page,omitempty"`
Limit int32 `json:"limit,omitempty"`
Offset int32 `json:"offset,omitempty"`
}
)
// EncodeResponse is the common method to encode all response types to the
// client. I chose to do it this way because, since we're using JSON, there's no
// reason to provide anything more specific. It's certainly possible to
// specialize on a per-response (per-method) basis.
func EncodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
w.Header().Set(ContentTypeHeader, ContentType)
if response == nil {
w.WriteHeader(http.StatusCreated)
return nil
}
switch r := response.(type) {
case Response, BoolResultResponse, ListResponse:
return json.NewEncoder(w).Encode(response)
case bool:
return json.NewEncoder(w).Encode(BoolResult(r, ""))
case ErrorResponse:
w.WriteHeader(r.Code)
return json.NewEncoder(w).Encode(response)
}
return json.NewEncoder(w).Encode(Response{Data: response})
}
// EncodeResponse is the common method to encode all response types to the
// client. I chose to do it this way because, since we're using JSON, there's no
// reason to provide anything more specific. It's certainly possible to
// specialize on a per-response (per-method) basis.
func EncodeResponseAsIs(_ context.Context, w http.ResponseWriter, response interface{}) error {
w.Header().Set(ContentTypeHeader, ContentType)
if response == nil {
w.WriteHeader(http.StatusCreated)
return nil
}
switch r := response.(type) {
case Response, BoolResultResponse, ListResponse:
return json.NewEncoder(w).Encode(response)
case bool:
return json.NewEncoder(w).Encode(BoolResult(r, ""))
}
return json.NewEncoder(w).Encode(response)
}
// BoolResult response helper
func BoolResult(result bool, msg string) BoolResultResponse {
return BoolResultResponse{Result: result, Message: msg}
}