-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
124 lines (99 loc) · 2.64 KB
/
api.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
package utils
import (
"encoding/json"
"errors"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator"
)
// tError data structure to pass an error data
type tError struct {
Code int `json:"code"`
Message string `json:"message"`
Refs *string `json:"refs,omitempty"`
}
// TReqResp is a base data structure to pass request/response data
type TReqResp struct {
c *gin.Context
Kind string `json:"kind"`
Success bool `json:"success"`
Values interface{} `json:"values,omitempty"`
Error *tError `json:"error,omitempty"`
}
// NewReqRespStruct creates TReqResp object
func NewReqRespStruct(c *gin.Context, kind string) TReqResp {
return TReqResp{c: c, Kind: kind}
}
// Ok will pass the value then wraps gin json response
func (r *TReqResp) Ok(values interface{}) {
r.Success = true
r.Values = values
r.c.JSON(http.StatusOK, *r)
return
}
// BadReq will pass the error message and error reference then wraps gin json response
func (r *TReqResp) BadReq(message string, errRef error) {
r.Success = false
r.Error = &tError{
Code: http.StatusBadRequest,
Message: message,
}
if errRef != nil {
er := errRef.Error()
r.Error.Refs = &er
// Custom mongodb unauthorized
if er == "(Unauthorized) command find requires authentication" {
r.Error.Code = http.StatusInternalServerError
}
}
r.c.JSON(r.Error.Code, r)
}
// Unauthorized will pass the error message then wraps gin json response
func (r *TReqResp) Unauthorized(message string, errRef error) {
r.Success = false
r.Error = &tError{
Code: 401,
Message: message,
}
if errRef != nil {
er := errRef.Error()
r.Error.Refs = &er
// Custom mongodb unauthorized
if er == "(Unauthorized) command find requires authentication" {
r.Error.Code = http.StatusInternalServerError
}
}
r.c.JSON(r.Error.Code, *r)
}
// InternalErr will pass the error message and error reference then wraps gin json response
func (r *TReqResp) InternalErr(message string, errRef error) {
r.Success = false
r.Error = &tError{
Code: http.StatusInternalServerError,
Message: message,
}
if errRef != nil {
er := errRef.Error()
r.Error.Refs = &er
}
r.c.JSON(r.Error.Code, *r)
}
func ParseKindAndBody(c *gin.Context, kind string, body interface{}) error {
var req TReqResp
if err := c.BindJSON(&req); err != nil {
log.Println("didn't parse correctly:", err.Error())
return err
}
if req.Kind != kind {
return errors.New("unknown kind")
}
byteReqValues, err := json.Marshal(req.Values)
if err != nil {
return err
}
if err := json.Unmarshal(byteReqValues, body); err != nil {
return err
}
return validator.New().Struct(body)
}