-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
116 lines (99 loc) · 4.01 KB
/
Copy pathresponse.go
File metadata and controls
116 lines (99 loc) · 4.01 KB
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
package restapi
import "github.com/gin-gonic/gin"
// WriteResponse writes body as a JSON response to c with the given HTTP
// status. The write is a no-op if:
// - c is nil, or
// - the response has already been written (c.Writer.Written() is true).
//
// When abort is true, the middleware chain is terminated after the write; the
// call is equivalent to [gin.Context.AbortWithStatusJSON] (or
// [gin.Context.AbortWithStatus] when body is nil). When abort is false, the
// chain continues; the call is equivalent to [gin.Context.JSON] (or
// [gin.Context.Status] when body is nil).
func (a *API) WriteResponse(c *gin.Context, statusCode int, abort bool, body *ResponseBody) {
if c == nil || c.Writer.Written() {
return
}
if abort {
if body == nil {
c.AbortWithStatus(statusCode)
return
}
c.AbortWithStatusJSON(statusCode, body)
return
}
if body == nil {
c.Status(statusCode)
c.Writer.WriteHeaderNow()
return
}
c.JSON(statusCode, body)
}
// Success writes a successful response with data as the envelope Data field.
// Additional options allow setting pagination and extra errors.
func (a *API) Success(c *gin.Context, statusCode int, data any, opts ...ResponseBodyOption) {
body := &ResponseBody{Data: data}
for _, opt := range opts {
if opt != nil {
opt(body)
}
}
a.WriteResponse(c, statusCode, false, body)
}
// SuccessBody writes a caller-supplied [ResponseBody] as a successful
// response. It is the escape hatch for exotic envelopes.
func (a *API) SuccessBody(c *gin.Context, statusCode int, body *ResponseBody) {
a.WriteResponse(c, statusCode, false, body)
}
// Error writes an error response whose HTTP status and default message come
// from the registry entry for errorCode. The middleware chain continues.
func (a *API) Error(c *gin.Context, errorCode int, opts ...ErrorOption) {
entry, _ := a.registry.Lookup(errorCode)
errorResponse := buildErrorResponse(entry, opts...)
a.WriteResponse(c, entry.StatusCode, false, &ResponseBody{Errors: []ErrorResponse{errorResponse}})
}
// ErrorBody writes a caller-supplied [ResponseBody] as an error response at
// the given HTTP status. The middleware chain continues.
func (a *API) ErrorBody(c *gin.Context, statusCode int, body *ResponseBody) {
a.WriteResponse(c, statusCode, false, body)
}
// Abort writes an error response whose HTTP status and default message come
// from the registry entry for errorCode, and terminates the middleware chain.
func (a *API) Abort(c *gin.Context, errorCode int, opts ...ErrorOption) {
entry, _ := a.registry.Lookup(errorCode)
errorResponse := buildErrorResponse(entry, opts...)
a.WriteResponse(c, entry.StatusCode, true, &ResponseBody{Errors: []ErrorResponse{errorResponse}})
}
// AbortBody writes a caller-supplied [ResponseBody] as an error response at
// the given HTTP status and terminates the middleware chain.
func (a *API) AbortBody(c *gin.Context, statusCode int, body *ResponseBody) {
a.WriteResponse(c, statusCode, true, body)
}
// WriteResponse calls Default().WriteResponse.
func WriteResponse(c *gin.Context, statusCode int, abort bool, body *ResponseBody) {
Default().WriteResponse(c, statusCode, abort, body)
}
// Success calls Default().Success.
func Success(c *gin.Context, statusCode int, data any, opts ...ResponseBodyOption) {
Default().Success(c, statusCode, data, opts...)
}
// SuccessBody calls Default().SuccessBody.
func SuccessBody(c *gin.Context, statusCode int, body *ResponseBody) {
Default().SuccessBody(c, statusCode, body)
}
// Error calls Default().Error.
func Error(c *gin.Context, errorCode int, opts ...ErrorOption) {
Default().Error(c, errorCode, opts...)
}
// ErrorBody calls Default().ErrorBody.
func ErrorBody(c *gin.Context, statusCode int, body *ResponseBody) {
Default().ErrorBody(c, statusCode, body)
}
// Abort calls Default().Abort.
func Abort(c *gin.Context, errorCode int, opts ...ErrorOption) {
Default().Abort(c, errorCode, opts...)
}
// AbortBody calls Default().AbortBody.
func AbortBody(c *gin.Context, statusCode int, body *ResponseBody) {
Default().AbortBody(c, statusCode, body)
}