/
standard_response.go
55 lines (48 loc) · 1.49 KB
/
standard_response.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
package goweb
import "net/http"
// The standard API response object
type standardResponse struct {
S int `json:"status"`
D interface{} `json:"data"`
E []string `json:"error"`
}
// The standard API response object
type paginatedResponse struct {
S int `json:"status"`
D interface{} `json:"data"`
E []string `json:"error"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Count int `json:"total_count"`
}
// Makes a standardResponse object with the specified settings
func makeStandardResponse() *standardResponse {
response := new(standardResponse)
//response.C = ""
response.S = 200
response.E = nil
return response
}
// Makes a standardResponse object with the specified settings
func makePaginatedResponse() *paginatedResponse {
response := new(paginatedResponse)
response.S = 200
response.E = nil
return response
}
// Makes a successful standardResponse object with the specified settings
func makeSuccessfulStandardResponse(context string, statusCode int, data interface{}) *standardResponse {
response := makeStandardResponse()
//response.C = context
response.S = statusCode
response.D = data
return response
}
// Makes an unsuccessful standardResponse object with the specified settings
func makeFailureStandardResponse(context string, statusCode int) *standardResponse {
response := makeStandardResponse()
//response.C = context
response.S = statusCode
response.E = []string{http.StatusText(statusCode)}
return response
}