-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse_json.go
168 lines (128 loc) · 5.4 KB
/
response_json.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package webfmwk
import (
"bytes"
"fmt"
"log/slog"
"net/http"
"github.com/segmentio/encoding/json"
)
const _contentType = "application/json; charset=UTF-8"
type JSONHTTPResponse interface { //nolint: interfacebloat
// JSONOk return the interface with an http.StatusOK (200).
JSONOk(content interface{}) error
// JSONCreated return the interface with an http.StatusCreated (201).
JSONCreated(content interface{}) error
// JSONAccepted return the interface with an http.StatusAccepted (202).
JSONAccepted(content interface{}) error
// JSONNoContent return an empty payload an http.StatusNoContent (204).
JSONNoContent() error
// JSONBadRequest return the interface with an http.StatusBadRequest (400).
JSONBadRequest(content interface{}) error
// JSONUnauthorized return the interface with an http.StatusUnauthorized (401).
JSONUnauthorized(content interface{}) error
// JSONForbiden return the interface with an http.StatusForbidden (403).
JSONForbidden(content interface{}) error
// JSONNoContent return the interface with an http.StatusNotFound (404).
JSONNotFound(content interface{}) error
// JSONMethodNotAllowed return the interface with an http.NotAllowed (405).
JSONMethodNotAllowed(content interface{}) error
// JSONConflict return the interface with an http.StatusConflict (409).
JSONConflict(content interface{}) error
// JSONUnauthorized return the interface with an http.StatusUnprocessableEntity (422).
JSONUnprocessable(content interface{}) error
// JSONInternalError return the interface with an http.StatusInternalServerError (500).
JSONInternalError(content interface{}) error
// JSONNotImplemented return the interface with an http.StatusNotImplemented (501).
JSONNotImplemented(content interface{}) error
}
// JSONResponse interface is used to answer JSON content to the client.
type JSONResponse interface {
JSONHTTPResponse
// JSONBlob answer the JSON content with the status code op.
JSONBlob(op int, content []byte) error
// JSON answer the JSON content with the status code op.
JSON(op int, content interface{}) error
}
// JSONBlob sent a JSON response already encoded
func (c *icontext) JSONBlob(statusCode int, content []byte) error {
var (
out bytes.Buffer
run = func() error {
if c.IsPretty() {
return json.Indent(&out, content, "", " ")
}
return json.Compact(&out, content)
}
)
if e := run(); e != nil {
c.slog.Error("cannot prettying the content", slog.Any("error", e))
} else {
content = out.Bytes()
}
if statusCode != http.StatusNoContent {
c.SetContentType(_contentType)
c.SetHeader("Produce", _contentType)
}
c.SetHeader("Accept", _contentType)
return c.response(statusCode, content)
}
// JSON implement JSONResponse by returning a JSON encoded response.
func (c *icontext) JSON(statusCode int, content interface{}) error {
data, e := json.Marshal(content)
if e != nil {
return fmt.Errorf("cannot json response : %w", e)
}
return c.JSONBlob(statusCode, data)
}
// JSONOk implement JSONResponse by returning a JSON encoded 200 response.
func (c *icontext) JSONOk(content interface{}) error {
return c.JSON(http.StatusOK, content)
}
// JSONCreated implement JSONResponse by returning a JSON encoded 201 response.
func (c *icontext) JSONCreated(content interface{}) error {
return c.JSON(http.StatusCreated, content)
}
// JSONAccepted implement JSONResponse by returning a JSON encoded 202 response.
func (c *icontext) JSONAccepted(content interface{}) error {
return c.JSON(http.StatusAccepted, content)
}
// JSONNoContent implement JSONResponse by returning a JSON encoded 204 response.
func (c *icontext) JSONNoContent() error {
return c.JSON(http.StatusNoContent, nil)
}
// JSONBadRequest implement JSONResponse by returning a JSON encoded 400 response.
func (c *icontext) JSONBadRequest(content interface{}) error {
return c.JSON(http.StatusBadRequest, content)
}
// JSONUnauthorized implement JSONResponse by returning a JSON encoded 401 response.
func (c *icontext) JSONUnauthorized(content interface{}) error {
return c.JSON(http.StatusUnauthorized, content)
}
// JSONForbidden implement JSONResponse by returning a JSON encoded 403 response.
func (c *icontext) JSONForbidden(content interface{}) error {
return c.JSON(http.StatusForbidden, content)
}
// JSONNotFound implement JSONResponse by returning a JSON encoded 404 response.
func (c *icontext) JSONNotFound(content interface{}) error {
return c.JSON(http.StatusNotFound, content)
}
// JSONMethodNotAllowed implement JSONResponse by returning a JSON encoded 405 response.
func (c *icontext) JSONMethodNotAllowed(content interface{}) error {
return c.JSON(http.StatusMethodNotAllowed, content)
}
// JSONConflict implement JSONResponse by returning a JSON encoded 429 response.
func (c *icontext) JSONConflict(content interface{}) error {
return c.JSON(http.StatusConflict, content)
}
// JSONUnprocessable implement JSONResponse by returning a JSON encoded 422 response.
func (c *icontext) JSONUnprocessable(content interface{}) error {
return c.JSON(http.StatusUnprocessableEntity, content)
}
// JSONInternalError implement JSONResponse by returning a JSON encoded 500 response.
func (c *icontext) JSONInternalError(content interface{}) error {
return c.JSON(http.StatusInternalServerError, content)
}
// JSONNotImplemented implement JSONResponse by returning a JSON encoded 501 response.
func (c *icontext) JSONNotImplemented(content interface{}) error {
return c.JSON(http.StatusNotImplemented, content)
}