-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonapi_helper.go
158 lines (139 loc) · 3.87 KB
/
jsonapi_helper.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
package api
import (
"log"
"net/http"
)
// Relationships user data
type Relationships struct {
User RelationshipsUser `json:"user"`
}
// RelationshipsUser user data
type RelationshipsUser struct {
Data RelationshipsUserData `json:"data"`
}
// RelationshipsUserData user data
type RelationshipsUserData struct {
Type string `json:"type"`
ID string `json:"id" binding:"required"`
}
// Meta jsonapi
type Meta struct {
Total int64 `json:"total"`
Max int64 `json:"max"`
}
// IncludeItem included item for jsonapi
type IncludeItem struct {
Type string `json:"type"`
Attributes interface{} `json:"attributes"`
}
// JsonapiCreateEntityBody jsonapi.org top wrap for request
type JsonapiCreateEntityBody struct {
Data struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes SampleEntity `json:"attributes"`
Relationships Relationships `json:"relationships" binding:"required"`
} `json:"data"`
}
// ItemDataGen General
type ItemDataGen struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes interface{} `json:"attributes"`
}
// JsonapiDataGen jsonapi.org top wrap
type JsonapiDataGen struct {
Data []*ItemDataGen `json:"data"`
Meta interface{} `json:"meta"`
}
// JsonapiError jsonapi.org error item
type JsonapiError struct {
APIErrors []*JsonapiErrorItem `json:"errors"`
}
// JsonapiErrorItem jsonapi.org error wrap
type JsonapiErrorItem struct {
Status string `json:"status"`
Source string `json:"source"`
Title string `json:"title"`
Detail string `json:"detail"`
}
// JsonapiResourcesResp a generic jsonapi multiple resource response with "type" and "id"
func JsonapiResourcesResp(status int, resType, resID string, metaData interface{}, attrs ...interface{}) (int, interface{}) {
var jsonapiDataItem []*ItemDataGen
for _, attr := range attrs {
jsonapiDataItem = append(jsonapiDataItem, &ItemDataGen{
Type: resType,
ID: resID,
Attributes: attr,
})
}
return status, JsonapiDataGen{
Data: jsonapiDataItem,
Meta: metaData,
}
}
// JsonapiResourceResp a generic jsonapi resource response with "type" and "id"
func JsonapiResourceResp(status int, resType, resID string, attr interface{}) (int, interface{}) {
return status, JsonapiDataGen{
Data: []*ItemDataGen{{
Type: resType,
ID: resID,
Attributes: attr,
}},
}
}
// JsonapiCustomError single error
func JsonapiCustomError(statusCode int, status, source, title, detail *string) (int, interface{}) {
statusStr := *status
sourceStr := *source
titleStr := *title
detailStr := *detail
if status == nil {
statusStr = http.StatusText(statusCode)
}
if source == nil {
sourceStr = "Litchi api"
}
if title == nil {
titleStr = http.StatusText(statusCode)
}
if detail == nil {
detailStr = http.StatusText(statusCode)
}
return statusCode, JsonapiError{
APIErrors: []*JsonapiErrorItem{
{Status: statusStr,
Source: sourceStr,
Title: titleStr,
Detail: detailStr}},
}
}
// JsonapiErrorResp single error json api body
func JsonapiErrorResp(status int, msg string) (int, interface{}) {
log.Printf("Litchi: API error response, status %d error: %s \n", status, msg)
return status, JsonapiError{
APIErrors: []*JsonapiErrorItem{
{
Status: http.StatusText(status),
Source: "Litchi api",
Title: http.StatusText(status),
Detail: msg,
},
},
}
}
// JsonapiErrorsResp multiple error json api body
func JsonapiErrorsResp(status int, msges ...string) (int, interface{}) {
var jsonapiErrItem []*JsonapiErrorItem
for _, msg := range msges {
jsonapiErrItem = append(jsonapiErrItem, &JsonapiErrorItem{
Status: http.StatusText(status),
Source: "Litchi api",
Title: http.StatusText(status),
Detail: msg})
log.Printf("Litchi: API error response, status %d error: %s \n", status, msg)
}
return status, JsonapiError{
APIErrors: jsonapiErrItem,
}
}