-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
89 lines (75 loc) · 1.81 KB
/
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
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
package celerity
import (
"errors"
"net/http"
)
// Response - The response object reurned by an endpoint.
type Response struct {
StatusCode int
Data interface{}
Error error
Meta map[string]interface{}
Header http.Header
raw []byte
Handled bool
}
// NewResponse - Create a new response object
func NewResponse() Response {
return Response{
Meta: map[string]interface{}{},
Header: http.Header{},
StatusCode: 200,
}
}
// NewErrorResponse - Return a new error response
func NewErrorResponse(status int, message string) Response {
return Response{
StatusCode: status,
Error: errors.New(message),
}
}
// StatusText returns the text version of the StatusCode
func (r *Response) StatusText() string {
return http.StatusText(r.StatusCode)
}
// Success returns true if the response was marked succcessful and if an error
// is not present
func (r *Response) Success() bool {
return r.Error == nil
}
// IsRaw determens if the response is a raw response
func (r *Response) IsRaw() bool {
return len(r.raw) > 0
}
// SetRaw sets the responses raw output
func (r *Response) SetRaw(b []byte) {
r.raw = b
r.Data = nil
}
// Raw returns the raw data for the request
func (r *Response) Raw() []byte {
return r.raw
}
// Status sets the status code for the response
func (r Response) Status(code int) Response {
r.StatusCode = code
return r
}
// Respond sets teh response data
func (r Response) Respond(data interface{}) Response {
r.Data = data
return r
}
// R aliases Respond
func (r Response) R(data interface{}) Response {
return r.Respond(data)
}
// S aliases Status
func (r Response) S(code int) Response {
return r.Status(code)
}
// MetaValue sets the metadata key for the response
func (r Response) MetaValue(k string, v interface{}) Response {
r.Meta[k] = v
return r
}