-
Notifications
You must be signed in to change notification settings - Fork 199
/
shared.go
104 lines (85 loc) · 2.97 KB
/
shared.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
package shared
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
// MiddlewarePosition is the type that specifies the position of a middleware relative to the base endpoint handler
type MiddlewarePosition bool
const (
// Before indicates that the middleware should be used before the base endpoint handler
Before MiddlewarePosition = true
// After indicates that the middleware should be used after the base endpoint handler
After MiddlewarePosition = false
)
// AdditionalMiddleware holds the data needed for adding a middleware to an API endpoint
type AdditionalMiddleware struct {
Middleware gin.HandlerFunc
Position MiddlewarePosition
}
// EndpointHandlerData holds the items needed for creating a new gin HTTP endpoint
type EndpointHandlerData struct {
Path string
Method string
Handler gin.HandlerFunc
AdditionalMiddlewares []AdditionalMiddleware
}
// GenericAPIResponse defines the structure of all responses on API endpoints
type GenericAPIResponse struct {
Data interface{} `json:"data"`
Error string `json:"error"`
Code ReturnCode `json:"code"`
}
// ReturnCode defines the type defines to identify return codes
type ReturnCode string
// ReturnCodeSuccess defines a successful request
const ReturnCodeSuccess ReturnCode = "successful"
// ReturnCodeInternalError defines a request which hasn't been executed successfully due to an internal error
const ReturnCodeInternalError ReturnCode = "internal_issue"
// ReturnCodeRequestError defines a request which hasn't been executed successfully due to a bad request received
const ReturnCodeRequestError ReturnCode = "bad_request"
// ReturnCodeSystemBusy defines a request which hasn't been executed successfully due to too many requests
const ReturnCodeSystemBusy ReturnCode = "system_busy"
// RespondWith will respond with the generic API response
func RespondWith(c *gin.Context, status int, dataField interface{}, errMessage string, code ReturnCode) {
c.JSON(
status,
GenericAPIResponse{
Data: dataField,
Error: errMessage,
Code: code,
},
)
}
// RespondWithValidationError should be called when the request cannot be satisfied due to a (request) validation error
func RespondWithValidationError(c *gin.Context, err error, innerErr error) {
errMessage := fmt.Sprintf("%s: %s", err.Error(), innerErr.Error())
RespondWith(
c,
http.StatusBadRequest,
nil,
errMessage,
ReturnCodeRequestError,
)
}
// RespondWithInternalError should be called when the request cannot be satisfied due to an internal error
func RespondWithInternalError(c *gin.Context, err error, innerErr error) {
errMessage := fmt.Sprintf("%s: %s", err.Error(), innerErr.Error())
RespondWith(
c,
http.StatusInternalServerError,
nil,
errMessage,
ReturnCodeInternalError,
)
}
// RespondWithSuccess should be called when the request can be satisfied
func RespondWithSuccess(c *gin.Context, data interface{}) {
RespondWith(
c,
http.StatusOK,
data,
"",
ReturnCodeSuccess,
)
}