-
Notifications
You must be signed in to change notification settings - Fork 6
/
response.go
155 lines (127 loc) · 3.54 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
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
package fetch
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"github.com/go-zoox/headers"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v3"
)
// Response is the fetch response
type Response struct {
Status int
Headers http.Header
Body []byte
resultCache gjson.Result
parsed bool
//
Request *Config
//
Stream io.ReadCloser
}
// String returns the body as string
func (r *Response) String() string {
return string(r.Body)
}
// Value returns the body as gjson.Result
func (r *Response) Value() gjson.Result {
if !r.parsed {
r.resultCache = gjson.Parse(r.String())
r.parsed = true
}
return r.resultCache
}
// Get returns the value of the key
func (r *Response) Get(key string) gjson.Result {
return r.Value().Get(key)
}
// JSON returns the body as json string
func (r *Response) JSON() (string, error) {
raw := r.String()
b, err := json.MarshalIndent(gjson.Parse(raw).Value(), "", " ")
if err != nil {
return "", errors.New("invalid json: " + raw)
}
return string(b), nil
}
// func (r *Response) Unmarshal(v interface{}) error {
// return json.Unmarshal(r.Body, v)
// // return decode(v, r)
// }
// UnmarshalJSON unmarshals body to json struct
//
// @TODO bug when lint (go vet) method UnmarshalJSON(v interface{}) error should have signature UnmarshalJSON([]byte) error
func (r *Response) UnmarshalJSON(v interface{}) error {
return json.Unmarshal(r.Body, v)
}
// UnmarshalYAML unmarshals body to yaml struct
func (r *Response) UnmarshalYAML(v interface{}) error {
return yaml.Unmarshal(r.Body, v)
}
// Ok returns true if status code is 2xx
func (r *Response) Ok() bool {
return r.Status >= 200 && r.Status < 300
}
// Error returns error with status and response string.
func (r *Response) Error() error {
return fmt.Errorf("[%d] %s", r.Status, r.String())
}
// StatusCode returns status code of the response
func (r *Response) StatusCode() int {
return r.Status
}
// StatusText returns status text of the response
func (r *Response) StatusText() string {
return http.StatusText(r.Status)
}
// ContentType returns content type of the response
func (r *Response) ContentType() string {
return r.Headers.Get(headers.ContentType)
}
// Location returns location of the response
func (r *Response) Location() string {
return r.Headers.Get(headers.Location)
}
// ContentLength returns content length of the response
func (r *Response) ContentLength() int {
vs := r.Headers.Get(headers.ContentLength)
if vs == "" {
return 0
}
value, err := strconv.Atoi(vs)
if err != nil {
return 0
}
return value
}
// ContentEncoding returns content encoding of the response
func (r *Response) ContentEncoding() string {
return r.Headers.Get(headers.ContentEncoding)
}
// TransferEncoding returns transfer encoding of the response
func (r *Response) TransferEncoding() string {
return r.Headers.Get(headers.TransferEncoding)
}
// ContentLanguage returns content language of the response
func (r *Response) ContentLanguage() string {
return r.Headers.Get(headers.ContentLanguage)
}
// XPoweredBy returns x-powered-by of the response
func (r *Response) XPoweredBy() string {
return r.Headers.Get(headers.XPoweredBy)
}
// XRequestID returns x-request-id of the response
func (r *Response) XRequestID() string {
return r.Headers.Get(headers.XRequestID)
}
// AcceptRanges returns x-accept-ranges of the response
func (r *Response) AcceptRanges() string {
return r.Headers.Get(headers.AcceptRanges)
}
// SetCookie returns set-cookie of the response
func (r *Response) SetCookie() string {
return r.Headers.Get(headers.SetCookie)
}